-
Notifications
You must be signed in to change notification settings - Fork 12
/
basic_layers.py
40 lines (37 loc) · 1.58 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
37
38
39
40
import torch
import torch.nn as nn
from torch.nn import init
import functools
from torch.autograd import Variable
import numpy as np
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)
self.conv1 = nn.Conv2d(input_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(output_channels//4, 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(output_channels//4, output_channels, kernel_size=1, stride=1, bias = False)
self.conv4 = nn.Conv2d(input_channels, output_channels , kernel_size=1, stride=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