-
Notifications
You must be signed in to change notification settings - Fork 1
/
basic_layers.py
36 lines (33 loc) · 1.61 KB
/
basic_layers.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
import torch.nn as nn
class ResidualBlock(nn.Module):
def __init__(self, input_channels, output_channels, stride=1):
super(ResidualBlock, self).__init__()
self.input_channels = input_channels
self.output_channels = output_channels
self.stride = stride
self.bn1 = nn.BatchNorm2d(input_channels)
self.relu = nn.ReLU(inplace=True)
#nn.Conv2d(in_channels=in_dim, out_channels=in_dim // 8, kernel_size=1)
self.conv1 = nn.Conv2d(in_channels=input_channels, out_channels=output_channels // 4, kernel_size=1, stride=1, bias = False)
self.bn2 = nn.BatchNorm2d(output_channels // 4)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(in_channels=output_channels // 4, out_channels=output_channels // 4, kernel_size=3, stride=stride, padding = 1, bias = False)
self.bn3 = nn.BatchNorm2d(output_channels // 4)
self.relu = nn.ReLU(inplace=True)
self.conv3 = nn.Conv2d(in_channels=output_channels // 4, out_channels=output_channels, kernel_size=1, stride=1, bias = False)
self.conv4 = nn.Conv2d(input_channels, output_channels , 1, stride, bias = False)
def forward(self, x):
residual = x
out = self.bn1(x)
out1 = self.relu(out)
out = self.conv1(out1)
out = self.bn2(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn3(out)
out = self.relu(out)
out = self.conv3(out)
if (self.input_channels != self.output_channels) or (self.stride !=1 ):
residual = self.conv4(out1)
out += residual
return out