Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inception Preprocessing #1666

Merged
merged 10 commits into from
Jan 8, 2025
46 changes: 34 additions & 12 deletions brainscore_vision/model_helpers/activations/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,38 @@ def preprocess_images(images, image_size, **kwargs):

def torchvision_preprocess_input(image_size, **kwargs):
from torchvision import transforms
return transforms.Compose([
transforms.Resize((image_size, image_size)),
torchvision_preprocess(**kwargs),
])


def torchvision_preprocess(normalize_mean=(0.485, 0.456, 0.406), normalize_std=(0.229, 0.224, 0.225)):
preprocess_type = kwargs.get('preprocess_type', 'imagenet').lower()
if preprocess_type == 'imagenet':
return transforms.Compose([
transforms.Resize((image_size, image_size)),
torchvision_preprocess(**kwargs),
])
elif preprocess_type == 'inception': # inception-style resize
resize_size = int(image_size * 256 / 224)
return transforms.Compose([
transforms.Resize(resize_size),
transforms.CenterCrop(image_size),
torchvision_preprocess(preprocess_type='inception')
])
else:
raise ValueError(f"Unknown preprocess_type '{preprocess_type}'")


def torchvision_preprocess(preprocess_type="imagenet", **kwargs):
from torchvision import transforms
return transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=normalize_mean, std=normalize_std),
lambda img: img.unsqueeze(0)
])
if preprocess_type == "imagenet":
normalize_mean = kwargs.get('normalize_mean', (0.485, 0.456, 0.406))
normalize_std = kwargs.get('normalize_std', (0.229, 0.224, 0.225))
return transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=normalize_mean, std=normalize_std),
lambda img: img.unsqueeze(0)
])
elif preprocess_type == "inception":
return transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
lambda img: img.unsqueeze(0)
])
else:
raise ValueError(f"Unknown preprocess_type '{preprocess_type}'")
Loading