-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocks.py
93 lines (78 loc) · 3.03 KB
/
blocks.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
import torch
import torch.nn as nn
class ConvBlock(nn.Module):
def __init__(self, input_size, output_size, kernel_size, stride=1, padding=0, activation='relu', norm=None):
super(ConvBlock, self).__init__()
self.conv = nn.Conv2d(input_size, output_size, kernel_size, stride=stride, padding=padding)
self.norm = norm
if self.norm == 'batch':
self.bn = torch.nn.BatchNorm2d(output_size)
self.activation = activation
if self.activation == 'relu':
self.act = nn.ReLU()
elif self.activation == 'relu6':
self.act = nn.ReLU6()
elif self.activation == 'lrelu':
self.act = nn.LeakyReLU()
elif self.activation == 'prelu':
self.act = nn.PReLU()
elif self.activation == 'elu':
self.act = nn.ELU()
elif self.activation == 'selu':
self.act = nn.SELU()
elif self.activation == 'tanh':
self.act = torch.nn.Tanh()
elif self.activation == 'sigmoid':
self.act = torch.nn.Sigmoid()
elif self.activation == 'logsigmoid':
self.act = torch.nn.LogSigmoid()
def forward(self, x):
if self.norm is not None:
out = self.bn(self.conv1(x))
else:
out = self.conv(x)
if self.activation is not None:
return self.act(out)
else:
return out
class ResnetBlock(torch.nn.Module):
def __init__(self, num_filter, kernel_size=3, stride=1, padding=1, activation='relu', norm='batch'):
super(ResnetBlock, self).__init__()
self.conv1 = torch.nn.Conv2d(num_filter, num_filter, kernel_size, stride, padding)
self.conv2 = torch.nn.Conv2d(num_filter, num_filter, kernel_size, stride, padding)
self.norm = norm
if self.norm == 'batch':
self.bn = torch.nn.BatchNorm2d(num_filter)
self.activation = activation
if self.activation == 'relu':
self.act = nn.ReLU()
elif self.activation == 'relu6':
self.act = nn.ReLU6()
elif self.activation == 'lrelu':
self.act = nn.LeakyReLU()
elif self.activation == 'prelu':
self.act = nn.PReLU()
elif self.activation == 'elu':
self.act = nn.ELU()
elif self.activation == 'selu':
self.act = nn.SELU()
elif self.activation == 'tanh':
self.act = torch.nn.Tanh()
elif self.activation == 'sigmoid':
self.act = torch.nn.Sigmoid()
elif self.activation == 'logsigmoid':
self.act = torch.nn.LogSigmoid()
def forward(self, x):
residual = x
if self.norm is not None:
out = self.bn(self.conv1(x))
else:
out = self.conv1(x)
if self.activation is not None:
out = self.act(out)
if self.norm is not None:
out = self.bn(self.conv2(out))
else:
out = self.conv2(out)
out = torch.add(out, residual)
return out