-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resnet.py
37 lines (32 loc) · 1.2 KB
/
Resnet.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
import loader
import torch.nn as nn
import torch.nn.functional as F
device = loader.device
# This Resnet structure is taken from Author's Implementation
class Residual(nn.Module):
def __init__(self, in_channels, out_channels, hidden_channels):
super(Residual, self).__init__()
self.block = nn.Sequential(
nn.Conv2d(in_channels,hidden_channels,kernel_size=3, stride=1, padding=1, bias=False),
nn.ReLU(),
nn.Conv2d(hidden_channels,out_channels,kernel_size=1, stride=1, bias=False),
nn.ReLU(),
)
def forward(self, x):
x = x + self.block(x)
return x
class ResidualBlocks(nn.Module):
def __init__(self, in_channels,out_channels, residual_layers, hidden_channels):
super(ResidualBlocks, self).__init__()
self.residual_layers = residual_layers
self.layers = nn.ModuleList(
[
Residual(in_channels,out_channels,hidden_channels)
for _ in range(self.residual_layers)
]
)
def forward(self, x):
for i in range(self.residual_layers):
x = self.layers[i](x)
x = F.relu(x)
return x