-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnet.py
95 lines (75 loc) · 3.2 KB
/
net.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
# -*- coding:utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.modules
from collections import OrderedDict
def swish(x):
return x * F.sigmoid(x)
class FeatureExtractor(nn.Module):
def __init__(self, cnn, feature_layer=11):
super(FeatureExtractor, self).__init__()
self.features = nn.Sequential(*list(cnn.features.children())[:(feature_layer + 1)])
def forward(self, x):
return self.features(x)
class residualBlock(nn.Module):
def __init__(self, in_channels=64, k=3, n=64, s=1):
super(residualBlock, self).__init__()
m = OrderedDict()
m['conv1'] = nn.Conv2d(in_channels, n, k, stride=s, padding=1)
m['bn1'] = nn.BatchNorm2d(n)
m['ReLU1'] = nn.ReLU(inplace=True)
m['conv2'] = nn.Conv2d(n, n, k, stride=s, padding=1)
m['bn2'] = nn.BatchNorm2d(n)
self.group1 = nn.Sequential(m)
self.relu = nn.Sequential(nn.ReLU(inplace=True))
def forward(self, x):
out = self.group1(x) + x
out = self.relu(out)
return out
class Generator(nn.Module):
def __init__(self, n_residual_blocks):
super(Generator, self).__init__()
self.n_residual_blocks = n_residual_blocks
#self.upsample_factor = upsample_factor
self.conv1 = nn.Conv2d(6, 64, 9, stride=1, padding=4) #9 4
self.bn1 = nn.BatchNorm2d(64)
self.relu1 = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(64, 64, 3, stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.relu2 = nn.ReLU(inplace=True)
self.conv3 = nn.Conv2d(64, 64, 3, stride=1, padding=1)
self.bn3 = nn.BatchNorm2d(64)
self.relu3 = nn.ReLU(inplace=True)
for i in range(self.n_residual_blocks):
self.add_module('residual_block' + str(i + 1), residualBlock())
self.conv4 = nn.Conv2d(64, 64, 3, stride=1, padding=1)
self.bn4 = nn.BatchNorm2d(64)
self.relu4 = nn.ReLU(inplace=True)
self.conv5 = nn.Conv2d(64, 64, 3, stride=1, padding=1)
self.bn5 = nn.BatchNorm2d(64)
self.relu5 = nn.ReLU(inplace=True)
self.conv6 = nn.Conv2d(64, 2, 3, stride=1, padding=1) #64,2,3 for pair
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = swish(self.relu1(self.bn1(self.conv1(x))))
x = swish(self.relu2(self.bn2(self.conv2(x))))
x = swish(self.relu3(self.bn3(self.conv3(x))))
y = x.clone()
for i in range(self.n_residual_blocks):
y = self.__getattr__('residual_block' + str(i + 1))(y)
x = swish(self.relu4(self.bn4(self.conv4(y))))# + x
x = swish(self.relu5(self.bn5(self.conv5(x))))
return self.sigmoid(self.conv6(x))
def initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
torch.nn.init.xavier_normal_(m.weight.data)
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):
torch.nn.init.normal_(m.weight.data, 0, 0.01)
m.bias.data.zero_()