-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
130 lines (109 loc) · 4.38 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
import sys
import hpconfig as cfg
import pathconfig
sys.path.append(pathconfig.sys_path)
from fastai.conv_learner import *
from data import get_model_data
# import lovasz_losses as L
# Instantiate model
def get_learner(arch='resnet34'):
# Instantiate fastai learner
md = get_model_data()
if(arch == 'resnet34'):
f = resnet34
cut,lr_cut = model_meta[f]
layers = cut_model(f(True), cut)
m_base = nn.Sequential(*layers)
m = to_gpu(Unet34(m_base, arch='resnet34'))
elif(arch == 'densenet121'):
f = dn121
cut,lr_cut = model_meta[f]
layers = cut_model(f(True), cut)
m_base = nn.Sequential(*layers)[0]
m = to_gpu(Unet34(m_base, arch='densenet121'))
models = UnetModel(m, lr_cut=lr_cut)
learn = ConvLearner(md, models)
learn.opt_fn=optim.Adam
# learn.crit=L.lovasz_hinge
learn.crit=nn.BCEWithLogitsLoss()
learn.metrics=[accuracy_thresh(0.5),dice]
return learn
# Similar to IoU metric
def dice(pred, targs):
pred = (pred>0).float()
return 2. * (pred*targs).sum() / (pred+targs).sum()
# Save activations from contracting path to concatenate to expansive path in Unet
class SaveFeatures():
features=None
def __init__(self, m): self.hook = m.register_forward_hook(self.hook_fn)
def hook_fn(self, module, input, output): self.features = output
def remove(self): self.hook.remove()
# Neural net module for expansive path in Unet
class UnetBlock(nn.Module):
def __init__(self, up_in, x_in, n_out):
super().__init__()
up_out = x_out = n_out//2
self.x_conv = nn.Conv2d(x_in, x_out, 1)
self.tr_conv = nn.ConvTranspose2d(up_in, up_out, 2, stride=2)
self.bn = nn.BatchNorm2d(n_out)
def forward(self, up_p, x_p):
up_p = self.tr_conv(up_p) # Expansive part
x_p = self.x_conv(x_p) # Further convolution on the activations from contracting part
cat_p = torch.cat([up_p,x_p], dim=1)
return self.bn(F.relu(cat_p))
# Expansive path of Unet
class Unet34(nn.Module):
def __init__(self, rn, arch='resnet34', p = cfg.p):
super().__init__()
# Number of channels (filters) at key layers
rn34_ch = [(512, 256), (256, 128), (256, 64), (256, 64)]
dn121_ch = [(1024, 1024), (256, 512), (256, 256), (256, 64)]
# Layer groups to save features of
rn34_l = [2,4,5,6]
dn121_l = [2,4,6,8]
self.rn = rn # Resnet base
if(arch == 'resnet34'):
ch = rn34_ch
ls = rn34_l
elif(arch == 'densenet121'):
ch = dn121_ch
ls = dn121_l
self.sfs = [SaveFeatures(rn[i]) for i in ls] # Saved activations from contracting/resnet part
# self.sfs = [SaveFeatures(rn[i]) for i in [2,5,12,22]] # for VGG16
# self.up1 = UnetBlock(ch[0][0],ch[0][1],256)
self.up1 = UnetBlock(ch[0][0], ch[0][1], cfg.kernels[0])
self.drop1 = nn.Dropout2d(p[0])
# self.up2 = UnetBlock(ch[1][0],ch[1][1],256)
self.up2 = UnetBlock(cfg.kernels[0], ch[1][1], cfg.kernels[1])
self.drop2 = nn.Dropout2d(p[1])
# self.up3 = UnetBlock(ch[2][0],ch[2][1],256)
self.up3 = UnetBlock(cfg.kernels[1], ch[2][1], cfg.kernels[2])
self.drop3 = nn.Dropout2d(p[2])
# self.up4 = UnetBlock(ch[3][0],ch[3][1],256)
self.up4 = UnetBlock(cfg.kernels[2], ch[3][1], cfg.kernels[3])
self.drop4 = nn.Dropout2d(p[3])
# self.up1 = UnetBlock(512,256,256)
# self.up2 = UnetBlock(256,128,256)
# self.up3 = UnetBlock(256,64,256)
# self.up4 = UnetBlock(256,64,256)
self.up5 = nn.ConvTranspose2d(cfg.kernels[3], 1, 2, stride=2)
def forward(self,x):
x = F.relu(self.rn(x))
x = self.up1(x, self.sfs[3].features)
x = self.drop1(x)
x = self.up2(x, self.sfs[2].features)
x = self.drop2(x)
x = self.up3(x, self.sfs[1].features)
x = self.drop3(x)
x = self.up4(x, self.sfs[0].features)
x = self.drop4(x)
x = self.up5(x)
return x[:,0]
def close(self):
for sf in self.sfs: sf.remove()
class UnetModel():
def __init__(self, model, lr_cut, name='unet'):
self.model,self.name,self.lr_cut = model,name, lr_cut
def get_layer_groups(self, precompute):
lgs = list(split_by_idxs(children(self.model.rn), [self.lr_cut]))
return lgs + [children(self.model)[1:]]