Skip to content

Commit a4ff8d9

Browse files
committed
up
1 parent 1b32f60 commit a4ff8d9

File tree

10 files changed

+1350
-3
lines changed

10 files changed

+1350
-3
lines changed

README.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,45 @@ To eval(uate) the trained model use:
106106

107107

108108

109-
That's it.
109+
That's it.
110+
111+
112+
113+
## Bonus - AlexNet (Anno 2012) - From 61,706 Weights to 61,100,840
114+
115+
116+
The award-winning AlexNet is basically a LeNet5 scaled up 1000x and
117+
introduces relu activation, dropout layers, and more to the world (of deep neural networks).
118+
The summary of the model reads:
119+
120+
121+
```
122+
----------------------------------------------------------------
123+
Layer (type) Output Shape Param #
124+
================================================================
125+
Conv2d-1 [-1, 64, 55, 55] 23,296
126+
ReLU-2 [-1, 64, 55, 55] 0
127+
MaxPool2d-3 [-1, 64, 27, 27] 0
128+
Conv2d-4 [-1, 192, 27, 27] 307,392
129+
ReLU-5 [-1, 192, 27, 27] 0
130+
MaxPool2d-6 [-1, 192, 13, 13] 0
131+
Conv2d-7 [-1, 384, 13, 13] 663,936
132+
ReLU-8 [-1, 384, 13, 13] 0
133+
Conv2d-9 [-1, 256, 13, 13] 884,992
134+
ReLU-10 [-1, 256, 13, 13] 0
135+
Conv2d-11 [-1, 256, 13, 13] 590,080
136+
ReLU-12 [-1, 256, 13, 13] 0
137+
MaxPool2d-13 [-1, 256, 6, 6] 0
138+
AdaptiveAvgPool2d-14 [-1, 256, 6, 6] 0
139+
Dropout-15 [-1, 9216] 0
140+
Linear-16 [-1, 4096] 37,752,832
141+
ReLU-17 [-1, 4096] 0
142+
Dropout-18 [-1, 4096] 0
143+
Linear-19 [-1, 4096] 16,781,312
144+
ReLU-20 [-1, 4096] 0
145+
Linear-21 [-1, 1000] 4,097,000
146+
================================================================
147+
Total params: 61,100,840
148+
```
149+
150+

alexnet/SUMMARY.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Summary
2+
3+
to generate - try
4+
5+
$ python ./models.py
6+
7+
resulting in:
8+
9+
## AlexNet input_size=(3, 224, 224)
10+
11+
```
12+
AlexNet(
13+
(features): Sequential(
14+
(0): Conv2d(3, 64, kernel_size=(11, 11), stride=(4, 4), padding=(2, 2))
15+
(1): ReLU(inplace=True)
16+
(2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
17+
(3): Conv2d(64, 192, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
18+
(4): ReLU(inplace=True)
19+
(5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
20+
(6): Conv2d(192, 384, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
21+
(7): ReLU(inplace=True)
22+
(8): Conv2d(384, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
23+
(9): ReLU(inplace=True)
24+
(10): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
25+
(11): ReLU(inplace=True)
26+
(12): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=False)
27+
)
28+
(avgpool): AdaptiveAvgPool2d(output_size=(6, 6))
29+
(classifier): Sequential(
30+
(0): Dropout(p=0.5, inplace=False)
31+
(1): Linear(in_features=9216, out_features=4096, bias=True)
32+
(2): ReLU(inplace=True)
33+
(3): Dropout(p=0.5, inplace=False)
34+
(4): Linear(in_features=4096, out_features=4096, bias=True)
35+
(5): ReLU(inplace=True)
36+
(6): Linear(in_features=4096, out_features=1000, bias=True)
37+
)
38+
)
39+
40+
Total number of trainable model parameters: 61100840
41+
about 231.27 MBs, 237746.46 KBs
42+
43+
----------------------------------------------------------------
44+
Layer (type) Output Shape Param #
45+
================================================================
46+
Conv2d-1 [-1, 64, 55, 55] 23,296
47+
ReLU-2 [-1, 64, 55, 55] 0
48+
MaxPool2d-3 [-1, 64, 27, 27] 0
49+
Conv2d-4 [-1, 192, 27, 27] 307,392
50+
ReLU-5 [-1, 192, 27, 27] 0
51+
MaxPool2d-6 [-1, 192, 13, 13] 0
52+
Conv2d-7 [-1, 384, 13, 13] 663,936
53+
ReLU-8 [-1, 384, 13, 13] 0
54+
Conv2d-9 [-1, 256, 13, 13] 884,992
55+
ReLU-10 [-1, 256, 13, 13] 0
56+
Conv2d-11 [-1, 256, 13, 13] 590,080
57+
ReLU-12 [-1, 256, 13, 13] 0
58+
MaxPool2d-13 [-1, 256, 6, 6] 0
59+
AdaptiveAvgPool2d-14 [-1, 256, 6, 6] 0
60+
Dropout-15 [-1, 9216] 0
61+
Linear-16 [-1, 4096] 37,752,832
62+
ReLU-17 [-1, 4096] 0
63+
Dropout-18 [-1, 4096] 0
64+
Linear-19 [-1, 4096] 16,781,312
65+
ReLU-20 [-1, 4096] 0
66+
Linear-21 [-1, 1000] 4,097,000
67+
================================================================
68+
Total params: 61,100,840
69+
Trainable params: 61,100,840
70+
Non-trainable params: 0
71+
----------------------------------------------------------------
72+
Input size (MB): 0.57
73+
Forward/backward pass size (MB): 8.38
74+
Params size (MB): 233.08
75+
Estimated Total Size (MB): 242.03
76+
----------------------------------------------------------------
77+
```
78+
79+
80+
81+
82+

alexnet/eval.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import torch
2+
from torchvision import transforms
3+
from PIL import Image
4+
5+
6+
### local imports
7+
from model import model
8+
9+
10+
11+
path = './files/coffee.jpg'
12+
# path = './files/cat.jpg'
13+
# path = './files/stephansdom.jpg'
14+
15+
16+
img = Image.open( path )
17+
18+
19+
transform = transforms.Compose([
20+
transforms.Resize(256),
21+
transforms.CenterCrop(224), # 224x224
22+
transforms.ToTensor(),
23+
transforms.Normalize(
24+
mean=[0.485, 0.456, 0.406],
25+
std=[0.229, 0.224, 0.225])])
26+
27+
img_tensor = transform(img)
28+
batch = torch.unsqueeze(img_tensor, dim=0)
29+
30+
31+
model.eval()
32+
y = model(batch)
33+
34+
y_max, index = torch.max(y,dim=1)
35+
36+
with open('./files/imagenet_class_labels.txt') as f:
37+
classes = [line.strip() for line in f.readlines()]
38+
39+
prob = torch.nn.functional.softmax(y, dim=1)[0]
40+
print( classes[index[0]], prob[index[0]].item())
41+
print()
42+
#=> 967: 'espresso', 0.8799548745155334
43+
44+
y_sort, indices = torch.sort(y, descending=True)
45+
for idx in indices[0][:5]:
46+
print(classes[idx], prob[idx].item())
47+
#=> 967: 'espresso', 0.8799548745155334
48+
# 968: 'cup', 0.07688959687948227
49+
# 504: 'coffee mug', 0.038615722209215164
50+
# 925: 'consomme', 0.0035129631869494915
51+
# 960: 'chocolate sauce, chocolate syrup', 0.0005007769796065986
52+
53+
54+
print( "bye" )
55+
56+
57+
"""
58+
try with
59+
60+
path = './files/cat.jpg'
61+
62+
resulting in
63+
64+
283: 'Persian cat', 0.31462812423706055
65+
552: 'feather boa, boa', 0.21569392085075378
66+
285: 'Egyptian cat', 0.17547936737537384
67+
281: 'tabby, tabby cat', 0.03902266174554825
68+
262: 'Brabancon griffon', 0.031412456184625626
69+
70+
or with
71+
72+
path = './files/stephansdom.jpg'
73+
74+
resulting in
75+
76+
497: 'church, church building', 0.4236260652542114
77+
698: 'palace', 0.2292090207338333
78+
663: 'monastery', 0.1675940901041031
79+
442: 'bell cote, bell cot', 0.03244597092270851
80+
483: 'castle', 0.024614153429865837
81+
"""
82+

alexnet/files/cat.jpg

600 KB
Loading

alexnet/files/coffee.jpg

392 KB
Loading

0 commit comments

Comments
 (0)