-
Notifications
You must be signed in to change notification settings - Fork 0
/
resnet.py
86 lines (64 loc) · 3.02 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
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
import torch.nn as nn
import math
from torchmeta.modules import MetaModule, MetaConv2d, MetaBatchNorm2d, MetaLinear, MetaSequential
from collections import OrderedDict
def conv3x3(in_planes, out_planes, stride=1):
"3x3 convolution with padding"
return MetaConv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, bias=False)
class BasicBlock(MetaModule):
expansion = 1
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = MetaBatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = MetaBatchNorm2d(planes)
self.downsample = downsample
self.stride = stride
def forward(self, x, params=None):
if params is None:
params = OrderedDict(self.named_parameters())
residual = x
out = self.conv1(x, params=self.get_subdict(params, 'conv1'))
out = self.bn1(out, params=self.get_subdict(params, 'bn1'))
out = self.relu(out)
out = self.conv2(out, params=self.get_subdict(params, 'conv2'))
out = self.bn2(out, params=self.get_subdict(params, 'bn2'))
if self.downsample is not None:
residual = self.downsample(x, params=self.get_subdict(params, 'downsample'))
out += residual
out = self.relu(out)
return out
class Bottleneck(MetaModule):
expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = MetaConv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = MetaBatchNorm2d(planes)
self.conv2 = MetaConv2d(planes, planes, kernel_size=3, stride=stride,
padding=1, bias=False)
self.bn2 = MetaBatchNorm2d(planes)
self.conv3 = MetaConv2d(planes, planes * 4, kernel_size=1, bias=False)
self.bn3 = MetaBatchNorm2d(planes * 4)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x, params=None):
if params is None:
params = OrderedDict(self.named_parameters())
residual = x
out = self.conv1(x, params=self.get_subdict(params, 'conv1'))
out = self.bn1(out, params=self.get_subdict(params, 'bn1'))
out = self.relu(out)
out = self.conv2(out, params=self.get_subdict(params, 'conv2'))
out = self.bn2(out, params=self.get_subdict(params, 'bn2'))
out = self.relu(out)
out = self.conv3(out, params=self.get_subdict(params, 'conv3'))
out = self.bn3(out, params=self.get_subdict(params, 'bn3'))
if self.downsample is not None:
residual = self.downsample(x, params=self.get_subdict(params, 'downsample'))
out += residual
out = self.relu(out)
return out