Пример #1
0
 def test_creates_probabilities(self):
     activations_model = pytorch_custom()
     brain_model = ModelCommitment(identifier=activations_model.identifier,
                                   activations_model=activations_model,
                                   layers=None,
                                   behavioral_readout_layer='relu2')
     fitting_stimuli = StimulusSet({
         'image_id': ['rgb1', 'rgb2'],
         'image_label': ['label1', 'label2']
     })
     fitting_stimuli.image_paths = {
         'rgb1': os.path.join(os.path.dirname(__file__), 'rgb1.jpg'),
         'rgb2': os.path.join(os.path.dirname(__file__), 'rgb2.jpg')
     }
     fitting_stimuli.name = 'test_probabilities_mapping.creates_probabilities'
     brain_model.start_task(BrainModel.Task.probabilities, fitting_stimuli)
     probabilities = brain_model.look_at(fitting_stimuli)
     np.testing.assert_array_equal(probabilities.dims,
                                   ['presentation', 'choice'])
     np.testing.assert_array_equal(probabilities.shape, [2, 2])
     np.testing.assert_array_almost_equal(
         probabilities.sel(image_id='rgb1', choice='label1').values,
         probabilities.sel(image_id='rgb2', choice='label2').values)
     assert probabilities.sel(image_id='rgb1', choice='label1') + \
            probabilities.sel(image_id='rgb1', choice='label2') == approx(1)
Пример #2
0
    def test_model(self):
        target_model_pool = pytorch_custom(28)
        stimuli_model_pool = pytorch_custom(224)
        search_target_model_param = {}
        search_stimuli_model_param = {}
        search_target_model_param['target_model'] = target_model_pool
        search_stimuli_model_param['stimuli_model'] = stimuli_model_pool
        search_target_model_param['target_layer'] = 'relu1'
        search_stimuli_model_param['stimuli_layer'] = 'relu1'
        search_target_model_param['target_img_size'] = 28
        search_stimuli_model_param['search_image_size'] = 224

        model = ModelCommitment(identifier=stimuli_model_pool.identifier, activations_model=None, layers=['relu1'], search_target_model_param=search_target_model_param, search_stimuli_model_param=search_stimuli_model_param)
        assemblies = brainscore.get_assembly('klab.Zhang2018search_obj_array')
        stimuli = assemblies.stimulus_set
        fix = [[640, 512],
               [365, 988],
               [90, 512],
               [365, 36],
               [915, 36],
               [1190, 512],
               [915, 988]]
        max_fix = 6
        data_len = 300
        model.start_task(BrainModel.Task.visual_search_obj_arr, fix=fix, max_fix=max_fix, data_len=data_len)
        cumm_perf, saccades = model.look_at(stimuli)

        assert saccades.shape == (300, 8, 2)
        assert cumm_perf.shape == (7, 2)
Пример #3
0
 def load(identifier=basemodel_identifier, activations_model=activations_model, layers=layers):
     assert hasattr(activations_model, 'reload')
     activations_model.reload()
     brain_model = ModelCommitment(identifier=identifier, activations_model=activations_model,
                                   layers=layers)
     for region in regions:
         brain_model.commit_region(region)
     return brain_model
Пример #4
0
    def test_commit_record(self, model_ctr, layers, expected_layer, region):
        activations_model = model_ctr()
        brain_model = ModelCommitment(identifier=activations_model.identifier, activations_model=activations_model,
                                      layers=layers)

        brain_model.start_recording(region, [(70, 170)])
        predictions = brain_model.look_at([Path(__file__).parent / 'rgb1.jpg'])
        assert set(predictions['region'].values) == {region}
        assert set(predictions['layer'].values) == {expected_layer}
Пример #5
0
 def test_creates_synset(self, model_ctr):
     np.random.seed(0)
     activations_model = model_ctr()
     brain_model = ModelCommitment(identifier=activations_model.identifier, activations_model=activations_model,
                                   layers=None, behavioral_readout_layer='dummy')  # not needed
     stimuli = StimulusSet({'image_id': ['abc123']})
     stimuli.image_paths = {'abc123': os.path.join(os.path.dirname(__file__), 'rgb1.jpg')}
     stimuli.name = 'test_logits_behavior.creates_synset'
     brain_model.start_task(BrainModel.Task.label, 'imagenet')
     synsets = brain_model.look_at(stimuli)
     assert len(synsets) == 1
     assert synsets[0].startswith('n')
Пример #6
0
def check_processing(model, module):
    os.environ['RESULTCACHING_DISABLE'] = '1'
    model_instance = module.get_model(model)
    layers = module.get_layers(model)
    brain_model = ModelCommitment(identifier=model,
                                  activations_model=model_instance,
                                  layers=layers,
                                  region_benchmarks={'IT': _MockBenchmark()})
    brain_model.commit_region('IT')
    benchmark = _MockBenchmark()
    score = benchmark(brain_model, do_behavior=True)
    assert score is not None
    assert score.sel(aggregation='center')
Пример #7
0
def mobilenet_brainmodel(identifier, init_weights=True, function=None):
    model = get_mobilenet(identifier, init_weights)
    if identifier == 'mobilenet_v1_1.0_224':
        lay = layers['mobilenet_v1']
    else:
        lay = layers['mobilenet_pytorch']
    brain_model = ModelCommitment(identifier=identifier,
                                  activations_model=model,
                                  layers=lay)
    return brain_model
Пример #8
0
 def load(identifier=basemodel_identifier,
          activations_model=activations_model,
          layers=model_layer):
     assert hasattr(activations_model, 'reload')
     activations_model.reload()
     brain_model = ModelCommitment(
         identifier=identifier,
         activations_model=activations_model,
         layers=layers)
     return brain_model
Пример #9
0
def hmax_brainmodel(identifier, init_weights=True, function=None):
    model = get_hmax(identifier, 224)
    if identifier.startswith('hmax_2'):
        lay = layers['hmax_2']
    elif identifier.startswith('hmax_3'):
        lay = layers['hmax_3']
    else:
        lay = layers['hmax']
    brain_model = ModelCommitment(identifier=identifier,
                                  activations_model=model,
                                  layers=lay)
    return brain_model
Пример #10
0
            def load(identifier=basemodel_identifier,
                     activations_model=activations_model,
                     layers=layers,
                     target_model=target_model,
                     stimuli_model=stimuli_model):
                assert hasattr(activations_model, 'reload')
                activations_model.reload()

                search_target_model_param = {}
                search_stimuli_model_param = {}
                if (vs_model_param is not None) and (
                        identifier == 'vgg-16'
                ):  #as vs_layer is implemented only for vgg-16 as of now
                    search_target_model_param['target_model'] = target_model
                    search_stimuli_model_param['stimuli_model'] = stimuli_model
                    search_target_model_param[
                        'target_layer'] = visual_search_layer[identifier][0]
                    search_stimuli_model_param[
                        'stimuli_layer'] = visual_search_layer[identifier][0]
                    search_target_model_param[
                        'target_img_size'] = target_img_size
                    search_stimuli_model_param[
                        'search_image_size'] = stimuli_img_size
                else:
                    search_target_model_param['target_model'] = None
                    search_stimuli_model_param['stimuli_model'] = None
                    search_target_model_param['target_layer'] = None
                    search_stimuli_model_param['stimuli_layer'] = None
                    search_target_model_param['target_img_size'] = None
                    search_stimuli_model_param['search_image_size'] = None

                brain_model = ModelCommitment(
                    identifier=identifier,
                    activations_model=activations_model,
                    layers=layers,
                    search_target_model_param=search_target_model_param,
                    search_stimuli_model_param=search_stimuli_model_param)
                for region in regions:
                    brain_model.commit_region(region)
                return brain_model
Пример #11
0
 def test_creates_synset(self, model_ctr):
     np.random.seed(0)
     activations_model = model_ctr()
     brain_model = ModelCommitment(
         identifier=activations_model.identifier,
         activations_model=activations_model,
         layers=None,
         behavioral_readout_layer='dummy')  # not needed
     stimuli = StimulusSet({
         'image_id': ['1', '2'],
         'filename': ['rgb1', 'rgb2']
     })
     stimuli.image_paths = {
         '1': os.path.join(os.path.dirname(__file__), 'rgb1.jpg'),
         '2': os.path.join(os.path.dirname(__file__), 'rgb2.jpg')
     }
     stimuli.identifier = 'test_logits_behavior.creates_synset'
     brain_model.start_task(BrainModel.Task.label, 'imagenet')
     behavior = brain_model.look_at(stimuli)
     assert isinstance(behavior, BehavioralAssembly)
     assert set(behavior['image_id'].values) == {'1', '2'}
     assert len(behavior['synset']) == 2
     assert behavior['synset'].values[0].startswith('n')
Пример #12
0
def get_vonenet_commitment(identifier,
                           activations_model,
                           layers,
                           visual_degrees=8,
                           stochastic=True):
    if stochastic:
        model_commitment = StochasticModelCommitment(
            identifier=identifier,
            activations_model=activations_model,
            layers=layers,
            visual_degrees=visual_degrees)
    else:
        model_commitment = ModelCommitment(identifier=identifier,
                                           activations_model=activations_model,
                                           layers=layers,
                                           visual_degrees=visual_degrees)
    model_commitment.layer_model.region_layer_map['V1'] = 'vone_block.output'
    return model_commitment
Пример #13
0
def check_processing(model_identifier, module):
    os.environ['RESULTCACHING_DISABLE'] = '1'
    model_instance = module.get_model(model_identifier)
    layers = module.get_layers(model_identifier)
    benchmark = _MockBenchmark()
    layer_selection = LayerSelection(model_identifier=model_identifier,
                                     activations_model=model_instance,
                                     layers=layers,
                                     visual_degrees=8)
    region_layer_map = RegionLayerMap(layer_selection=layer_selection,
                                      region_benchmarks={'IT': benchmark})

    brain_model = ModelCommitment(identifier=model_identifier,
                                  activations_model=model_instance,
                                  layers=layers,
                                  region_layer_map=region_layer_map)
    score = benchmark(brain_model, do_behavior=True)
    assert score is not None
    assert score.sel(aggregation='center')
Пример #14
0
def resnet_brainmodel(identifier, init_weights=True, function=None):
    model = resnet50(identifier, init_weights)
    brain_model = ModelCommitment(identifier=identifier,
                                  activations_model=model,
                                  layers=layers['resnet-50'])
    return brain_model
Пример #15
0
def alexnet_brainmodel(identifier, init_weights=True, function=None):
    model = alexnet(identifier, init_weights)
    brain_model = ModelCommitment(identifier=identifier,
                                  activations_model=model,
                                  layers=layers['alexnet'])
    return brain_model
Пример #16
0
 def load(identifier=basemodel_identifier, activations_model=activations_model, layers=model_layer):
     assert hasattr(activations_model, 'reload')
     activations_model.reload()
     from model_tools.brain_transformation import ModelCommitment
     brain_model = ModelCommitment(identifier=identifier, activations_model=activations_model, layers=layers)
     return brain_model
Пример #17
0
 def test_standard_commitment(self):
     brain_model = ModelCommitment(identifier=fullname(self),
                                   activations_model=None,
                                   layers=['dummy'])
     assert brain_model.visual_degrees() == 8