-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathSR_DenseNet_2.py
More file actions
85 lines (61 loc) · 2.85 KB
/
Copy pathSR_DenseNet_2.py
File metadata and controls
85 lines (61 loc) · 2.85 KB
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
# coding: utf-8
# In[16]:
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 SingleBlock(nn.Module):
def __init__(self, inChannels,growthRate,nDenselayer):
super(SingleBlock, self).__init__()
self.block= self._make_dense(inChannels,growthRate, nDenselayer)
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=self.block(x)
return out
class Net(nn.Module):
def __init__(self,inChannels,growthRate,nDenselayer,nBlock):
super(Net,self).__init__()
self.conv1 = nn.Conv2d(1,growthRate,kernel_size=3, padding=1,bias=True)
inChannels = growthRate
self.denseblock = self._make_block(inChannels,growthRate, nDenselayer,nBlock)
inChannels +=growthRate* nDenselayer*nBlock
self.Bottleneck = nn.Conv2d(in_channels=inChannels, out_channels=128, kernel_size=1,padding=0, bias=True)
self.convt1 = nn.ConvTranspose2d(in_channels=128, out_channels=128, kernel_size=4, stride=2, padding=1, bias=True)
self.convt2 =nn.ConvTranspose2d(in_channels=128, out_channels=128, kernel_size=4, stride=2, padding=1, bias=True)
self.conv2 =nn.Conv2d(in_channels=128, 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_block(self, inChannels,growthRate, nDenselayer,nBlock):
blocks =[]
for i in range(int(nBlock)):
blocks.append(SingleBlock(inChannels,growthRate,nDenselayer))
inChannels += growthRate* nDenselayer
return nn.Sequential(* blocks)
def forward(self,x):
out = F.relu(self.conv1(x))
out = self.denseblock(out)
out = self.Bottleneck(out)
out = self.convt1(out)
out = self.convt2(out)
HR = self.conv2(out)
return HR