keras中模型的可视化

1、构建基础神经网络

from keras import Sequential
from keras.layers import Dense, Flatten
from keras.layers import Conv2D,MaxPooling2D
from keras.utils.np_utils import to_categorical
from keras.datasets import mnist
from keras.utils import plot_model
import numpy as np

def load_data():
    # (x_train, y_train), (x_test, y_test) = mnist.load_data()
    f = np.load('mnist.npz')
    x_train, y_train = f['x_train'], f['y_train']
    x_test, y_test = f['x_test'], f['y_test']
    print(x_train.shape, y_train.shape)
    print(x_test.shape, y_test.shape)

    x_train = x_train.reshape((-1, 28, 28, 1))
    y_train = to_categorical(y_train, 10)
    x_test = x_test.reshape((-1, 28, 28, 1))
    y_test = to_categorical(y_test, 10)
    return x_train, y_train, x_test, y_test

def net():
    model = Sequential()
    model.add(Conv2D(6,(5,5),strides=(1,1),input_shape=(28,28,1),padding='valid',activation='relu',kernel_initializer='uniform'))
    model.add(MaxPooling2D(pool_size=(2,2)))
    model.add(Conv2D(16,(5,5),strides=(1,1),padding='valid',activation='relu',kernel_initializer='uniform'))
    model.add(MaxPooling2D(pool_size=(2,2)))
    model.add(Flatten())
    model.add(Dense(120,activation='relu'))
    model.add(Dense(84,activation='relu'))
    model.add(Dense(10,activation='softmax'))
    return model

def train(model, x_train, y_train, x_test, y_test):
    model.compile(optimizer='sgd',loss='categorical_crossentropy',metrics=['accuracy'])
    model.summary()
    model.fit(x_train, y_train, batch_size=100, epochs=20, validation_split=0.2, shuffle=True)
    model.save('LeNet-5_model.h5')
    loss, accuracy=model.evaluate(x_test, y_test,batch_size=100)
    print(loss, accuracy)

说明:mnist数据集可从如下地址下载: https://s3.amazonaws.com/img-datasets/mnist.npz

训练模型的代码示例如下:

if __name__ == '__main__':
    x_train, y_train, x_test, y_test = load_data()
    model = net()
    train(model, x_train, y_train, x_test, y_test)

2、网络结构

2.1、网络结构基本参数显示

if __name__ == '__main__':
    # x_train, y_train, x_test, y_test = load_data()
    model = net()
    model.summary()

网络层输出信息如下:

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 24, 24, 6)         156       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 12, 12, 6)         0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 8, 8, 16)          2416      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 4, 4, 16)          0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 256)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 120)               30840     
_________________________________________________________________
dense_2 (Dense)              (None, 84)                10164     
_________________________________________________________________
dense_3 (Dense)              (None, 10)                850       
=================================================================
Total params: 44,426
Trainable params: 44,426
Non-trainable params: 0
_________________________________________________________________

2.2、网络结构可视化

if __name__ == '__main__':
    # x_train, y_train, x_test, y_test = load_data()
    model = net()
    #model.summary()
    plot_model(model, to_file='plot_mnist.png',show_shapes=True)

注意:画出网络结构图,需要安装graphviz、pydot等包,如若是基于window平台,则需要手动安装graphviz

安装包地址:http://www.graphviz.org/  然后需要设置环境变量:'C:/Program Files (x86)/Graphviz2.38/bin'

如果还不行,则需要加入如下两行代码:

import os
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz2.38/bin'

3、网络特征可视化

# -*- coding:utf-8 -*-
from keras import backend as K
from keras.models import load_model
from keras.datasets import mnist
import numpy as np

import matplotlib.pyplot as plt
#matplotlib 3.*版本可能会报错, 请降版本,本人为2.2.4
#(x_train, y_train), (x_test, y_test) = mnist.load_data()
f = np.load('mnist.npz')
x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
# print(x_train.shape, y_train.shape)
# print(x_test.shape, y_test.shape)

#查看图片
fig1, ax1 = plt.subplots(figsize=(4,4))
img = np.reshape(x_test[10], (28, 28))
ax1.imshow(img)
plt.show()

model = load_model('LeNet-5_model.h5')
#随机选取index=10的图片
img =np.reshape(x_test[10], (-1,28, 28,1))

#可视化第一个MaxPooling2D
layer_1 = K.function([model.layers[0].input], [model.layers[1].output])
f1 = layer_1([img])[0]  #输出是(1,12,12,6)
re = np.transpose(f1, (0,3,1,2))
for i in range(6):
    plt.subplot(2,3,i+1)
    plt.imshow(re[0][i]) #,cmap='gray'
plt.show()

#可视化第二个MaxPooling2D
layer_2 = K.function([model.layers[0].input], [model.layers[3].output])
f2 = layer_2([img])[0]  #输出是(1,4,4,16)
re = np.transpose(f2, (0,3,1,2))
for i in range(16):
    plt.subplot(4,4,i+1)
    plt.imshow(re[0][i]) #cmap='gray'
plt.show()

#可视化第二个MaxPooling2D
layer_2 = K.function([model.layers[0].input], [model.layers[3].output])
f2 = layer_2([img])[0]  #输出是(1,4,4,16)
re = np.transpose(f2, (0,3,1,2))
for i in range(16):
    plt.subplot(4,4,i+1)
    plt.imshow(re[0][i]) #cmap='gray'
plt.show()

原图如下:

可视化第一个MaxPooling2D图样如下:

可视化第二个MaxPooling2D图样如下:

4、Kernel的可视化

待续

 

参考链接:

https://blog.csdn.net/lwy_520/article/details/81479486

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值