resnet.py 1.6 KB
Newer Older
Hang Zhang's avatar
Hang Zhang committed
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
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## Created by: Hang Zhang
## ECE Department, Rutgers University
## Email: zhang.hang@rutgers.edu
## Copyright (c) 2017
##
## This source code is licensed under the MIT-style license found in the
## LICENSE file in the root directory of this source tree 
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

import torch
import torch.nn as nn
import model.mynn as nn2

class Net(nn.Module):
	def __init__(self, num_blocks=[2,2,2,2], num_classes=10, 
								block=nn2.Bottleneck):
		super(Net, self).__init__()
		if block == nn2.Basicblock:
			self.expansion = 1
		else:
			self.expansion = 4

		self.inplanes = 64
		num_planes = [64, 128, 256, 512]
		strides = [1, 2, 2, 2]
		model = []
		# Conv_1
		model += [nn.Conv2d(3, self.inplanes, kernel_size=3, padding=1),
							nn.BatchNorm2d(self.inplanes),
							nn.ReLU(inplace=True)]
		# Residual units
		for i in range(4):
			model += [self._residual_unit(block, num_planes[i], num_blocks[i],
								strides[i])]
		# Last conv layer
		model += [nn.BatchNorm2d(self.inplanes),
							nn.ReLU(inplace=True),
							nn.AvgPool2d(4),
							nn2.View(-1, self.inplanes),
							nn.Linear(self.inplanes, num_classes)]
		self.model = nn.Sequential(*model)
		print(model)

	def _residual_unit(self, block, planes, n_blocks, stride):
		strides = [stride] + [1]*(n_blocks-1)
		layers = []
		for i in range(n_blocks):
			layers += [block(self.inplanes, planes, strides[i])]
			self.inplanes = self.expansion*planes
		return nn.Sequential(*layers)

	def forward(self, input):
		return self.model(input)