-
Notifications
You must be signed in to change notification settings - Fork 2
/
resnet.py
122 lines (106 loc) · 3.12 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python
# coding: utf-8
#
# Author: Kazuto Nakashima
# URL: https://kazuto1011.github.io
# Created: 2017-11-19
from collections import OrderedDict
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.model_zoo as model_zoo
class _ConvBatchNormReLU(nn.Sequential):
def __init__(
self,
in_channels,
out_channels,
kernel_size,
stride,
padding,
dilation,
relu=True,
):
super(_ConvBatchNormReLU, self).__init__()
self.add_module(
"conv",
nn.Conv2d(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding,
dilation=dilation,
bias=False,
),
)
self.add_module(
"bn",
nn.BatchNorm2d(
num_features=out_channels, eps=1e-5, momentum=0.999, affine=True
),
)
if relu:
self.add_module("relu", nn.ReLU())
def forward(self, x):
return super(_ConvBatchNormReLU, self).forward(x)
class _Bottleneck(nn.Sequential):
"""Bottleneck Unit"""
def __init__(
self, in_channels, mid_channels, out_channels, stride, dilation, downsample
):
super(_Bottleneck, self).__init__()
self.reduce = _ConvBatchNormReLU(in_channels, mid_channels, 1, stride, 0, 1)
self.conv3x3 = _ConvBatchNormReLU(
mid_channels, mid_channels, 3, 1, dilation, dilation
)
self.increase = _ConvBatchNormReLU(
mid_channels, out_channels, 1, 1, 0, 1, relu=False
)
self.downsample = downsample
if self.downsample: