-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResNet.py
47 lines (39 loc) · 1.51 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
38
39
40
41
42
43
44
45
46
47
import torch
from torchsummary import summary
class ResidualBlock(torch.nn.Module):
def __init__(self, in_channels, out_channels):
super(ResidualBlock, self).__init__()
self.conv1 = torch.nn.Sequential(
torch.nn.Conv2d(in_channels, out_channels, kernel_size = 3, stride = 1, padding = 1),
torch.nn.BatchNorm2d(out_channels),
torch.nn.ReLU())
self.conv2 = torch.nn.Sequential(
torch.nn.Conv2d(out_channels, out_channels, kernel_size = 3, stride = 1, padding = 1),
torch.nn.BatchNorm2d(out_channels))
self.downsample = torch.nn.Sequential(
torch.nn.Conv2d(in_channels, out_channels, kernel_size=1),
torch.nn.BatchNorm2d(out_channels)
)
self.relu = torch.nn.ReLU()
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.conv2(out)
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class ResNet(torch.nn.Module):
def __init__(self, layers):
super(ResNet, self).__init__()
self.blocks = []
for i in layers:
block = ResidualBlock(i[0], i[1])
self.blocks.append(block)
def forward(self, x):
for i in self.blocks:
x = i.forward(x)
return x
layers = [(3, 64), (64, 128), (128, 256)]
model = ResNet(layers)
summary(model, (3,512,512))