Example #1
0
def test_bs_PCA_fit():
    img_path_placeholder, ending_points, SESS \
            = _get_cate_seed0_ending_points_sess(
                    64,
                    '/mnt/fs4/chengxuz/brainscore_model_caches/cate_aug/res18/exp_seed0_bn_wd/checkpoint-505505')
    identifier = 'test-cate-bn-wd-pca'
    activations_model = TensorflowSlimWrapper(
            identifier=identifier, labels_offset=0,
            endpoints=ending_points, inputs=img_path_placeholder, 
            session=SESS)

    LayerPCA.hook(activations_model, n_components=1000)
    #benchmark = bench.load('dicarlo.Majaj2015.IT-pls')
    benchmark = bench.load('dicarlo.Majaj2015.V4-pls')
    all_layers = ['encode_%i' % i for i in range(1, 10)]

    model_scores = LayerScores(
            identifier, 
            activations_model=activations_model,
            )
    score = model_scores(
            benchmark=benchmark, 
            layers=all_layers,
            )
    print(score)
Example #2
0
def test_from_stimulus_set(model_ctr, layers, pca_components):
    image_names = [
        'rgb.jpg', 'grayscale.png', 'grayscale2.jpg', 'grayscale_alpha.png'
    ]
    stimulus_set = StimulusSet([{
        'image_id': image_name,
        'some_meta': image_name[::-1]
    } for image_name in image_names])
    stimulus_set.image_paths = {
        image_name: os.path.join(os.path.dirname(__file__), image_name)
        for image_name in image_names
    }

    activations_extractor = model_ctr()
    if pca_components:
        LayerPCA.hook(activations_extractor, pca_components)
    activations = activations_extractor.from_stimulus_set(
        stimulus_set, layers=layers, stimuli_identifier=False)

    assert activations is not None
    assert set(activations['image_id'].values) == set(image_names)
    assert all(activations['some_meta'].values ==
               [image_name[::-1] for image_name in image_names])
    assert len(np.unique(activations['layer'])) == len(layers)
    if pca_components is not None:
        assert len(activations['neuroid']) == pca_components * len(layers)
Example #3
0
 def test_base_model_pool_reload(self):
     activations_model = base_model_pool['alexnet']
     LayerPCA.hook(activations_model, n_components=1000)
     assert len(activations_model._extractor._batch_activations_hooks) == 1
     activations_model = base_model_pool['alexnet']
     assert len(activations_model._extractor._stimulus_set_hooks) == 0
     assert len(activations_model._extractor._batch_activations_hooks) == 0
     LayerPCA.hook(activations_model, n_components=1000)
     assert len(activations_model._extractor._batch_activations_hooks) == 1
Example #4
0
 def load(model_name=model_name, layer=layer, region=region, pca_components=pca_components):
     activations_model = base_model_pool[model_name]
     if pca_components:
         LayerPCA.hook(activations_model, n_components=pca_components)
         activations_model.identifier += "-pca_1000"
     model = LayerMappedModel(f"{model_name}-{layer}", activations_model=activations_model, visual_degrees=8)
     model.commit(region, layer)
     model = TemporalIgnore(model)
     return model
Example #5
0
 def test_brain_translated_pool_reload(self, model_identifier):
     activations_model = brain_translated_pool[model_identifier].content.activations_model
     LayerPCA.hook(activations_model, n_components=1000)
     assert len(activations_model._extractor._batch_activations_hooks) == 1
     activations_model = brain_translated_pool[model_identifier].content.activations_model
     assert len(activations_model._extractor._stimulus_set_hooks) == 0
     assert len(activations_model._extractor._batch_activations_hooks) == 0
     LayerPCA.hook(activations_model, n_components=1000)
     assert len(activations_model._extractor._batch_activations_hooks) == 1
Example #6
0
 def test_MajajHong2015ITpls(self, model_identifier, expected_score, attach_hook):
     model = brain_translated_pool[model_identifier]
     if attach_hook:
         activations_model = model.layer_model._layer_model.activations_model
         LayerPCA.hook(activations_model, n_components=1000)
         identifier = activations_model.identifier + "-pca_1000"
         activations_model.identifier = identifier
     score = score_model(model_identifier, 'dicarlo.MajajHong2015.IT-pls', model=model)
     assert score.raw.sel(aggregation='center') == approx(expected_score, abs=0.005)
Example #7
0
def test_from_stimulus_set(model_ctr, layers, pca_components):
    image_names = ['rgb.jpg', 'grayscale.png', 'grayscale2.jpg', 'grayscale_alpha.png', 'palletized.png']
    stimulus_set = _build_stimulus_set(image_names)

    activations_extractor = model_ctr()
    if pca_components:
        LayerPCA.hook(activations_extractor, pca_components)
    activations = activations_extractor.from_stimulus_set(stimulus_set, layers=layers, stimuli_identifier=False)

    assert activations is not None
    assert set(activations['image_id'].values) == set(image_names)
    assert all(activations['some_meta'].values == [image_name[::-1] for image_name in image_names])
    assert len(np.unique(activations['layer'])) == len(layers)
    if pca_components is not None:
        assert len(activations['neuroid']) == pca_components * len(layers)
Example #8
0
    def __call__(self, selection_identifier, benchmark):
        # for layer-mapping, attach LayerPCA so that we can cache activations
        model_identifier = self.model_identifier
        pca_hooked = LayerPCA.is_hooked(self._layer_scoring._activations_model)
        if not pca_hooked:
            pca_handle = LayerPCA.hook(self._layer_scoring._activations_model, n_components=1000)
            identifier = self._layer_scoring._activations_model.identifier
            self._layer_scoring._activations_model.identifier = identifier + "-pca_1000"
            model_identifier += "-pca_1000"

        result = self._call(model_identifier=model_identifier, selection_identifier=selection_identifier,
                            benchmark=benchmark)

        if not pca_hooked:
            pca_handle.remove()
            self._layer_scoring._activations_model.identifier = identifier
        return result
Example #9
0
def test_from_image_path(model_ctr, layers, image_name, pca_components, logits):
    stimuli_paths = [os.path.join(os.path.dirname(__file__), image_name)]

    activations_extractor = model_ctr()
    if pca_components:
        LayerPCA.hook(activations_extractor, pca_components)
    activations = activations_extractor.from_paths(stimuli_paths=stimuli_paths,
                                                   layers=layers if not logits else None)

    assert activations is not None
    assert len(activations['stimulus_path']) == 1
    assert len(np.unique(activations['layer'])) == len(layers) if not logits else 1
    if logits and not pca_components:
        assert len(activations['neuroid']) == 1000
    elif pca_components is not None:
        assert len(activations['neuroid']) == pca_components * len(layers)
    import gc
    gc.collect()  # free some memory, we're piling up a lot of activations at this point
    return activations