forked from pulkitag/CS280_SP18_Fallback_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
67 lines (62 loc) · 2.51 KB
/
models.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
import torch
from torch import nn
class MiniAlexNet(nn.Module):
r"""
A variant of AlexNet.
The changes with respect to the original AlexNet are:
- LRN (local response normalization) layers are not included
- The Fully Connected (FC) layers (fc6 and fc7) have smaller dimensions
due to the lower resolution of mini-places images (128x128) compared
with ImageNet images (usually resized to 256x256)
"""
def __init__(self):
super(MiniAlexNet, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(3, 96, kernel_size=11, stride=4),
nn.PReLU(num_parameters=1),
nn.BatchNorm2d(num_features=96),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(96, 256, kernel_size=5, padding=2, groups=2),
nn.PReLU(num_parameters=1),
nn.BatchNorm2d(num_features=256),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(256, 384, kernel_size=3, padding=1),
nn.PReLU(num_parameters=1),
nn.BatchNorm2d(num_features=384),
nn.Conv2d(384, 384, kernel_size=3, padding=1, groups=2),
nn.PReLU(num_parameters=1),
nn.BatchNorm2d(num_features=384),
nn.Conv2d(384, 1024, kernel_size=3, padding=1, groups=2),
nn.PReLU(num_parameters=1),
nn.BatchNorm2d(num_features=1024),
nn.MaxPool2d(kernel_size=3, stride=2),
)
self.fc = nn.Sequential(
nn.Linear(1024, 1024),
nn.PReLU(num_parameters=1),
nn.BatchNorm1d(num_features=1024),
nn.Dropout(p=0.2),
nn.Linear(1024, 1024),
nn.PReLU(num_parameters=1),
nn.BatchNorm1d(num_features=1024),
nn.Dropout(p=0.2),
nn.Linear(1024, 100),
)
self.init_model()
def init_model(self):
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
nn.init.kaiming_normal(m.weight.data)
if m.bias is not None:
m.bias.data.zero_()
elif classname == 'Linear':
nn.init.normal(m.weight.data, std=0.005)
if m.bias is not None:
m.bias.data.zero_()
self.apply(weights_init)
return self
def forward(self, input):
features = self.conv(input)
features = features.view(input.size(0), -1) # flatten to a 2d tensor
return self.fc(features)