2017-11-30 05:45:19 +00:00
|
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
2017-08-16 12:24:29 +00:00
|
|
|
|
# sub-parts of the U-Net model
|
|
|
|
|
|
|
|
|
|
import torch
|
|
|
|
|
import torch.nn as nn
|
|
|
|
|
import torch.nn.functional as F
|
|
|
|
|
|
2017-08-17 19:16:19 +00:00
|
|
|
|
|
2017-08-16 12:24:29 +00:00
|
|
|
|
class double_conv(nn.Module):
|
2017-11-30 05:45:19 +00:00
|
|
|
|
'''(conv => BN => ReLU) * 2'''
|
2017-08-16 12:24:29 +00:00
|
|
|
|
def __init__(self, in_ch, out_ch):
|
|
|
|
|
super(double_conv, self).__init__()
|
|
|
|
|
self.conv = nn.Sequential(
|
|
|
|
|
nn.Conv2d(in_ch, out_ch, 3, padding=1),
|
2017-08-19 08:59:51 +00:00
|
|
|
|
nn.BatchNorm2d(out_ch),
|
|
|
|
|
nn.ReLU(inplace=True),
|
2017-08-16 12:24:29 +00:00
|
|
|
|
nn.Conv2d(out_ch, out_ch, 3, padding=1),
|
2017-08-19 08:59:51 +00:00
|
|
|
|
nn.BatchNorm2d(out_ch),
|
|
|
|
|
nn.ReLU(inplace=True)
|
2017-08-16 12:24:29 +00:00
|
|
|
|
)
|
2017-08-17 19:16:19 +00:00
|
|
|
|
|
2017-08-16 12:24:29 +00:00
|
|
|
|
def forward(self, x):
|
|
|
|
|
x = self.conv(x)
|
|
|
|
|
return x
|
|
|
|
|
|
2017-08-17 19:16:19 +00:00
|
|
|
|
|
2017-08-16 12:24:29 +00:00
|
|
|
|
class inconv(nn.Module):
|
|
|
|
|
def __init__(self, in_ch, out_ch):
|
|
|
|
|
super(inconv, self).__init__()
|
|
|
|
|
self.conv = double_conv(in_ch, out_ch)
|
|
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
|
x = self.conv(x)
|
|
|
|
|
return x
|
|
|
|
|
|
2017-08-17 19:16:19 +00:00
|
|
|
|
|
2017-08-16 12:24:29 +00:00
|
|
|
|
class down(nn.Module):
|
|
|
|
|
def __init__(self, in_ch, out_ch):
|
|
|
|
|
super(down, self).__init__()
|
|
|
|
|
self.mpconv = nn.Sequential(
|
|
|
|
|
nn.MaxPool2d(2),
|
|
|
|
|
double_conv(in_ch, out_ch)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
|
x = self.mpconv(x)
|
|
|
|
|
return x
|
|
|
|
|
|
2017-08-17 19:16:19 +00:00
|
|
|
|
|
2017-08-16 12:24:29 +00:00
|
|
|
|
class up(nn.Module):
|
2017-11-30 05:45:19 +00:00
|
|
|
|
def __init__(self, in_ch, out_ch, bilinear=True):
|
2017-08-16 12:24:29 +00:00
|
|
|
|
super(up, self).__init__()
|
2017-11-30 05:45:19 +00:00
|
|
|
|
|
|
|
|
|
# would be a nice idea if the upsampling could be learned too,
|
|
|
|
|
# but my machine do not have enough memory to handle all those weights
|
|
|
|
|
if bilinear:
|
|
|
|
|
self.up = nn.UpsamplingBilinear2d(scale_factor=2)
|
|
|
|
|
else:
|
2018-06-07 08:20:35 +00:00
|
|
|
|
self.up = nn.ConvTranspose2d(in_ch//2, in_ch//2, 2, stride=2)
|
2017-11-30 05:45:19 +00:00
|
|
|
|
|
2017-08-16 12:24:29 +00:00
|
|
|
|
self.conv = double_conv(in_ch, out_ch)
|
|
|
|
|
|
|
|
|
|
def forward(self, x1, x2):
|
|
|
|
|
x1 = self.up(x1)
|
|
|
|
|
diffX = x1.size()[2] - x2.size()[2]
|
|
|
|
|
diffY = x1.size()[3] - x2.size()[3]
|
|
|
|
|
x2 = F.pad(x2, (diffX // 2, int(diffX / 2),
|
|
|
|
|
diffY // 2, int(diffY / 2)))
|
|
|
|
|
x = torch.cat([x2, x1], dim=1)
|
|
|
|
|
x = self.conv(x)
|
|
|
|
|
return x
|
|
|
|
|
|
2017-08-17 19:16:19 +00:00
|
|
|
|
|
2017-08-16 12:24:29 +00:00
|
|
|
|
class outconv(nn.Module):
|
|
|
|
|
def __init__(self, in_ch, out_ch):
|
|
|
|
|
super(outconv, self).__init__()
|
|
|
|
|
self.conv = nn.Conv2d(in_ch, out_ch, 1)
|
|
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
|
x = self.conv(x)
|
|
|
|
|
return x
|