|
|
import torch
|
|
|
import torch.nn as nn
|
|
|
import torch.nn.functional as F
|
|
|
|
|
|
|
|
|
def passthrough(x, **kwargs):
|
|
|
return x
|
|
|
|
|
|
def ELUCons(elu, nchan):
|
|
|
if elu:
|
|
|
return nn.ELU(inplace=True)
|
|
|
else:
|
|
|
return nn.PReLU(nchan)
|
|
|
|
|
|
|
|
|
|
|
|
class ContBatchNorm3d(nn.modules.batchnorm._BatchNorm):
|
|
|
def _check_input_dim(self, input):
|
|
|
if input.dim() != 5:
|
|
|
raise ValueError('expected 5D input (got {}D input)'
|
|
|
.format(input.dim()))
|
|
|
super(ContBatchNorm3d, self)._check_input_dim(input)
|
|
|
|
|
|
def forward(self, input):
|
|
|
self._check_input_dim(input)
|
|
|
return F.batch_norm(
|
|
|
input, self.running_mean, self.running_var, self.weight, self.bias,
|
|
|
True, self.momentum, self.eps)
|
|
|
|
|
|
|
|
|
class LUConv(nn.Module):
|
|
|
def __init__(self, nchan, elu):
|
|
|
super(LUConv, self).__init__()
|
|
|
self.relu1 = ELUCons(elu, nchan)
|
|
|
self.conv1 = nn.Conv3d(nchan, nchan, kernel_size=5, padding=2)
|
|
|
self.bn1 = nn.BatchNorm3d(nchan)
|
|
|
|
|
|
def forward(self, x):
|
|
|
out = self.relu1(self.bn1(self.conv1(x)))
|
|
|
return out
|
|
|
|
|
|
|
|
|
def _make_nConv(nchan, depth, elu):
|
|
|
layers = []
|
|
|
for _ in range(depth):
|
|
|
layers.append(LUConv(nchan, elu))
|
|
|
return nn.Sequential(*layers)
|
|
|
|
|
|
|
|
|
class InputTransition(nn.Module):
|
|
|
def __init__(self, outChans, elu):
|
|
|
super(InputTransition, self).__init__()
|
|
|
self.conv1 = nn.Conv3d(1, 16, kernel_size=5, padding=2)
|
|
|
self.bn1 = nn.BatchNorm3d(16)
|
|
|
self.relu1 = ELUCons(elu, 16)
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
|
|
out=self.conv1(x)
|
|
|
out = self.bn1(out)
|
|
|
|
|
|
x16 = torch.cat((x, x, x, x, x, x, x, x,
|
|
|
x, x, x, x, x, x, x, x), 1)
|
|
|
out = self.relu1(torch.add(out, x16))
|
|
|
return out
|
|
|
|
|
|
|
|
|
class DownTransition(nn.Module):
|
|
|
def __init__(self, inChans, nConvs, elu, dropout=False):
|
|
|
super(DownTransition, self).__init__()
|
|
|
outChans = 2*inChans
|
|
|
self.down_conv = nn.Conv3d(inChans, outChans, kernel_size=2, stride=2)
|
|
|
self.bn1 = nn.BatchNorm3d(outChans)
|
|
|
self.do1 = passthrough
|
|
|
self.relu1 = ELUCons(elu, outChans)
|
|
|
self.relu2 = ELUCons(elu, outChans)
|
|
|
if dropout:
|
|
|
self.do1 = nn.Dropout3d()
|
|
|
self.ops = _make_nConv(outChans, nConvs, elu)
|
|
|
|
|
|
def forward(self, x):
|
|
|
down = self.relu1(self.bn1(self.down_conv(x)))
|
|
|
out = self.do1(down)
|
|
|
out = self.ops(out)
|
|
|
out = self.relu2(torch.add(out, down))
|
|
|
return out
|
|
|
|
|
|
|
|
|
class UpTransition(nn.Module):
|
|
|
def __init__(self, inChans, outChans, nConvs, elu, dropout=False):
|
|
|
super(UpTransition, self).__init__()
|
|
|
self.up_conv = nn.ConvTranspose3d(inChans, outChans // 2, kernel_size=2, stride=2)
|
|
|
self.bn1 = nn.BatchNorm3d(outChans // 2)
|
|
|
self.do1 = passthrough
|
|
|
self.do2 = nn.Dropout3d()
|
|
|
self.relu1 = ELUCons(elu, outChans // 2)
|
|
|
self.relu2 = ELUCons(elu, outChans)
|
|
|
if dropout:
|
|
|
self.do1 = nn.Dropout3d()
|
|
|
self.ops = _make_nConv(outChans, nConvs, elu)
|
|
|
|
|
|
def forward(self, x, skipx):
|
|
|
out = self.do1(x)
|
|
|
skipxdo = self.do2(skipx)
|
|
|
out = self.relu1(self.bn1(self.up_conv(out)))
|
|
|
xcat = torch.cat((out, skipxdo), 1)
|
|
|
out = self.ops(xcat)
|
|
|
out = self.relu2(torch.add(out, xcat))
|
|
|
return out
|
|
|
|
|
|
|
|
|
class OutputTransition(nn.Module):
|
|
|
def __init__(self, inChans, elu, nll):
|
|
|
super(OutputTransition, self).__init__()
|
|
|
self.conv1 = nn.Conv3d(inChans, 2, kernel_size=5, padding=2)
|
|
|
self.bn1 = nn.BatchNorm3d(2)
|
|
|
self.conv2 = nn.Conv3d(2, 2, kernel_size=1)
|
|
|
self.relu1 = ELUCons(elu, 2)
|
|
|
if nll:
|
|
|
self.softmax = F.log_softmax
|
|
|
else:
|
|
|
self.softmax = F.softmax
|
|
|
|
|
|
def forward(self, x):
|
|
|
|
|
|
out = self.relu1(self.bn1(self.conv1(x)))
|
|
|
out = self.conv2(out)
|
|
|
print(out.shape)
|
|
|
|
|
|
out = out.permute(0, 2, 3, 4, 1).contiguous()
|
|
|
|
|
|
out = out.view(out.numel() // 2, 2)
|
|
|
out = self.softmax(out)
|
|
|
|
|
|
return out
|
|
|
class AdaptivePoolTransition(nn.Module):
|
|
|
def __init__(self,in_channels,out_channels):
|
|
|
super().__init__()
|
|
|
self.pool=nn.AdaptiveAvgPool3d(1)
|
|
|
self.ops=nn.Sequential(
|
|
|
nn.Linear(in_channels,in_channels//2),
|
|
|
ELUCons(elu=True,nchan=0),
|
|
|
nn.Linear(in_channels // 2,out_channels),
|
|
|
nn.Sigmoid()
|
|
|
)
|
|
|
def forward(self,x):
|
|
|
B,C,D,H,W=x.shape
|
|
|
out=self.pool(x).view(B,C)
|
|
|
return self.ops(out)
|
|
|
|
|
|
class HalfVNet(nn.Module):
|
|
|
|
|
|
|
|
|
def __init__(self, elu=True, nll=False):
|
|
|
super(HalfVNet, self).__init__()
|
|
|
self.in_tr = InputTransition(16, elu)
|
|
|
self.down_tr32 = DownTransition(16, 1, elu)
|
|
|
self.down_tr64 = DownTransition(32, 2, elu)
|
|
|
self.down_tr128 = DownTransition(64, 3, elu, dropout=True)
|
|
|
self.down_tr256 = DownTransition(128, 2, elu, dropout=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.pool=AdaptivePoolTransition(256,6)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def forward(self, x):
|
|
|
out16 = self.in_tr(x)
|
|
|
out32 = self.down_tr32(out16)
|
|
|
out64 = self.down_tr64(out32)
|
|
|
out128 = self.down_tr128(out64)
|
|
|
out256 = self.down_tr256(out128)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out=self.pool(out256)
|
|
|
return out
|
|
|
if __name__ == '__main__':
|
|
|
device='cuda' if torch.cuda.is_available() else 'cpu'
|
|
|
print(device)
|
|
|
net=HalfVNet().to(device)
|
|
|
image=torch.randn((1,1,128,320,320)).to(device)
|
|
|
with torch.no_grad():
|
|
|
out=net(image)
|
|
|
print(out.shape)
|
|
|
|
|
|
|
|
|
|
|
|
|