Repository navigation
Expand file tree
/
Copy pathnetwork.py
More file actions
193 lines (174 loc) · 6.63 KB
/
Copy pathnetwork.py
File metadata and controls
193 lines (174 loc) · 6.63 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
import numpy as np
import torch
import torch.nn as nn
import torchvision
from torchvision import models
from torch.autograd import Variable
import math
import torch.nn.utils.weight_norm as weightNorm
from collections import OrderedDict
from TransUNet.networks.vit_seg_modeling import VisionTransformer as ViT_seg
from TransUNet.networks.vit_seg_modeling import CONFIGS as CONFIGS_ViT_seg
from non_local_embedded_gaussian import NONLocalBlock2D
def calc_coeff(iter_num, high=1.0, low=0.0, alpha=10.0, max_iter=10000.0):
return np.float(2.0 * (high - low) / (1.0 + np.exp(-alpha*iter_num / max_iter)) - (high - low) + low)
def init_weights(m):
classname = m.__class__.__name__
if classname.find('Conv2d') != -1 or classname.find('ConvTranspose2d') != -1:
nn.init.kaiming_uniform_(m.weight)
nn.init.zeros_(m.bias)
elif classname.find('BatchNorm') != -1:
nn.init.normal_(m.weight, 1.0, 0.02)
nn.init.zeros_(m.bias)
elif classname.find('Linear') != -1:
nn.init.xavier_normal_(m.weight)
nn.init.zeros_(m.bias)
vgg_dict = {"vgg11":models.vgg11, "vgg13":models.vgg13, "vgg16":models.vgg16, "vgg19":models.vgg19,
"vgg11bn":models.vgg11_bn, "vgg13bn":models.vgg13_bn, "vgg16bn":models.vgg16_bn, "vgg19bn":models.vgg19_bn}
class VGGBase(nn.Module):
def __init__(self, vgg_name):
super(VGGBase, self).__init__()
model_vgg = vgg_dict[vgg_name](pretrained=True)
self.features = model_vgg.features
self.classifier = nn.Sequential()
for i in range(6):
self.classifier.add_module("classifier"+str(i), model_vgg.classifier[i])
self.in_features = model_vgg.classifier[6].in_features
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
res_dict = {"resnet18":models.resnet18, "resnet34":models.resnet34, "resnet50":models.resnet50,
"resnet101":models.resnet101, "resnet152":models.resnet152}
class ViT(nn.Module):
def __init__(self):
super(ViT, self).__init__()
config_vit = CONFIGS_ViT_seg['R50-ViT-B_16']
config_vit.n_classes = 100
config_vit.n_skip = 3
config_vit.patches.grid = (int(224 / 16), int(224 / 16))
self.feature_extractor = ViT_seg(config_vit, img_size=[224, 224], num_classes=config_vit.n_classes)
self.feature_extractor.load_from(weights=np.load(config_vit.pretrained_path))
self.in_features = 2048
def forward(self, x):
_, feat = self.feature_extractor(x)
return feat
class ResBase(nn.Module):
def __init__(self, res_name,se=False, nl=False):
super(ResBase, self).__init__()
model_resnet = res_dict[res_name](pretrained=True)
self.conv1 = model_resnet.conv1
self.bn1 = model_resnet.bn1
self.relu = model_resnet.relu
self.maxpool = model_resnet.maxpool
self.layer1 = model_resnet.layer1
self.layer2 = model_resnet.layer2
self.layer3 = model_resnet.layer3
self.layer4 = model_resnet.layer4
self.avgpool = model_resnet.avgpool
self.in_features = model_resnet.fc.in_features
self.se=se
self.nl=nl
if self.se:
self.SELayer=SELayer(self.in_features)
if self.nl:
self.nlLayer=NONLocalBlock2D(self.in_features)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
if self.se:
x=self.SELayer(x)
if self.nl:
x=self.nlLayer(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
return x
class feat_bootleneck(nn.Module):
def __init__(self, feature_dim, bottleneck_dim=256, type="ori"):
super(feat_bootleneck, self).__init__()
self.bn = nn.BatchNorm1d(bottleneck_dim, affine=True)
self.relu = nn.ReLU(inplace=True)
self.dropout = nn.Dropout(p=0.5)
self.bottleneck = nn.Linear(feature_dim, bottleneck_dim)
self.bottleneck.apply(init_weights)
self.type = type
def forward(self, x):
x = self.bottleneck(x)
if self.type == "bn":
x = self.bn(x)
return x
class feat_classifier(nn.Module):
def __init__(self, class_num, bottleneck_dim=256, type="linear"):
super(feat_classifier, self).__init__()
self.type = type
if type == 'wn':
self.fc = weightNorm(nn.Linear(bottleneck_dim, class_num), name="weight")
self.fc.apply(init_weights)
else:
self.fc = nn.Linear(bottleneck_dim, class_num)
self.fc.apply(init_weights)
def forward(self, x):
x = self.fc(x)
return x
class feat_classifier_two(nn.Module):
def __init__(self, class_num, input_dim, bottleneck_dim=256):
super(feat_classifier_two, self).__init__()
self.type = type
self.fc0 = nn.Linear(input_dim, bottleneck_dim)
self.fc0.apply(init_weights)
self.fc1 = nn.Linear(bottleneck_dim, class_num)
self.fc1.apply(init_weights)
def forward(self, x):
x = self.fc0(x)
x = self.fc1(x)
return x
class Res50(nn.Module):
def __init__(self):
super(Res50, self).__init__()
model_resnet = models.resnet50(pretrained=True)
self.conv1 = model_resnet.conv1
self.bn1 = model_resnet.bn1
self.relu = model_resnet.relu
self.maxpool = model_resnet.maxpool
self.layer1 = model_resnet.layer1
self.layer2 = model_resnet.layer2
self.layer3 = model_resnet.layer3
self.layer4 = model_resnet.layer4
self.avgpool = model_resnet.avgpool
self.in_features = model_resnet.fc.in_features
self.fc = model_resnet.fc
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
y = self.fc(x)
return x, y
class SELayer(nn.Module):
def __init__(self, channel, reduction=16):
super(SELayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction, bias=False),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel, bias=False),
nn.Sigmoid()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y.expand_as(x)