from keras.layers import Input, Conv2D, MaxPool2D, Flatten, Dense, Activation
from keras import regularizers
from keras.models import Model
import keras.activations as activation
from keras.utils import plot_model
import os
'''
原味Lenet,没有bn,dropout之类的处理
2020229
chenglong
'''
class Lenet():
def __init__(self,data_size=(100,32,32,3),
n_classes=10):
# 5
if len(data_size) == 4:
self.data_size = data_size
else:
raise TypeError
self.n_classes = n_classes
self.decay_weight = 0.5e-3
@property
def data_shape(self):
return self.data_size[1:]
def build(self):
data_input=Input(batch_shape=self.data_size)
net =Conv2D(6,(5,5),strides=(1,1),
padding="valid",activation="sigmoid",
kernel_regularizer=regularizers.l2(self.decay_weight),
name="c1")(data_input)
# net = BatchNormalization()
net = MaxPool2D((2,2),strides=(2,2),name="s2")(net)
net = Conv2D(16,(5,5),strides=(1,1),
activation="relu",padding="same",
kernel_regularizer=regularizers.l2(self.decay_weight),
name="c3")(net)
net = MaxPool2D((2,2),strides=(2,2),name="s4")(net)
net = Flatten()(net)
net = Dense(120,name="f5",activation="sigmoid")(net)
net = Dense(84,activation="sigmoid",name="f6")(net)
net = Dense(self.n_classes, activation="softmax")(net)
net = Activation('softmax')(net)
model = Model(inputs=data_input,outputs=net)
plot_model(model, to_file=os.path.join('./', "base_model.png"), show_shapes=True)
model.summary()
return net
model = Lenet()
model.build()
去官网,Graphviz,下载。
然后一直next就行了。然后把’D:\Program Files (x86)\Graphviz2.38\bin‘添加到环境变量的path。
然后用everything找的pydot.py文件,然后把文件中的self.prog = 'dot'修改为self.prog = 'dot.exe'就行了。
然后运行,这个图不是上面代码的结果。
关于环境变量问题,需要重启电脑
pip install graphviz
pip install torchviz
import torch
import torch.nn as nn
from torchviz import make_dot
from torch.autograd import Variable
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5)
self.mp1 = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(6, 16, 5)
self.mp2 = nn.MaxPool2d(2, 2)
self.conv3 = nn.Conv2d(400, 120, 1, 1)
self.fc4 = nn.Linear(120, 84)
self.fc5 = nn.Linear(84, 10)
def forward(self, x):
x = self.conv1(x)
x = self.mp1(x)
x = self.conv2(x)
x = self.mp2(x)
x = x.view(-1, x.size()[3] * x.size()[2] * x.size()[1])
x = torch.unsqueeze(x, dim=-1)
x = torch.unsqueeze(x, dim=-1)
x = self.conv3(x) # conv代替fc?
x = x.view(-1, x.size()[3] * x.size()[2] * x.size()[1])
x = self.fc4(x)
return self.fc5(x)
if __name__ == '__main__':
a = torch.randn(1, 1, 32, 32)
m = LeNet()
y = m(a)
vis_graph = make_dot(y.mean(), params=dict(m.named_parameters()))
vis_graph.view()
# print(m)
和keras输出结果不一样