-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSR_DenseNet.py
97 lines (69 loc) · 3.25 KB
/
SR_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
# coding: utf-8
# In[7]:
import torch
import torch.nn as nn
import torch.nn.functional as F
from math import sqrt
import numpy as np
import torch.nn.init as init
def xavier(param):
init.xavier_uniform(param)
class SingleLayer(nn.Module):
def __init__(self, inChannels,growthRate):
super(SingleLayer, self).__init__()
self.conv =nn.Conv2d(inChannels,growthRate,kernel_size=3,padding=1, bias=True)
def forward(self, x):
out = F.relu(self.conv(x))
out = torch.cat((x, out), 1)
return out
class Net(nn.Module):
def __init__(self,growthRate,nDenselayer):
super(Net,self).__init__()
self.conv1 = nn.Conv2d(1,growthRate,kernel_size=3, padding=1,bias=True)
inChannels = growthRate
self.dense1 = self._make_dense(inChannels,growthRate,nDenselayer)
inChannels += nDenselayer*growthRate
self.dense2 = self._make_dense(inChannels,growthRate,nDenselayer)
inChannels += nDenselayer*growthRate
self.dense3 = self._make_dense(inChannels,growthRate,nDenselayer)
inChannels += nDenselayer*growthRate
self.dense4 = self._make_dense(inChannels,growthRate,nDenselayer)
inChannels += nDenselayer*growthRate
self.dense5 = self._make_dense(inChannels,growthRate,nDenselayer)
inChannels += nDenselayer*growthRate
self.dense6 = self._make_dense(inChannels,growthRate,nDenselayer)
inChannels += nDenselayer*growthRate
self.dense7 = self._make_dense(inChannels,growthRate,nDenselayer)
inChannels += nDenselayer*growthRate
self.dense8 = self._make_dense(inChannels,growthRate,nDenselayer)
inChannels += nDenselayer*growthRate
self.Bottleneck = nn.Conv2d(in_channels=inChannels, out_channels=256, kernel_size=1,padding=0, bias=True)
self.convt1 = nn.ConvTranspose2d(in_channels=256, out_channels=256, kernel_size=4, stride=2, padding=1, bias=True)
self.convt2 =nn.ConvTranspose2d(in_channels=256, out_channels=256, kernel_size=4, stride=2, padding=1, bias=True)
self.conv2 =nn.Conv2d(in_channels=256, out_channels=1, kernel_size=3,padding=1, bias=True)
for m in self.modules():
if isinstance(m, nn.Conv2d):
xavier(m.weight.data)
if m.bias is not None:
m.bias.data.zero_()
def _make_dense(self,inChannels,growthRate, nDenselayer):
layers = []
for i in range(int(nDenselayer)):
layers.append(SingleLayer(inChannels,growthRate))
inChannels += growthRate
return nn.Sequential(*layers)
def forward(self,x):
out = F.relu(self.conv1(x))
out = self.dense1(out)
out = self.dense2(out)
out = self.dense3(out)
out = self.dense4(out)
out = self.dense5(out)
out = self.dense6(out)
out = self.dense7(out)
out = self.dense8(out)
out = self.Bottleneck(out)
out = self.convt1(out)
out = self.convt2(out)
HR = self.conv2(out)
return HR