-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
228 lines (177 loc) · 10.1 KB
/
model.py
File metadata and controls
228 lines (177 loc) · 10.1 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import torch
import torch.nn as nn
import torch.nn.functional as F
from models.resnet_features import resnet18_features, resnet34_features, resnet50_features, resnet101_features, resnet152_features
from models.densenet_features import densenet121_features, densenet161_features, densenet169_features, densenet201_features
from models.vgg_features import vgg11_features, vgg11_bn_features, vgg13_features, vgg13_bn_features, vgg16_features, vgg16_bn_features,\
vgg19_features, vgg19_bn_features
from util.receptive_field import compute_proto_layer_rf_info_v2
base_architecture_to_features = {'resnet18': resnet18_features,
'resnet34': resnet34_features,
'resnet50': resnet50_features,
'resnet101': resnet101_features,
'resnet152': resnet152_features,
'densenet121': densenet121_features,
'densenet161': densenet161_features,
'densenet169': densenet169_features,
'densenet201': densenet201_features,
'vgg11': vgg11_features,
'vgg11_bn': vgg11_bn_features,
'vgg13': vgg13_features,
'vgg13_bn': vgg13_bn_features,
'vgg16': vgg16_features,
'vgg16_bn': vgg16_bn_features,
'vgg19': vgg19_features,
'vgg19_bn': vgg19_bn_features}
class TESNet(nn.Module):
def __init__(self, features, img_size, prototype_shape,
proto_layer_rf_info, num_classes, init_weights=True,
prototype_activation_function='log',
add_on_layers_type='bottleneck'):
super(TESNet, self).__init__()
self.img_size = img_size
self.prototype_shape = prototype_shape
self.num_prototypes = prototype_shape[0]
self.num_classes = num_classes
self.epsilon = 1e-4
self.prototype_activation_function = prototype_activation_function #log
assert(self.num_prototypes % self.num_classes == 0)
# a onehot indication matrix for each prototype's class identity
self.prototype_class_identity = torch.zeros(self.num_prototypes,
self.num_classes)
self.num_prototypes_per_class = self.num_prototypes // self.num_classes
for j in range(self.num_prototypes):
self.prototype_class_identity[j, j // self.num_prototypes_per_class] = 1
self.proto_layer_rf_info = proto_layer_rf_info
self.features = features #
features_name = str(self.features).upper()
if features_name.startswith('VGG') or features_name.startswith('RES'):
first_add_on_layer_in_channels = \
[i for i in features.modules() if isinstance(i, nn.Conv2d)][-1].out_channels
elif features_name.startswith('DENSE'):
first_add_on_layer_in_channels = \
[i for i in features.modules() if isinstance(i, nn.BatchNorm2d)][-1].num_features
else:
raise Exception('other base base_architecture NOT implemented')
if add_on_layers_type == 'bottleneck':
add_on_layers = []
current_in_channels = first_add_on_layer_in_channels
while (current_in_channels > self.prototype_shape[1]) or (len(add_on_layers) == 0):
current_out_channels = max(self.prototype_shape[1], (current_in_channels // 2))
add_on_layers.append(nn.Conv2d(in_channels=current_in_channels,
out_channels=current_out_channels,
kernel_size=1))
add_on_layers.append(nn.ReLU())
add_on_layers.append(nn.Conv2d(in_channels=current_out_channels,
out_channels=current_out_channels,
kernel_size=1))
if current_out_channels > self.prototype_shape[1]:
add_on_layers.append(nn.ReLU())
else:
assert(current_out_channels == self.prototype_shape[1])
add_on_layers.append(nn.Sigmoid())
current_in_channels = current_in_channels // 2
self.add_on_layers = nn.Sequential(*add_on_layers)
else:
self.add_on_layers = nn.Sequential(
nn.Conv2d(in_channels=first_add_on_layer_in_channels, out_channels=self.prototype_shape[1], kernel_size=1),
nn.ReLU(),
nn.Conv2d(in_channels=self.prototype_shape[1], out_channels=self.prototype_shape[1], kernel_size=1),
nn.Sigmoid()
)
self.prototype_vectors = nn.Parameter(torch.rand(self.prototype_shape),
requires_grad=True)
self.ones = nn.Parameter(torch.ones(self.prototype_shape),
requires_grad=False)
self.last_layer = nn.Linear(self.num_prototypes, self.num_classes,
bias=False)
if init_weights:
self._initialize_weights()
def conv_features(self, x):
x = self.features(x)
x = self.add_on_layers(x)
return x
def _cosine_convolution(self, x):
x = F.normalize(x,p=2,dim=1)
now_prototype_vectors = F.normalize(self.prototype_vectors,p=2,dim=1)
distances = F.conv2d(input=x, weight=now_prototype_vectors)
distances = -distances
return distances
def _project2basis(self,x):
now_prototype_vectors = F.normalize(self.prototype_vectors, p=2, dim=1)
distances = F.conv2d(input=x, weight=now_prototype_vectors)
return distances
def prototype_distances(self, x):
conv_features = self.conv_features(x)
cosine_distances = self._cosine_convolution(conv_features)
project_distances = self._project2basis(conv_features)
return project_distances,cosine_distances
def distance_2_similarity(self, distances):
if self.prototype_activation_function == 'log':
return torch.log((distances + 1) / (distances + self.epsilon))
elif self.prototype_activation_function == 'linear':
return -distances
else:
raise Exception('other activation function NOT implemented')
def global_min_pooling(self,distances):
min_distances = -F.max_pool2d(-distances,
kernel_size=(distances.size()[2],
distances.size()[3]))
min_distances = min_distances.view(-1, self.num_prototypes)
return min_distances
def global_max_pooling(self,distances):
max_distances = F.max_pool2d(distances,
kernel_size=(distances.size()[2],
distances.size()[3]))
max_distances = max_distances.view(-1, self.num_prototypes)
return max_distances
def forward(self, x):
project_distances,cosine_distances = self.prototype_distances(x)
cosine_min_distances = self.global_min_pooling(cosine_distances)
project_max_distances = self.global_max_pooling(project_distances)
prototype_activations = project_max_distances
logits = self.last_layer(prototype_activations)
return logits, cosine_min_distances
def push_forward(self, x):
conv_output = self.conv_features(x) #[batchsize,128,14,14]
distances = self._project2basis(conv_output)
distances = - distances
return conv_output, distances
def set_last_layer_incorrect_connection(self, incorrect_strength):
positive_one_weights_locations = torch.t(self.prototype_class_identity)
negative_one_weights_locations = 1 - positive_one_weights_locations
correct_class_connection = 1
incorrect_class_connection = incorrect_strength
self.last_layer.weight.data.copy_(
correct_class_connection * positive_one_weights_locations
+ incorrect_class_connection * negative_one_weights_locations)
def _initialize_weights(self):
for m in self.add_on_layers.modules():
if isinstance(m, nn.Conv2d):
# every init technique has an underscore _ in the name
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
self.set_last_layer_incorrect_connection(incorrect_strength=-0.5)
def construct_TesNet(base_architecture, pretrained=True, img_size=224,
prototype_shape=(2000, 128, 1, 1), num_classes=200,
prototype_activation_function='log',
add_on_layers_type='bottleneck'):
features = base_architecture_to_features[base_architecture](pretrained=pretrained)
layer_filter_sizes, layer_strides, layer_paddings = features.conv_info()
proto_layer_rf_info = compute_proto_layer_rf_info_v2(img_size=img_size,#224
layer_filter_sizes=layer_filter_sizes,#
layer_strides=layer_strides,
layer_paddings=layer_paddings,
prototype_kernel_size=prototype_shape[2])
return TESNet(features=features,
img_size=img_size,
prototype_shape=prototype_shape,
proto_layer_rf_info=proto_layer_rf_info,
num_classes=num_classes,
init_weights=True,
prototype_activation_function=prototype_activation_function,
add_on_layers_type=add_on_layers_type)