-
Notifications
You must be signed in to change notification settings - Fork 4
/
models.py
46 lines (37 loc) · 1.64 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import torch
import torchvision
import torch.nn as nn
import torch.nn.functional as F
class GlobalAvgPool2d(nn.Module):
def __init__(self):
super(GlobalAvgPool2d, self).__init__()
def forward(self, feature_map):
return F.adaptive_avg_pool2d(feature_map, 1).squeeze(-1).squeeze(-1)
class ImageClassifier(torch.nn.Module):
def __init__(self, P):
super(ImageClassifier, self).__init__()
self.arch = P['arch']
if P['dataset'] == 'OPENIMAGES':
feature_extractor = torchvision.models.resnet101(pretrained=P['use_pretrained'])
else:
feature_extractor = torchvision.models.resnet50(pretrained=P['use_pretrained'])
feature_extractor = torch.nn.Sequential(*list(feature_extractor.children())[:-2])
if P['freeze_feature_extractor']:
for param in feature_extractor.parameters():
param.requires_grad = False
else:
for param in feature_extractor.parameters():
param.requires_grad = True
self.feature_extractor = feature_extractor
self.avgpool = GlobalAvgPool2d()
self.onebyone_conv = nn.Conv2d(P['feat_dim'], P['num_classes'], 1)
self.alpha = P['alpha']
def unfreeze_feature_extractor(self):
for param in self.feature_extractor.parameters():
param.requires_grad = True
def forward(self, x):
feats = self.feature_extractor(x)
CAM = self.onebyone_conv(feats)
CAM = torch.where(CAM > 0, CAM * self.alpha, CAM) # BoostLU operation
logits = F.adaptive_avg_pool2d(CAM, 1).squeeze(-1).squeeze(-1)
return logits