Skip to content

Commit 3d8f1ed

Browse files
committed
alexnet sppnet zfnet实现 以及readme
1 parent 4ef017a commit 3d8f1ed

File tree

5 files changed

+278
-0
lines changed

5 files changed

+278
-0
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
### 一、介绍
4+
本项目为本人研习 李玉鑑,张婷,单传辉,刘兆英等 所著的 《深度学习-卷积神经网络从入门到精通》(2018年7月第1版第1次印刷),使用pytorch实现的案例。
5+
6+
7+
### 二、学习建议
8+
* 理解什么是卷积(好像没有书写了这个,哈哈。另注:我是看完前面的讲解,再看果程C
9+
的回答理解的)。 [传送门](https://www.zhihu.com/question/22298352/answer/228543288)
10+
* 跳过书中第二章 《预备知识》,改阅读或者参考 由 [] 涌井良幸 涌井贞美 著 杨瑞龙 译的 《深度学习的数学》。
11+
* 神经元科普视频: [传送门](https://b23.tv/av65013444)
12+
13+
14+
### 三、遇到的问题
15+
* 实现过程中发现书的有部分笔误,或者讲述不清的地方。
16+
* 训练数据下载链接失效,或者测试数据残缺。(代码目前我只提供了module,数据准备的代码已经实现,等验证后补上。)
17+
18+
### 四、学习交流
19+
微信: yyguzi (请标注 cnn)
20+

alexnet/module.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#-*-coding:utf-8-*-
2+
3+
import torch
4+
import torch.nn as nn
5+
import torch.nn.functional as F
6+
import torchvision
7+
from PIL import Image
8+
import numpy as np
9+
10+
class AlexNet(nn.Module):
11+
12+
def __init__(self):
13+
super(AlexNet, self).__init__()
14+
15+
self.conv1 = nn.Conv2d(3, 96, (11, 11), stride=4)
16+
self.conv2 = nn.Conv2d(96, 256,(5,5),stride=1 )
17+
self.conv3 = nn.Conv2d(256, 384,(3,3),stride=1 )
18+
self.conv4 = nn.Conv2d(384, 384, (3,3),stride=1 )
19+
self.conv5 = nn.Conv2d(384, 256, (3,3),stride=1 )
20+
21+
self.fc1 = nn.Linear(256 * 2 * 2, 4096)
22+
self.fc2 = nn.Linear(4096, 4096)
23+
self.fc3 = nn.Linear(4096, 1000)
24+
25+
def forward(self, x):
26+
# 第一层
27+
x = F.local_response_norm(F.max_pool2d(F.relu( self.conv1(x)), kernel_size=3,stride=2), 4,alpha=0.001/9.0,beta=0.75)
28+
# 第二层
29+
x = F.local_response_norm(F.max_pool2d(F.relu( self.conv2(x)), kernel_size=3,stride=2), 4,alpha=0.001/9.0,beta=0.75)
30+
# 第三层
31+
x = F.relu(self.conv3(x))
32+
# 第四层
33+
x = F.relu(self.conv4(x))
34+
# 第五层
35+
x = F.max_pool2d( F.relu(self.conv5(x)), (3,3) ,stride = 2)
36+
#数据转化成一维向量
37+
x = x.view(x.size(0), -1 )
38+
# 第六层
39+
x = F.dropout(F.relu(self.fc1(x)), p= 0.5)
40+
# 第七层
41+
x = F.dropout(F.relu(self.fc2(x)), p= 0.5)
42+
# 第八层
43+
x = F.softmax(self.fc3(x))
44+
45+
46+
print(x.size())
47+
return x
48+
49+
50+
if __name__ == "__main__":
51+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
52+
img = Image.open('../test.JPEG')
53+
54+
img = img.resize((227, 227),Image.ANTIALIAS).convert('RGB')
55+
np_data = np.array(img)
56+
57+
net = AlexNet().to(device)
58+
input = torch.from_numpy(np_data).transpose(2,0).unsqueeze(0).float()
59+
60+
net(input)

sppnet/moudle.py

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# -*-coding:utf-8-*
2+
3+
import torch
4+
import torch.nn as nn
5+
import torch.nn.functional as F
6+
import torchvision
7+
from PIL import Image
8+
import numpy as np
9+
import math
10+
from functools import reduce
11+
12+
def spatial_pyramid_pool(input, out_size):
13+
num_sample, channel, w, h = input.size()
14+
15+
for i, tmp_size in enumerate(out_size):
16+
w_wid = int(math.ceil(w / tmp_size))
17+
h_wid = int(math.ceil(h / tmp_size))
18+
19+
w_pad = int(math.ceil(w_wid * tmp_size - w + 1)/2)
20+
h_pad = int(math.ceil(h_wid * tmp_size - h + 1)/2)
21+
22+
maxpool = nn.MaxPool2d((w_wid, h_wid), stride=(w_wid, h_wid), padding=(w_pad, h_pad))
23+
24+
x = maxpool(input)
25+
if(i == 0):
26+
spp = x.view(num_sample, -1)
27+
else:
28+
spp = torch.cat((spp, x.view(num_sample, -1) ), 1)
29+
return spp
30+
31+
class SPPNet(nn.Module):
32+
33+
def __init__(self):
34+
super(SPPNet, self).__init__()
35+
self.output_num = (4,2,1)
36+
37+
#第一层
38+
self.layer1 = nn.Sequential(
39+
nn.Conv2d(3, 96, (7,7), stride=2), # 227 * 227 * 3 -> 110 * 110* 96
40+
nn.ReLU(), #原书缺失此步骤
41+
nn.MaxPool2d((3,3), 2, padding= 1) , # 110 * 110* 96 -> 55 * 55 * 96
42+
nn.LocalResponseNorm(5) #原书缺失此步骤
43+
)
44+
45+
#第二层
46+
self.layer2 = nn.Sequential(
47+
nn.Conv2d(96, 256, (5, 5), stride=2), # 55 * 55 * 96 -> 26 * 26 * 256
48+
nn.ReLU(), #原书缺失此步骤
49+
nn.MaxPool2d((3,3), 2, padding= 1), # 26 * 26 * 256 -> 13 * 13 * 256
50+
nn.LocalResponseNorm(5) #原书缺失此步骤
51+
)
52+
53+
self.layer3 = nn.Sequential(
54+
nn.Conv2d(256, 384, (3, 3), stride= 1, padding=1), # 13 * 13 * 256 -> 13 * 13 * 384
55+
nn.ReLU(), #原书缺失此步骤
56+
)
57+
58+
self.layer4 = nn.Sequential(
59+
nn.Conv2d(384, 384, (3, 3), stride= 1 , padding=1), # 13 * 13 * 384 -> 13 * 13 * 384
60+
nn.ReLU(), #原书缺失此步骤
61+
)
62+
63+
self.layer5 = nn.Sequential(
64+
nn.Conv2d(384, 256, (3, 3), stride= 1, padding=1), # 13 * 13 * 384 -> 13 * 13 * 256
65+
nn.ReLU(), #原书缺失此步骤
66+
nn.MaxPool2d((3, 3), 2) # 13 * 13 * 256 -> 6*6*256
67+
)
68+
69+
# self.layer6 = nn.Sequential(
70+
# nn.Linear(6*6*256, 4096),
71+
# nn.ReLU(), #原书缺失此步骤
72+
# )
73+
74+
self.layer7 = nn.Sequential(
75+
nn.Linear(reduce(lambda x,y: x + y * y * 256, self.output_num, 0), 4096),
76+
nn.ReLU(), #原书缺失此步骤
77+
)
78+
79+
self.layer8 = nn.Sequential(
80+
nn.Linear(4096, 1000),
81+
nn.Softmax(dim=1)
82+
)
83+
84+
def forward(self, x):
85+
x = self.layer1(x)
86+
x = self.layer2(x)
87+
x = self.layer3(x)
88+
x = self.layer4(x)
89+
x = self.layer5(x)
90+
x = spatial_pyramid_pool(x, self.output_num)
91+
92+
x = x.view(x.size(0), -1)
93+
x = self.layer7(x)
94+
x = self.layer8(x)
95+
return x
96+
97+
98+
99+
if __name__ == "__main__":
100+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
101+
img = Image.open('../test.JPEG')
102+
103+
#img = img.resize((227, 227),Image.ANTIALIAS).convert('RGB')
104+
img = img .convert('RGB')
105+
np_data = np.array(img)
106+
107+
net = SPPNet().to(device)
108+
input = torch.from_numpy(np_data).transpose(2,0).unsqueeze(0).float()
109+
110+
net(input)

test.JPEG

33.1 KB
Loading

zfnet/moudle.py

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#-*-coding:utf-8-*
2+
3+
import torch
4+
import torch.nn as nn
5+
import torch.nn.functional as F
6+
import torchvision
7+
from PIL import Image
8+
import numpy as np
9+
10+
# 参考https://github.com/amir-saniyan/ZFNet/blob/master/zfnet.py
11+
12+
class ZFNet(nn.Module):
13+
14+
def __init__(self):
15+
super(ZFNet, self).__init__()
16+
17+
#第一层
18+
self.layer1 = nn.Sequential(
19+
nn.Conv2d(3, 96, (7,7), stride=2), # 227 * 227 * 3 -> 110 * 110* 96
20+
nn.ReLU(), #原书缺失此步骤
21+
nn.MaxPool2d((3,3), 2, padding= 1) , # 110 * 110* 96 -> 55 * 55 * 96
22+
nn.LocalResponseNorm(5) #原书缺失此步骤
23+
)
24+
25+
#第二层
26+
self.layer2 = nn.Sequential(
27+
nn.Conv2d(96, 256, (5, 5), stride=2), # 55 * 55 * 96 -> 26 * 26 * 256
28+
nn.ReLU(), #原书缺失此步骤
29+
nn.MaxPool2d((3,3), 2, padding= 1), # 26 * 26 * 256 -> 13 * 13 * 256
30+
nn.LocalResponseNorm(5) #原书缺失此步骤
31+
)
32+
33+
self.layer3 = nn.Sequential(
34+
nn.Conv2d(256, 384, (3, 3), stride= 1, padding=1), # 13 * 13 * 256 -> 13 * 13 * 384
35+
nn.ReLU(), #原书缺失此步骤
36+
)
37+
38+
self.layer4 = nn.Sequential(
39+
nn.Conv2d(384, 384, (3, 3), stride= 1 , padding=1), # 13 * 13 * 384 -> 13 * 13 * 384
40+
nn.ReLU(), #原书缺失此步骤
41+
)
42+
43+
self.layer5 = nn.Sequential(
44+
nn.Conv2d(384, 256, (3, 3), stride= 1, padding=1), # 13 * 13 * 384 -> 13 * 13 * 256
45+
nn.ReLU(), #原书缺失此步骤
46+
nn.MaxPool2d((3, 3), 2) # 13 * 13 * 256 -> 6*6*256
47+
)
48+
49+
self.layer6 = nn.Sequential(
50+
nn.Linear(6*6*256, 4096),
51+
nn.ReLU(), #原书缺失此步骤
52+
)
53+
54+
self.layer7 = nn.Sequential(
55+
nn.Linear(4096, 4096),
56+
nn.ReLU(), #原书缺失此步骤
57+
)
58+
59+
self.layer8 = nn.Sequential(
60+
nn.Linear(4096, 1000),
61+
nn.Softmax(dim=1)
62+
)
63+
64+
def forward(self, x):
65+
x = self.layer1(x)
66+
x = self.layer2(x)
67+
x = self.layer3(x)
68+
x = self.layer4(x)
69+
x = self.layer5(x)
70+
x = x.view(x.size(0), -1)
71+
x = self.layer6(x)
72+
x = self.layer7(x)
73+
x = self.layer8(x)
74+
return x
75+
76+
77+
78+
if __name__ == "__main__":
79+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
80+
img = Image.open('../test.JPEG')
81+
82+
img = img.resize((227, 227),Image.ANTIALIAS).convert('RGB')
83+
np_data = np.array(img)
84+
85+
net = ZFNet().to(device)
86+
input = torch.from_numpy(np_data).transpose(2,0).unsqueeze(0).float()
87+
88+
net(input)

0 commit comments

Comments
 (0)