-
Notifications
You must be signed in to change notification settings - Fork 0
/
unet2.py
135 lines (104 loc) · 3.65 KB
/
unet2.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
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
class UNetDec(nn.Module):
def __init__(self, in_channels, features, out_channels):
super(UNetDec,self).__init__()
self.up = nn.Sequential(
nn.Conv2d(in_channels, features, 3),
nn.ReLU(inplace=True),
nn.Conv2d(features, features, 3),
nn.ReLU(inplace=True),
nn.ConvTranspose2d(features, out_channels, 2),
# nn.ReLU(inplace=True),
)
def forward(self, x):
return self.up(x)
class UNetEnc(nn.Module):
def __init__(self, in_channels, out_channels, dropout=False):
super(UNetEnc,self).__init__()
layers = [
nn.Conv2d(in_channels, out_channels, 3),
nn.ReLU(inplace=True),
nn.Conv2d(out_channels, out_channels, 3),
nn.ReLU(inplace=True),
]
if dropout:
layers += [nn.Dropout(.5)]
layers += [nn.MaxPool2d(2, stride=2, ceil_mode=True)]
self.down = nn.Sequential(*layers)
def forward(self, x):
return self.down(x)
class UNet(nn.Module):
def __init__(self, num_classes):
super(UNet,self).__init__()
self.enc1 = UNetEnc(1, 64)
self.enc2 = UNetEnc(64, 128)
self.enc3 = UNetEnc(128, 256)
self.center = nn.Sequential(
nn.Conv2d(256, 512, 3),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, 3),
nn.ReLU(inplace=True),
nn.Dropout(),
nn.ConvTranspose2d(512, 256, 2),
# nn.ReLU(inplace=True),
)
self.dec3 = UNetDec(512, 256, 128)
self.dec2 = UNetDec(256, 128, 64)
self.dec1 = nn.Sequential(
nn.Conv2d(128, 192, 3),
nn.ReLU(inplace=True),
nn.Conv2d(192, 64, 3),
nn.ReLU(inplace=True),
)
self.final = nn.Conv2d(64, num_classes, 1)
self.recon = nn.Conv2d(num_classes,1,1)
self.drr=None
self.reconstruc=None
self.loss=None
def forward(self, x,a=None,b=None,c=None):
enc1 = self.enc1(x)
enc2 = self.enc2(enc1)
enc3 = self.enc3(enc2)
center = self.center(enc3)
dec3 = self.dec3(torch.cat([
center, F.upsample_bilinear(enc3, center.size()[2:])], 1))
dec2 = self.dec2(torch.cat([
dec3, F.upsample_bilinear(enc2, dec3.size()[2:])], 1))
dec1 = self.dec1(torch.cat([
dec2, F.upsample_bilinear(enc1, dec2.size()[2:])], 1))
self.drr= F.upsample_bilinear(self.final(dec1), x.size()[2:])
self.reconstruc = self.recon(self.drr)
if self.training:
self.loss= self.build_loss(x,a,b,c)
return self.reconstruc,self.drr
#
def build_loss(self,x,a,b,c):
drr1,drr2,drr3=torch.split(self.drr,1,1)
l2loss = nn.MSELoss()
# l1loss = torch.abs(drr1 - a)+torch.abs(drr2 - b)+torch.abs(drr3 - c)
output_l2_loss = l2loss(drr1, a)+l2loss(drr2, b)+l2loss(drr3, c)
l1loss= nn.L1Loss()
output_l1_loss = l1loss(drr1, a) + l1loss(drr2, b) + l1loss(drr3, c)
loss_decom=output_l1_loss*0.2+output_l2_loss*0.8
loss_recon=l2loss(self.reconstruc,x)
loss = loss_decom*0.5+loss_recon*0.5
return loss
#
#
# if __name__ == "__main__":
# """
# testing
# """
# model = UNet(3)
# print model
# x = Variable(torch.FloatTensor(np.random.random(( 1,1, 320, 320))))
# recon,drr = model(x,x,x,x)
# print drr.size()
# loss = torch.sum(drr)
# print loss
# loss.backward()