MCPcopy Create free account
hub / github.com/Sirwenhao/Deep-Learning-Notes / AlexNet

Class AlexNet

CV/Pytorch_classification/AlexNet/model.py:5–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3
4
5class AlexNet(nn.Module):
6 def __init__(self, num_classes = 1000, init_weights = False):
7 super(AlexNet, self).__init__()
8 self.features = nn.Sequential(
9 nn.Conv2d(3, 48, kernel_size = 11, stride = 4, padding = 2), #padding也可以是tuple:(1,2),
10 # 1表示上下方各补一行0,2代表左右两侧各补2列0
11 nn.ReLU(inplace = True),
12 nn.MaxPool2d(kernel_size = 3, stride = 2),
13 nn.Conv2d(48, 128, kernel_size = 5, padding = 2),
14 nn.ReLU(inplace = True),
15 nn.MaxPool2d(kernel_size = 3, stride = 2),
16 nn.Conv2d(128, 192, kernel_size = 3, padding = 1),
17 nn.ReLU(inplace = True),
18 nn.Conv2d(192, 192, kernel_size = 3, padding = 1),
19 nn.ReLU(inplace = True),
20 nn.Conv2d(192, 128, kernel_size = 3, padding = 1),
21 nn.ReLU(inplace = True),
22 nn.MaxPool2d(kernel_size = 3, stride = 2),
23 )
24 self.classifier = nn.Sequential(
25 nn.Dropout(p = 0.5),
26 nn.Linear(128 * 6 * 6, 2048),
27 nn.ReLU(inplace = True),
28 nn.Dropout(p = 0.5),
29 nn.Linear(2048, 2048),
30 nn.ReLU(inplace = True),
31 nn.Linear(2048, num_classes),
32 )
33 if init_weights:
34 self._initialize_weights()
35
36 def forward(self, x):
37 x = self.features(x)
38 x = torch.flatten(x, start_dim = 1)
39 x = self.classifier(x)
40 return x
41
42 def _initialize_weights(self):
43 for m in self.modules():
44 if isinstance(m, nn.Conv2d):
45 nn.init.kaiming_normal_(m.weight, mode = 'fan_out', nonlinearity = 'relu')
46 if m.bias is not None:
47 nn.init.constant_(m.bias, 0)
48 elif isinstance(m, nn.Linear):
49 nn.init.normal_(m.weight, 0, 0.01)
50 nn.init.constant_(m.bias, 0)

Callers 2

predict.pyFile · 0.90
train.pyFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected