-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresnet_block.py
60 lines (46 loc) · 1.96 KB
/
resnet_block.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class Resnet_block(nn.Module):
def __init__(self, nb_filts, kernels, strides, first=False, downsample=False):
super(Resnet_block, self).__init__()
self.first = first
self.downsample = downsample
if not self.first:
self.bn1 = nn.BatchNorm2d(num_features=nb_filts[0])
self.lrelu = nn.LeakyReLU(negative_slope=0.3)
self.conv1 = nn.Conv2d(in_channels=nb_filts[0],
out_channels=nb_filts[1],
kernel_size=kernels,
padding=(1, 3),
stride=strides)
self.bn2 = nn.BatchNorm2d(num_features=nb_filts[1])
self.conv2 = nn.Conv1d(in_channels=nb_filts[1],
out_channels=nb_filts[1],
padding=(1, 3),
kernel_size=kernels,
stride=1)
if downsample:
self.conv_downsample = nn.Conv2d(in_channels=nb_filts[0],
out_channels=nb_filts[1],
padding=(1, 3),
kernel_size=kernels,
stride=strides)
# self.bn_downsample = nn.BatchNorm2d(num_features = nb_filts[2])
def forward(self, x):
identity = x
if not self.first:
out = self.bn1(x)
out = self.lrelu(out)
else:
out = x
out = self.conv1(out)
out = self.bn2(out)
out = self.lrelu(out)
out = self.conv2(out)
if self.downsample:
identity = self.conv_downsample(identity)
# identity = self.bn_downsample(identity)
out += identity
# print(identity.size())
return out