-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocks.py
94 lines (82 loc) · 3.27 KB
/
blocks.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import numpy as np
import torch
import torch.nn as nn
from torchvision import datasets
from torchvision import transforms
from torch.utils.data.sampler import SubsetRandomSampler
##################
# blocks.py
#
# Definition of residual blocks used to construct the network
# Modified from an existing
#
##################
# Code based on implementation from https://blog.paperspace.com/writing-resnet-from-scratch-in-pytorch/
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1, downsample = None):
super(ResidualBlock, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1),
nn.BatchNorm2d(out_channels),
nn.ReLU())
self.conv2 = nn.Sequential(
nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(out_channels))
self.downsample = downsample
self.relu = nn.ReLU()
self.out_channels = out_channels
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.conv2(out)
if self.downsample:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
# Code based on implementation at https://blog.paperspace.com/writing-resnet-from-scratch-in-pytorch/
class ResNet(nn.Module):
def __init__(self, in_channels, block, layers, num_classes=10):
super(ResNet, self).__init__()
self.inplanes = 64
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels, 64, kernel_size=7, stride=1, padding=3),
nn.BatchNorm2d(64),
nn.ReLU())
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=1, padding=1)
self.layer0 = self._make_layer(block, 64, layers[0], stride=1)
self.layer1 = self._make_layer(block, 128, layers[1], stride=1)
self.layer2 = self._make_layer(block, 256, layers[2], stride=1)
self.layer3 = self._make_layer(block, 512, layers[3], stride=1)
self.avgpool = nn.AvgPool2d(1, stride=1, )
self.collapse = nn.Conv2d(512, 1, 1, 1) # Collapse down to single channel
# We will not do FC layer since we want a 2D output
# self.fc = nn.Linear(512, num_classes)
def _make_layer(self, block, planes, blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes, kernel_size=1, stride=stride),
nn.BatchNorm2d(planes),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes
for i in range(1, blocks):
layers.append(block(self.inplanes, planes))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
# Disable pooling
# x = self.maxpool(x)
x = self.layer0(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
# Disable 2D pooling
# x = self.avgpool(x)
# Final 1x1 convolution
x = self.collapse(x).squeeze(1)
# We will not do FC layer since we want a 2D output
# x = self.fc(x)
return x