Пример #1
0
def _get_imagenet_val(num_images):
    _logger = logging.getLogger(fullname(_get_imagenet_val))
    num_classes = 1000
    num_images_per_class = (num_images - 1) // num_classes
    base_indices = np.arange(num_images_per_class).astype(int)
    indices = []
    for i in range(num_classes):
        indices.extend(50 * i + base_indices)
    for i in range((num_images - 1) % num_classes + 1):
        indices.extend(50 * i + np.array([num_images_per_class]).astype(int))

    framework_home = os.path.expanduser(os.getenv('MT_HOME', '~/.model-tools'))
    imagenet_filepath = os.getenv('MT_IMAGENET_PATH', os.path.join(framework_home, 'imagenet2012.hdf5'))
    imagenet_dir = f"{imagenet_filepath}-files"
    os.makedirs(imagenet_dir, exist_ok=True)

    if not os.path.isfile(imagenet_filepath):
        os.makedirs(os.path.dirname(imagenet_filepath), exist_ok=True)
        _logger.debug(f"Downloading ImageNet validation to {imagenet_filepath}")
        s3.download_file("imagenet2012-val.hdf5", imagenet_filepath)

    filepaths = []
    with h5py.File(imagenet_filepath, 'r') as f:
        for index in indices:
            imagepath = os.path.join(imagenet_dir, f"{index}.png")
            if not os.path.isfile(imagepath):
                image = np.array(f['val/images'][index])
                Image.fromarray(image).save(imagepath)
            filepaths.append(imagepath)

    return filepaths
Пример #2
0
 def __init__(self, layer_model):
     self._logger = logging.getLogger(fullname(self))
     self._layer_model = layer_model
     self.region_layer_map = self._layer_model.region_layer_map
     self.activations_model = self._layer_model.activations_model
     self.start_task = self._layer_model.start_task
     self._time_bins = None
Пример #3
0
    def __init__(self, get_activations, preprocessing, identifier=False, batch_size=Defaults.batch_size):
        """
        :param identifier: an activations identifier for the stored results file. False to disable saving.
        """
        self._logger = logging.getLogger(fullname(self))

        self._batch_size = batch_size
        self.identifier = identifier
        self.get_activations = get_activations
        self.preprocess = preprocessing or (lambda x: x)
        self._stimulus_set_hooks = {}
        self._batch_activations_hooks = {}
Пример #4
0
 def __init__(self, model, preprocessing, identifier=None, *args, **kwargs):
     import torch
     logger = logging.getLogger(fullname(self))
     self._device = torch.device(
         "cuda" if torch.cuda.is_available() else "cpu")
     logger.debug(f"Using device {self._device}")
     self._model = model
     self._model = self._model.to(self._device)
     identifier = identifier or model.__class__.__name__
     self._extractor = self._build_extractor(
         identifier=identifier,
         preprocessing=preprocessing,
         get_activations=self.get_activations,
         *args,
         **kwargs)
     self._extractor.insert_attrs(self)
Пример #5
0
 def __init__(self, activations_extractor, n_components):
     self._logger = logging.getLogger(fullname(self))
     self._extractor = activations_extractor
     self._n_components = n_components
     self._layer_pcas = {}
Пример #6
0
 def test_standard_commitment(self):
     brain_model = ModelCommitment(identifier=fullname(self),
                                   activations_model=None,
                                   layers=['dummy'])
     assert brain_model.visual_degrees() == 8