您的位置:首页 > 理论基础 > 计算机网络

画出卷积神经网络结构图[转载]

2017-02-02 14:10 417 查看
徐海蛟教学
使用Keras框架(后端可选tensorflow或者theano),可以画出卷积神经网络的结构图帮助我们理解或确认自己创立的模型。

问题的关键在于使用
from keras.utils.visualize_util import plot
中的
plot
函数。
但是直接使用会提示缺少
pydot

首先安装
sudo pip3 install pydot
以及
sudo apt-get install graphviz
(在Ubuntu上)。

但是会提示一个和新版keras的兼容问题。于是我们需要安装
sudo pip3 install pydot-ng
来解决这个问题。

现在就可以画出结构图了:

使用样例一

from keras.layers import Input, Convolution2D, Flatten, Dense, Activationfrom keras.models import Sequentialfrom keras.optimizers import SGD , Adamfrom keras.initializations import normalfrom keras.utils.visualize_util import plot# apply a 3x3 convolution with 64 output filters on a 256x256 image:model = Sequential()
model.add(Convolution2D(64, 3, 3, border_mode='same', dim_ordering='th',input_shape=(3, 256, 256)))# now model.output_shape == (None, 64, 256, 256)# add a 3x3 convolution on top, with 32 output filters:model.add(Convolution2D(32, 3, 3, border_mode='same', dim_ordering='th'))# now model.output_shape == (None, 32, 256, 256)adam = Adam(lr=1e-6)
model.compile(loss='mse',optimizer=adam)
print("We finish building the model")

plot(model, to_file='model1.png', show_shapes=True)



样例一

使用样例二

from keras.layers import Input, Convolution2D, MaxPooling2D, Flatten, Dense
from keras.models import Model
from keras.utils.visualize_util import plotinputs = Input(shape=(229, 229, 3))x = Convolution2D(32, 3, 3, subsample=(2, 2), border_mode='valid', dim_ordering='tf')(inputs)x = Flatten()(x)loss = Dense(32, activation='relu', name='loss')(x)model = Model(input=inputs, output=loss)model.compile(optimizer='rmsprop', loss='binary_crossentropy')# visualize model layout with pydot_ngplot(model, to_file='model2.png', show_shapes=True)



样例二

使用样例三

from keras.layers import Input, Convolution2D, Flatten, Dense, Activationfrom keras.models import Sequentialfrom keras.optimizers import SGD , Adamfrom keras.initializations import normalfrom keras.utils.visualize_util import plot

print("Now we build the model")
model = Sequential()
img_channels = 4 #output dimenson nothing with channelsimg_rows = 80img_cols = 80model.add(Convolution2D(32, 8, 8, subsample=(4,4),init=lambda shape, name: normal(shape, scale=0.01, name=name), border_mode='same', dim_ordering='th',input_shape=(img_channels,img_rows,img_cols)))
model.add(Activation('relu'))
model.add(Convolution2D(64, 4, 4, subsample=(2,2),init=lambda shape, name: normal(shape, scale=0.01, name=name), border_mode='same', dim_ordering='th'))
model.add(Activation('relu'))
model.add(Convolution2D(64, 3, 3, subsample=(1,1),init=lambda shape, name: normal(shape, scale=0.01, name=name), border_mode='same', dim_ordering='th'))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(512, init=lambda shape, name: normal(shape, scale=0.01, name=name)))
model.add(Activation('relu'))
model.add(Dense(2,init=lambda shape, name: normal(shape, scale=0.01, name=name)))

adam = Adam(lr=1e-6)
model.compile(loss='mse',optimizer=adam)
print("We finish building the model")

plot(model, to_file='model3.png', show_shapes=True)



model3
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐