Keras Note
Keras测试的精度比训练时返回的精度要高
Keras在测试时,一些规则化方法(如Drop out、L1/L2)被关闭了,且在训练时,权值始终在变化,而测试时keras的权值是训练时最后一次运算的结果,因此测试的精度通常都会比训练时返回的精度要高。
Keras可视化
可视化模型
from keras.utils.visualize_util import plot
plot(model, to_file='model.png')
可视化特征层
# -*- coding: utf-8 -*-
"""
visualizing model or feature maps in convolution layer and maxpooling layer
"""
from keras.models import model_from_json
from keras.utils.visualize_util import plot
import os
import theano
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
MODEL_PATH = '/home/zhou/dl_data/chen-6-27/MachineLearning/cnn/Result/2016-07-12 04:09:21'
MODEL_NAME = 'model.json'
def visualize_model(to_file='model.png'):
model = model_from_json(open(os.path.join(MODEL_PATH, MODEL_NAME)).read())
plot(model, to_file=os.path.join(MODEL_PATH, to_file), show_shapes=True, show_layer_names=True)
def visualize_feature_map(data=None):
"""
可视化特征层:卷积层、池化层
"""
model = model_from_json(open(os.path.join(MODEL_PATH, MODEL_NAME)).read())
input_shape = model.layers[0].input_shape
assert input_shape[1:] == data.shape[1:], "data shape error, it should be {:}".format(input_shape)
layers = model.layers
pre = data.copy()
for i in range(data.shape[0]):
path = os.path.join(MODEL_PATH, "img{:}".format(i+1))
if not os.path.exists(path):
os.mkdir(path)
path = os.path.join(path, 'original.png')
img = np.squeeze(data[i])
plt.imsave(path, img, cmap=cm.gray)
for n, layer in enumerate(layers):
prefix = layer.name.split('_')[0]
if prefix == 'dropout':
continue
func = theano.function([layer.input], layer.output, allow_input_downcast=False)
pre = func(pre)
if prefix in ['convolution2d', 'maxpooling2d']:
for i in range(pre.shape[0]):
root = os.path.join(MODEL_PATH, 'img{:}'.format(i+1), 'layer{:}_'.format(n+1)+layer.name)
if not os.path.exists(root):
os.mkdir(root)
feature_maps = np.squeeze(pre[i])
for j, m in enumerate(feature_maps):
path = os.path.join(root, '{:}.png'.format(j+1))
plt.imsave(path, m, cmap=cm.gray)
def require_data():
# test
from CNN import real_data
data = real_data()[0][0:2] # previous two images
return data
def main():
# visualize_model()
data = require_data()
visualize_feature_map(data)
if __name__ == '__main__':
main()