Keras Note

5897 ワード

Keras Note


Kerasテストの精度は訓練時に戻る精度より高い


Kerasはテスト時に、Drop out、L 1/L 2などのいくつかの規則化方法が閉じられ、トレーニング時には常に重み値が変化し、テスト時の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()