-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathblock.py
87 lines (65 loc) · 3.07 KB
/
block.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
import torch
import torch.nn as nn
###############################################################################
# redefine conv layer
###############################################################################
def redefine_conv3x3(in_planes, out_planes, stride=1):
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, bias=False)
def redefine_conv1x1(in_planes, out_planes, stride=1):
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride,
padding=0, bias=False)
##############################################################################
# The implementation of Basic Block for reference
###############################################################################
class BasicBlock(nn.Module):
expansion = 1
def __init__(self, in_planes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = redefine_conv3x3(in_planes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = redefine_conv3x3(planes, planes * BasicBlock.expansion)
self.bn2 = nn.BatchNorm2d(planes * BasicBlock.expansion)
self.downsample = downsample
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
##############################################################################
# Implementation of Bottleneck Block
###############################################################################
class Bottleneck(nn.Module):
expansion = 4
def __init__(self, in_planes, planes, stride=1, downsample=None):
super(Bottleneck, self).__init__()
##############################################################
# TODO: Please define your layers with the BottleNeck from the paper "Deep Residual Learning for Image Recognition"
#
# Note: You **must not** use the nn.Conv2d here but use **redefine_conv3x3** and **redefine_conv1x1** in this script instead
##############################################################
pass
###############################################################
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
def forward(self, x):
residual = x
##############################################################
# TODO: Please write the forward function with your defined layers
##############################################################
out = x # you can delete this line if it's not needed
pass
###############################################################
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out