-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.py
197 lines (140 loc) · 6.05 KB
/
model.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
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
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import glob
import torch
from torch import nn,optim
from torch.utils.data import DataLoader,Dataset
from torchsummary import summary
from torch.autograd import Function
from torch.optim.lr_scheduler import StepLR
import torchvision.transforms.functional as TF
from torchvision import transforms
from PIL import Image
import pickle
from tqdm.notebook import tqdm
import random
from sklearn import metrics
from skimage import io, filters
import joblib
import json
class GradientReversalFn(Function):
@staticmethod
def forward(ctx, x, alpha):
ctx.alpha = alpha
return x
@staticmethod
def backward(ctx, grad_output):
output = - ctx.alpha * grad_output
return output, None
class Downsample(nn.Module):
def __init__(self):
super(Downsample,self).__init__()
self.pool1 = nn.MaxPool2d(kernel_size=2)
self.conv1 = nn.Conv2d(3,64,kernel_size=3,padding=1)
self.bn1 = nn.BatchNorm2d(64)
self.conv2 = nn.Conv2d(64,64,kernel_size=3,padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.conv3 = nn.Conv2d(64,128,kernel_size=3,padding=1)
self.bn3 = nn.BatchNorm2d(128)
self.conv4 = nn.Conv2d(128,128,kernel_size=3,padding=1)
self.bn4 = nn.BatchNorm2d(128)
self.conv5 = nn.Conv2d(128,256,kernel_size=3,padding=1)
self.bn5 = nn.BatchNorm2d(256)
self.conv6 = nn.Conv2d(256,256,kernel_size=3,padding=1)
self.bn6 = nn.BatchNorm2d(256)
self.conv7 = nn.Conv2d(256,512,kernel_size=3,padding=1)
self.bn7 = nn.BatchNorm2d(512)
self.conv8 = nn.Conv2d(512,512,kernel_size=3,padding=1)
self.bn8 = nn.BatchNorm2d(512)
self.relu = nn.ReLU()
def forward(self,x):
x1 = self.relu(self.bn1(self.conv1(x)))
x2 = self.relu(self.bn2(self.conv2(x1)))
x3 = self.pool1(x2)
x4 = self.relu(self.bn3(self.conv3(x3)))
x5 = self.relu(self.bn4(self.conv4(x4)))
x6 = self.pool1(x5)
x7 = self.relu(self.bn5(self.conv5(x6)))
x8 = self.relu(self.bn6(self.conv6(x7)))
x9 = self.pool1(x8)
x10 = self.relu(self.bn7(self.conv7(x9)))
x11 = self.relu(self.bn8(self.conv8(x10)))
return x2,x5,x8,x11
class Upsample(nn.Module):
def __init__(self):
super(Upsample,self).__init__()
self.deconv2 = nn.ConvTranspose2d(512,256,kernel_size=2,stride=2)
self.conv13 = nn.Conv2d(512,256,kernel_size=3,padding=1)
self.bn13 = nn.BatchNorm2d(256)
self.conv14 = nn.Conv2d(256,256,kernel_size=3,padding=1)
self.bn14 = nn.BatchNorm2d(256)
self.deconv3 = nn.ConvTranspose2d(256,128,kernel_size=2,stride=2)
self.conv15 = nn.Conv2d(256,128,kernel_size=3,padding=1)
self.bn15 = nn.BatchNorm2d(128)
self.conv16 = nn.Conv2d(128,128,kernel_size=3,padding=1)
self.bn16 = nn.BatchNorm2d(128)
self.deconv4 = nn.ConvTranspose2d(128,64,kernel_size=2,stride=2)
self.conv17 = nn.Conv2d(128,64,kernel_size=3,padding=1)
self.bn17 = nn.BatchNorm2d(64)
self.conv18 = nn.Conv2d(64,64,kernel_size=3,padding=1)
self.bn18 = nn.BatchNorm2d(64)
self.conv19 = nn.Conv2d(64,1,kernel_size=1)
self.relu = nn.ReLU()
self.classifier = nn.Sigmoid()
def _merge(self,layer_down,layer_up):
slice_f = layer_up.size()[-1]//2
center = layer_down.size()[-1]//2
s,e = center-slice_f,center+slice_f
x_out = torch.cat((layer_down[:,:,s:e,s:e],(layer_up)),1)
return x_out
def forward(self,x,x2,x5,x8):
x19 = self.deconv2(x)
x20 = self._merge(x19,x8)
x21 = self.relu(self.bn13(self.conv13(x20)))
x22 = self.relu(self.bn14(self.conv14(x21)))
x23 = self.deconv3(x22)
x24 = self._merge(x23,x5)
x25 = self.relu(self.bn15(self.conv15(x24)))
x26 = self.relu(self.bn16(self.conv16(x25)))
x27 = self.deconv4(x26)
x28 = self._merge(x27,x2)
x29 = self.relu(self.bn17(self.conv17(x28)))
x30 = self.relu(self.bn18(self.conv18(x29)))
x31 = self.classifier(self.conv19(x30))
return x31
class Adaptation(nn.Module):
def __init__(self):
super(Adaptation,self).__init__()
self.pool1 = nn.MaxPool2d(kernel_size=2)
self.conv_ad1 = nn.Conv2d(512,512,kernel_size=3)
self.bn_ad1 = nn.BatchNorm2d(512)
self.conv_ad2 = nn.Conv2d(512,256,kernel_size=3)
self.bn_ad2 = nn.BatchNorm2d(256)
self.conv_ad3 = nn.Conv2d(256,256,kernel_size=3)
self.bn_ad3 = nn.BatchNorm2d(256)
self.conv_ad4 = nn.Conv2d(256,1024,kernel_size=3,padding=1)
self.bn_ad4 = nn.BatchNorm2d(1024)
self.conv_ad5 = nn.Conv2d(1024,1,kernel_size=1)
self.bn_ad5 = nn.BatchNorm2d(1)
self.classifier = nn.Sigmoid()
self.relu = nn.ReLU()
def forward(self,x,grl_lambda=1):
x_ad0 = GradientReversalFn.apply(x,grl_lambda)
x_ad1 = self.pool1(self.relu(self.bn_ad1(self.conv_ad1(x_ad0))))
x_ad2 = self.pool1(self.relu(self.bn_ad2(self.conv_ad2(x_ad1))))
x_ad3 = self.pool1(self.relu(self.bn_ad3(self.conv_ad3(x_ad2))))
x_ad4 = self.pool1(self.relu(self.bn_ad4(self.conv_ad4(x_ad3))))
x_ad5 = self.classifier((self.conv_ad5(x_ad4)))
return x_ad5
class CountEstimate(nn.Module):
def __init__(self):
super(CountEstimate,self).__init__()
self.downsample = Downsample()
self.upsample = Upsample()
self.adapt = Adaptation()
def forward(self,x,grl_lambda=1):
x2,x5,x8,x11 = self.downsample(x)
x2 = self.upsample(x11,x2,x5,x8)
x3 = self.adapt(x11,grl_lambda)
return x2,x3