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

Allow execution on CPU, fix ImageNet head for timm models #216

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion examples/task_adaptation/image_classification/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ def get_model(model_name, pretrained_checkpoint=None):
backbone = timm.create_model(model_name, pretrained=True)
try:
backbone.out_features = backbone.get_classifier().in_features
backbone.fc_imagenet = backbone.fc
backbone.reset_classifier(0, '')
backbone.copy_head = backbone.get_classifier
backbone.copy_head = lambda: copy.deepcopy(backbone.fc_imagenet)
except:
backbone.out_features = backbone.head.in_features
backbone.head = nn.Identity()
Expand Down
9 changes: 6 additions & 3 deletions tllib/normalization/stochnorm.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,20 @@ def forward(self, input):
if input.dim() == 2:
s = torch.from_numpy(
np.random.binomial(n=1, p=self.p, size=self.num_features).reshape(1,
self.num_features)).float().cuda()
self.num_features)).float()
elif input.dim() == 3:
s = torch.from_numpy(
np.random.binomial(n=1, p=self.p, size=self.num_features).reshape(1, self.num_features,
1)).float().cuda()
1)).float()
elif input.dim() == 4:
s = torch.from_numpy(
np.random.binomial(n=1, p=self.p, size=self.num_features).reshape(1, self.num_features, 1,
1)).float().cuda()
1)).float()
else:
raise BaseException()

if torch.cuda.is_available():
s = s.cuda()

z = (1 - s) * z_0 + s * z_1
else:
Expand Down