-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathresnet_temporal.py
133 lines (119 loc) · 4.44 KB
/
resnet_temporal.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import torch
from torch import nn
from torch.nn import functional as F
import torchvision
if __package__:
pass
else:
import sys
sys.path.insert(0, '..')
__all__ = ['ResNet50TP', 'ResNet50TA', 'ResNet50RNN']
class ResNet50TP(nn.Module):
def __init__(self, num_classes, loss={'xent'}, **kwargs):
super(ResNet50TP, self).__init__()
self.loss = loss
resnet50 = torchvision.models.resnet50(pretrained=True)
self.base = nn.Sequential(*list(resnet50.children())[:-2])
self.feat_dim = 2048
self.classifier = nn.Linear(self.feat_dim, num_classes)
def forward(self, x):
b = x.size(0)
t = x.size(1)
x = x.view(b * t, x.size(2), x.size(3), x.size(4))
x = self.base(x)
x = F.avg_pool2d(x, x.size()[2:])
x = x.view(b, t, -1)
x = x.permute(0, 2, 1)
f = F.avg_pool1d(x, t)
f = f.view(b, self.feat_dim)
if not self.training:
return f
y = self.classifier(f)
if self.loss == {'xent'}:
return y
elif self.loss == {'xent', 'htri'}:
return y, f
elif self.loss == {'cent'}:
return y, f
else:
raise KeyError("Unsupported loss: {}".format(self.loss))
class ResNet50TA(nn.Module):
def __init__(self, num_classes, loss={'xent'}, **kwargs):
super(ResNet50TA, self).__init__()
self.loss = loss
resnet50 = torchvision.models.resnet50(pretrained=True)
self.base = nn.Sequential(*list(resnet50.children())[:-2])
self.att_gen = 'softmax' # method for attention generation: softmax or sigmoid
self.feat_dim = 2048 # feature dimension
self.middle_dim = 256 # middle layer dimension
self.classifier = nn.Linear(self.feat_dim, num_classes)
# 7,4 cooresponds to 224, 112 input image size
self.attention_conv = nn.Conv2d(self.feat_dim, self.middle_dim, [7, 4])
self.attention_tconv = nn.Conv1d(self.middle_dim, 1, 3, padding=1)
def forward(self, x):
b = x.size(0)
t = x.size(1)
x = x.view(b * t, x.size(2), x.size(3), x.size(4))
x = self.base(x)
a = F.relu(self.attention_conv(x))
a = a.view(b, t, self.middle_dim)
a = a.permute(0, 2, 1)
a = F.relu(self.attention_tconv(a))
a = a.view(b, t)
x = F.avg_pool2d(x, x.size()[2:])
if self.att_gen == 'softmax':
a = F.softmax(a, dim=1)
elif self.att_gen == 'sigmoid':
a = F.sigmoid(a)
a = F.normalize(a, p=1, dim=1)
else:
raise KeyError("Unsupported attention generation function: {}".format(self.att_gen))
x = x.view(b, t, -1)
a = torch.unsqueeze(a, -1)
a = a.expand(b, t, self.feat_dim)
att_x = torch.mul(x, a)
att_x = torch.sum(att_x, 1)
f = att_x.view(b, self.feat_dim)
if not self.training:
return f
y = self.classifier(f)
if self.loss == {'xent'}:
return y
elif self.loss == {'xent', 'htri'}:
return y, f
elif self.loss == {'cent'}:
return y, f
else:
raise KeyError("Unsupported loss: {}".format(self.loss))
class ResNet50RNN(nn.Module):
def __init__(self, num_classes, loss={'xent'}, **kwargs):
super(ResNet50RNN, self).__init__()
self.loss = loss
resnet50 = torchvision.models.resnet50(pretrained=True)
self.base = nn.Sequential(*list(resnet50.children())[:-2])
self.hidden_dim = 512
self.feat_dim = 2048
self.classifier = nn.Linear(self.hidden_dim, num_classes)
self.lstm = nn.LSTM(input_size=self.feat_dim, hidden_size=self.hidden_dim, num_layers=1, batch_first=True)
def forward(self, x):
b = x.size(0)
t = x.size(1)
x = x.view(b * t, x.size(2), x.size(3), x.size(4))
x = self.base(x)
x = F.avg_pool2d(x, x.size()[2:])
x = x.view(b, t, -1)
output, (h_n, c_n) = self.lstm(x)
output = output.permute(0, 2, 1)
f = F.avg_pool1d(output, t)
f = f.view(b, self.hidden_dim)
if not self.training:
return f
y = self.classifier(f)
if self.loss == {'xent'}:
return y
elif self.loss == {'xent', 'htri'}:
return y, f
elif self.loss == {'cent'}:
return y, f
else:
raise KeyError("Unsupported loss: {}".format(self.loss))