Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

fit to fitting_stimuli in LabelBehavior instead of assuming pretrained imagenet #50

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions model_tools/brain_transformation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from brainscore.model_interface import BrainModel
from brainscore.utils import LazyLoad
from model_tools.brain_transformation.temporal import TemporalIgnore
from .behavior import BehaviorArbiter, LogitsBehavior, ProbabilitiesMapping
from .behavior import BehaviorArbiter, LabelBehavior, ProbabilitiesMapping
from .neural import LayerMappedModel, LayerSelection, LayerScores


Expand All @@ -28,10 +28,10 @@ def __init__(self, identifier,
self.region_benchmarks = {**self.standard_region_benchmarks, **(region_benchmarks or {})}
layer_model = LayerMappedModel(identifier=identifier, activations_model=activations_model)
self.layer_model = TemporalIgnore(layer_model)
logits_behavior = LogitsBehavior(identifier=identifier, activations_model=activations_model)
behavioral_readout_layer = behavioral_readout_layer or layers[-1]
probabilities_behavior = ProbabilitiesMapping(identifier=identifier, activations_model=activations_model,
layer=behavioral_readout_layer)
logits_behavior = LabelBehavior(probabilities_mapping=probabilities_behavior)
self.behavior_model = BehaviorArbiter({BrainModel.Task.label: logits_behavior,
BrainModel.Task.probabilities: probabilities_behavior})
self.do_behavior = False
Expand Down
36 changes: 15 additions & 21 deletions model_tools/brain_transformation/behavior.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from collections import OrderedDict

import sklearn.linear_model
Expand All @@ -22,32 +21,27 @@ def look_at(self, stimuli, *args, **kwargs):
return self.current_executor.look_at(stimuli, *args, **kwargs)


class LogitsBehavior(BrainModel):
def __init__(self, identifier, activations_model):
self.identifier = identifier
self.activations_model = activations_model
self.current_task = None
class LabelBehavior(BrainModel):
def __init__(self, probabilities_mapping):
self.probabilities_mapping = probabilities_mapping

def start_task(self, task: BrainModel.Task, fitting_stimuli):
assert task in [BrainModel.Task.passive, BrainModel.Task.label]
if task == BrainModel.Task.label:
assert fitting_stimuli == 'imagenet'
self.current_task = task
self.probabilities_mapping.start_task(BrainModel.Task.probabilities, fitting_stimuli=fitting_stimuli)

def look_at(self, stimuli, number_of_trials=1):
if self.current_task is BrainModel.Task.passive:
return
logits = self.activations_model(stimuli, layers=['logits'])
assert len(logits['neuroid']) == 1000
logits = logits.transpose('presentation', 'neuroid')
prediction_indices = logits.values.argmax(axis=1)
with open(os.path.join(os.path.dirname(__file__), 'imagenet_classes.txt')) as f:
synsets = f.read().splitlines()
prediction_synsets = [synsets[index] for index in prediction_indices]
return BehavioralAssembly([prediction_synsets], coords={
**{coord: (dims, values) for coord, dims, values in walk_coords(logits['presentation'])},
**{'synset': ('presentation', prediction_synsets), 'logit': ('presentation', prediction_indices)}},
dims=['choice', 'presentation'])
probabilities = self.probabilities_mapping.look_at(stimuli=stimuli, number_of_trials=number_of_trials)
assert len(probabilities['choice']) == 1_000
prediction_indices = probabilities.argmax('choice').values
predictions = probabilities['choice'].values[prediction_indices]
return BehavioralAssembly(predictions, coords={
**{coord: (dims, values) for coord, dims, values in walk_coords(probabilities['presentation'])},
**{'synset': ('presentation', predictions),
'logit': ('presentation', predictions),
'label': ('presentation', predictions),
'choice': ('presentation', predictions)}},
dims=['presentation'])


class ProbabilitiesMapping(BrainModel):
Expand Down