-
Notifications
You must be signed in to change notification settings - Fork 28
/
densenet.py
144 lines (126 loc) · 7.02 KB
/
densenet.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 2 11:20:33 2019
@author: Shusil Dangi
References:
https://github.com/ShusilDangi/DenseUNet-K
It is a simplied version of DenseNet with U-NET architecture.
2D implementation
"""
import torch
import math
import torch.nn as nn
import torch.nn.functional as F
class DenseNet2D_down_block(nn.Module):
def __init__(self,input_channels,output_channels,down_size,dropout=False,prob=0):
super(DenseNet2D_down_block, self).__init__()
self.conv1 = nn.Conv2d(input_channels,output_channels,kernel_size=(3,3),padding=(1,1))
self.conv21 = nn.Conv2d(input_channels+output_channels,output_channels,kernel_size=(1,1),padding=(0,0))
self.conv22 = nn.Conv2d(output_channels,output_channels,kernel_size=(3,3),padding=(1,1))
self.conv31 = nn.Conv2d(input_channels+2*output_channels,output_channels,kernel_size=(1,1),padding=(0,0))
self.conv32 = nn.Conv2d(output_channels,output_channels,kernel_size=(3,3),padding=(1,1))
self.max_pool = nn.AvgPool2d(kernel_size=down_size)
self.relu = nn.LeakyReLU()
self.down_size = down_size
self.dropout = dropout
self.dropout1 = nn.Dropout(p=prob)
self.dropout2 = nn.Dropout(p=prob)
self.dropout3 = nn.Dropout(p=prob)
self.bn = torch.nn.BatchNorm2d(num_features=output_channels)
def forward(self, x):
if self.down_size != None:
x = self.max_pool(x)
if self.dropout:
x1 = self.relu(self.dropout1(self.conv1(x)))
x21 = torch.cat((x,x1),dim=1)
x22 = self.relu(self.dropout2(self.conv22(self.conv21(x21))))
x31 = torch.cat((x21,x22),dim=1)
out = self.relu(self.dropout3(self.conv32(self.conv31(x31))))
else:
x1 = self.relu(self.conv1(x))
x21 = torch.cat((x,x1),dim=1)
x22 = self.relu(self.conv22(self.conv21(x21)))
x31 = torch.cat((x21,x22),dim=1)
out = self.relu(self.conv32(self.conv31(x31)))
return self.bn(out)
class DenseNet2D_up_block_concat(nn.Module):
def __init__(self,skip_channels,input_channels,output_channels,up_stride,dropout=False,prob=0):
super(DenseNet2D_up_block_concat, self).__init__()
self.conv11 = nn.Conv2d(skip_channels+input_channels,output_channels,kernel_size=(1,1),padding=(0,0))
self.conv12 = nn.Conv2d(output_channels,output_channels,kernel_size=(3,3),padding=(1,1))
self.conv21 = nn.Conv2d(skip_channels+input_channels+output_channels,output_channels,
kernel_size=(1,1),padding=(0,0))
self.conv22 = nn.Conv2d(output_channels,output_channels,kernel_size=(3,3),padding=(1,1))
self.relu = nn.LeakyReLU()
self.up_stride = up_stride
self.dropout = dropout
self.dropout1 = nn.Dropout(p=prob)
self.dropout2 = nn.Dropout(p=prob)
def forward(self,prev_feature_map,x):
x = nn.functional.interpolate(x,scale_factor=self.up_stride,mode='nearest')
x = torch.cat((x,prev_feature_map),dim=1)
if self.dropout:
x1 = self.relu(self.dropout1(self.conv12(self.conv11(x))))
x21 = torch.cat((x,x1),dim=1)
out = self.relu(self.dropout2(self.conv22(self.conv21(x21))))
else:
x1 = self.relu(self.conv12(self.conv11(x)))
x21 = torch.cat((x,x1),dim=1)
out = self.relu(self.conv22(self.conv21(x21)))
return out
class DenseNet2D(nn.Module):
def __init__(self,in_channels=1,out_channels=4,channel_size=32,concat=True,dropout=False,prob=0):
super(DenseNet2D, self).__init__()
self.down_block1 = DenseNet2D_down_block(input_channels=in_channels,output_channels=channel_size,
down_size=None,dropout=dropout,prob=prob)
self.down_block2 = DenseNet2D_down_block(input_channels=channel_size,output_channels=channel_size,
down_size=(2,2),dropout=dropout,prob=prob)
self.down_block3 = DenseNet2D_down_block(input_channels=channel_size,output_channels=channel_size,
down_size=(2,2),dropout=dropout,prob=prob)
self.down_block4 = DenseNet2D_down_block(input_channels=channel_size,output_channels=channel_size,
down_size=(2,2),dropout=dropout,prob=prob)
self.down_block5 = DenseNet2D_down_block(input_channels=channel_size,output_channels=channel_size,
down_size=(2,2),dropout=dropout,prob=prob)
self.up_block1 = DenseNet2D_up_block_concat(skip_channels=channel_size,input_channels=channel_size,
output_channels=channel_size,up_stride=(2,2),dropout=dropout,prob=prob)
self.up_block2 = DenseNet2D_up_block_concat(skip_channels=channel_size,input_channels=channel_size,
output_channels=channel_size,up_stride=(2,2),dropout=dropout,prob=prob)
self.up_block3 = DenseNet2D_up_block_concat(skip_channels=channel_size,input_channels=channel_size,
output_channels=channel_size,up_stride=(2,2),dropout=dropout,prob=prob)
self.up_block4 = DenseNet2D_up_block_concat(skip_channels=channel_size,input_channels=channel_size,
output_channels=channel_size,up_stride=(2,2),dropout=dropout,prob=prob)
self.out_conv1 = nn.Conv2d(in_channels=channel_size,out_channels=out_channels,kernel_size=1,padding=0)
self.concat = concat
self.dropout = dropout
self.dropout1 = nn.Dropout(p=prob)
self._initialize_weights()
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
n = m.weight.size(1)
m.weight.data.normal_(0, 0.01)
m.bias.data.zero_()
def forward(self,x):
self.x1 = self.down_block1(x)
self.x2 = self.down_block2(self.x1)
self.x3 = self.down_block3(self.x2)
self.x4 = self.down_block4(self.x3)
self.x5 = self.down_block5(self.x4)
self.x6 = self.up_block1(self.x4,self.x5)
self.x7 = self.up_block2(self.x3,self.x6)
self.x8 = self.up_block3(self.x2,self.x7)
self.x9 = self.up_block4(self.x1,self.x8)
if self.dropout:
out = self.out_conv1(self.dropout1(self.x9))
else:
out = self.out_conv1(self.x9)
return out