Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.
Closed
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
12 changes: 6 additions & 6 deletions model_tools/activations/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ def graph(self):
return g


def load_preprocess_images(image_filepaths, image_size):
def load_preprocess_images(image_filepaths, image_size, normalize_mean=(0.485, 0.456, 0.406), normalize_std=(0.229, 0.224, 0.225)):
images = load_images(image_filepaths)
images = preprocess_images(images, image_size=image_size)
images = preprocess_images(images, image_size=image_size, normalize_mean=normalize_mean, normalize_std=normalize_std)
return images


Expand All @@ -125,18 +125,18 @@ def load_image(image_filepath):
return rgb_image


def preprocess_images(images, image_size):
preprocess = torchvision_preprocess_input(image_size)
def preprocess_images(images, image_size, normalize_mean=(0.485, 0.456, 0.406), normalize_std=(0.229, 0.224, 0.225)):
preprocess = torchvision_preprocess_input(image_size, normalize_mean, normalize_std)
images = [preprocess(image) for image in images]
images = np.concatenate(images)
return images


def torchvision_preprocess_input(image_size):
def torchvision_preprocess_input(image_size, normalize_mean=(0.485, 0.456, 0.406), normalize_std=(0.229, 0.224, 0.225)):
from torchvision import transforms
return transforms.Compose([
transforms.Resize(image_size),
torchvision_preprocess(),
torchvision_preprocess(normalize_mean, normalize_std),
])


Expand Down
13 changes: 10 additions & 3 deletions model_tools/brain_transformation/behavior.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ def __init__(self, identifier, activations_model, layer):
def start_task(self, task: BrainModel.Task, fitting_stimuli):
assert task in [BrainModel.Task.passive, BrainModel.Task.probabilities]
self.current_task = task

fitting_features = self.activations_model(fitting_stimuli, layers=[self.layer])
if type(self.layer) is not list:
layers = [self.layer]
else:
layers = self.layer
fitting_features = self.activations_model(fitting_stimuli, layers=layers)
Comment on lines -60 to +64

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current models did not fail because of this, but I understand the confusion from calling it layer and then actually requiring a list. Checking for a list type is unstable though, what about e.g. tuples or other iterables? I'd rather not allow multiple ways to achieve the same thing, and instead make the docs more clear that the passed layer should be a list.

fitting_features = fitting_features.transpose('presentation', 'neuroid')
assert all(fitting_features['image_id'].values == fitting_stimuli['image_id'].values), \
"image_id ordering is incorrect"
Expand All @@ -67,7 +70,11 @@ def start_task(self, task: BrainModel.Task, fitting_stimuli):
def look_at(self, stimuli):
if self.current_task is BrainModel.Task.passive:
return
features = self.activations_model(stimuli, layers=[self.layer])
if type(self.layer) is not list:
layers = [self.layer]
else:
layers = self.layer
features = self.activations_model(stimuli, layers=layers)
features = features.transpose('presentation', 'neuroid')
prediction = self.classifier.predict_proba(features)
return prediction
Expand Down