-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconv_net.py
121 lines (114 loc) · 5.96 KB
/
conv_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
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
import torch.nn as nn
import torch
from copy import deepcopy
import torch.nn.functional as F
# Abstraction for using nonlinearities
class Nonlinearity(torch.nn.Module):
def __init__(self):
super(Nonlinearity, self).__init__()
def forward(self, x):
#return F.selu(x)
#return F.relu(x)
#return F.leaky_relu(x)
#return x + torch.sin(10*x)/5
#return x + torch.sin(x)
#return x + torch.sin(x) / 2
#return x + torch.sin(4*x) / 2
return torch.cos(x) - x
#return x * F.sigmoid(x)
#return torch.exp(x)#x**2
#return x - .1*torch.sin(5*x)
# Sample U-Net
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
size = 64
k = 2
b = False
self.first = nn.Conv2d(3, size, 3, stride=1, padding=1, bias=b)
self.downsample = nn.Sequential(nn.Conv2d(size, size, 3,
padding=1, stride=k,
bias=b),
Nonlinearity(),
nn.Conv2d(size, size, 3,
padding=1, stride=k,
bias=b),
Nonlinearity(),
nn.Conv2d(size, size, 3,
padding=1, stride=k,
bias=b),
Nonlinearity(),
nn.Conv2d(size, size, 3,
padding=1, stride=k,
bias=b),
Nonlinearity(),
nn.Conv2d(size, size, 3,
padding=1, stride=k,
bias=b),
Nonlinearity(),
nn.Conv2d(size, size, 3,
padding=1, stride=k,
bias=b),
Nonlinearity())
self.upsample = nn.Sequential(nn.Conv2d(size, size, 3,
padding=1, stride=1,
bias=b),
Nonlinearity(),
nn.Conv2d(size, size, 3,
padding=1, stride=1,
bias=b),
Nonlinearity(),
nn.Upsample(scale_factor=2,
mode='bilinear',
align_corners=True),
nn.Conv2d(size, size, 3,
padding=1, stride=1,
bias=True),
Nonlinearity(),
nn.Upsample(scale_factor=2,
mode='bilinear',
align_corners=True),
nn.Conv2d(size, size, 3,
padding=1, stride=1,
bias=b),
Nonlinearity(),
nn.Upsample(scale_factor=2,
mode='bilinear',
align_corners=True),
nn.Conv2d(size, size, 3,
padding=1, stride=1,
bias=b),
Nonlinearity(),
nn.Upsample(scale_factor=2,
mode='bilinear',
align_corners=True),
nn.Conv2d(size, size, 3,
padding=1, stride=1,
bias=b),
Nonlinearity(),
nn.Upsample(scale_factor=2,
mode='bilinear',
align_corners=True),
nn.Conv2d(size, size, 3,
padding=1, stride=1,
bias=b),
Nonlinearity(),
nn.Upsample(scale_factor=2,
mode='bilinear',
align_corners=True),
nn.Conv2d(size, size, 3,
padding=1, stride=1,
bias=b),
Nonlinearity(),
nn.Conv2d(size, size, 3,
padding=1, stride=1,
bias=b),
Nonlinearity(),
nn.Conv2d(size, 3, 3,
padding=1, stride=1,
bias=b))
def forward(self, x):
o = self.first(x)
o = self.downsample(o)
o = self.upsample(o)
return o