Caffe2 - Python API
A deep learning, cross platform ML framework
squeezenet.py
1 import math
2 import torch
3 import torch.nn as nn
4 import torch.nn.init as init
5 
6 
7 class Fire(nn.Module):
8 
9  def __init__(self, inplanes, squeeze_planes,
10  expand1x1_planes, expand3x3_planes):
11  super(Fire, self).__init__()
12  self.inplanes = inplanes
13  self.squeeze = nn.Conv2d(inplanes, squeeze_planes, kernel_size=1)
14  self.squeeze_activation = nn.ReLU(inplace=True)
15  self.expand1x1 = nn.Conv2d(squeeze_planes, expand1x1_planes,
16  kernel_size=1)
17  self.expand1x1_activation = nn.ReLU(inplace=True)
18  self.expand3x3 = nn.Conv2d(squeeze_planes, expand3x3_planes,
19  kernel_size=3, padding=1)
20  self.expand3x3_activation = nn.ReLU(inplace=True)
21 
22  def forward(self, x):
23  x = self.squeeze_activation(self.squeeze(x))
24  return torch.cat([
25  self.expand1x1_activation(self.expand1x1(x)),
26  self.expand3x3_activation(self.expand3x3(x))
27  ], 1)
28 
29 
30 class SqueezeNet(nn.Module):
31 
32  def __init__(self, version=1.0, num_classes=1000, ceil_mode=False):
33  super(SqueezeNet, self).__init__()
34  if version not in [1.0, 1.1]:
35  raise ValueError("Unsupported SqueezeNet version {version}:"
36  "1.0 or 1.1 expected".format(version=version))
37  self.num_classes = num_classes
38  if version == 1.0:
39  self.features = nn.Sequential(
40  nn.Conv2d(3, 96, kernel_size=7, stride=2),
41  nn.ReLU(inplace=True),
42  nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=ceil_mode),
43  Fire(96, 16, 64, 64),
44  Fire(128, 16, 64, 64),
45  Fire(128, 32, 128, 128),
46  nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=ceil_mode),
47  Fire(256, 32, 128, 128),
48  Fire(256, 48, 192, 192),
49  Fire(384, 48, 192, 192),
50  Fire(384, 64, 256, 256),
51  nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=ceil_mode),
52  Fire(512, 64, 256, 256),
53  )
54  else:
55  self.features = nn.Sequential(
56  nn.Conv2d(3, 64, kernel_size=3, stride=2),
57  nn.ReLU(inplace=True),
58  nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=ceil_mode),
59  Fire(64, 16, 64, 64),
60  Fire(128, 16, 64, 64),
61  nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=ceil_mode),
62  Fire(128, 32, 128, 128),
63  Fire(256, 32, 128, 128),
64  nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=ceil_mode),
65  Fire(256, 48, 192, 192),
66  Fire(384, 48, 192, 192),
67  Fire(384, 64, 256, 256),
68  Fire(512, 64, 256, 256),
69  )
70  # Final convolution is initialized differently from the rest
71  final_conv = nn.Conv2d(512, self.num_classes, kernel_size=1)
72  self.classifier = nn.Sequential(
73  nn.Dropout(p=0.5),
74  final_conv,
75  nn.ReLU(inplace=True),
76  nn.AvgPool2d(13)
77  )
78 
79  for m in self.modules():
80  if isinstance(m, nn.Conv2d):
81  if m is final_conv:
82  init.normal_(m.weight.data, mean=0.0, std=0.01)
83  else:
84  init.kaiming_uniform_(m.weight.data)
85  if m.bias is not None:
86  m.bias.data.zero_()
87 
88  def forward(self, x):
89  x = self.features(x)
90  x = self.classifier(x)
91  return x.view(x.size(0), self.num_classes)