CNN 可视化示例

CNN 可视化示例
For keras < v1

In [1]:

import os
os.environ[‘THEANO_FLAGS‘]=‘mode=FAST_RUN,device=gpu,floatX=float32‘
# This gives a ‘perform‘ error in compile
#os.environ[‘THEANO_FLAGS‘]=‘mode=FAST_COMPILE,device=gpu1,floatX=float32‘
#os.environ[‘THEANO_FLAGS‘]=‘device=gpu0‘

In [2]:

import theano
print theano.config.device
Couldn‘t import dot_parser, loading of dot files will not be possible.
gpu
Using gpu device 0: Tesla M2075

In [3]:

from __future__ import absolute_import
from __future__ import print_function
import pylab as pl
import matplotlib.cm as cm
import numpy as np
np.random.seed(1337) # for reproducibility

from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.utils import np_utils

‘‘‘
    Train a simple convnet on the MNIST dataset.

    Run on GPU: THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python mnist_cnn.py

    Get to 99.25% test accuracy after 12 epochs (there is still a lot of margin for parameter tuning).
    16 seconds per epoch on a GRID K520 GPU.
‘‘‘

Out[3]:

‘\n    Train a simple convnet on the MNIST dataset.\n\n    Run on GPU: THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python mnist_cnn.py\n\n    Get to 99.25% test accuracy after 12 epochs (there is still a lot of margin for parameter tuning).\n    16 seconds per epoch on a GRID K520 GPU.\n‘

In [4]:

np.set_printoptions(precision=5, suppress=True)

In [5]:

%matplotlib inline

In [6]:

nb_classes = 10

# the data, shuffled and split between tran and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()

X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)
X_train = X_train.astype("float32")
X_test = X_test.astype("float32")
X_train /= 255
X_test /= 255
print(‘X_train shape:‘, X_train.shape)
print(X_train.shape[0], ‘train samples‘)
print(X_test.shape[0], ‘test samples‘)

# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
X_train shape: (60000, 1, 28, 28)
60000 train samples
10000 test samples

In [7]:

i = 4600
pl.imshow(X_train[i, 0], interpolation=‘nearest‘, cmap=cm.binary)
print("label : ", Y_train[i,:])
label :  [ 0.  0.  0.  0.  0.  1.  0.  0.  0.  0.]

In [8]:

model = Sequential()

model.add(Convolution2D(32, 1, 3, 3, border_mode=‘full‘))
convout1 = Activation(‘relu‘)
model.add(convout1)
model.add(Convolution2D(32, 32, 3, 3))

convout2 = Activation(‘relu‘)
model.add(convout2)
model.add(MaxPooling2D(poolsize=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(32*196, 128))
model.add(Activation(‘relu‘))
model.add(Dropout(0.5))

model.add(Dense(128, nb_classes))
model.add(Activation(‘softmax‘))

model.compile(loss=‘categorical_crossentropy‘, optimizer=‘adadelta‘)

In [9]:

WEIGHTS_FNAME = ‘mnist_cnn_weights.hdf‘
if True and os.path.exists(WEIGHTS_FNAME):
    # Just change the True to false to force re-training
    print(‘Loading existing weights‘)
    model.load_weights(WEIGHTS_FNAME)
else:
    batch_size = 128
    nb_epoch = 12
    model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
              show_accuracy=True, verbose=1, validation_data=(X_test, Y_test))
    model.save_weights(WEIGHTS_FNAME)
score = model.evaluate(X_test, Y_test, show_accuracy=True, verbose=0)
print(‘Test score:‘, score[0])
print(‘Test accuracy:‘, score[1])
Train on 60000 samples, validate on 10000 samples
Epoch 0
60000/60000 [==============================] - 79s - loss: 0.2596 - acc: 0.9200 - val_loss: 0.0548 - val_acc: 0.9826
Epoch 1
60000/60000 [==============================] - 79s - loss: 0.0961 - acc: 0.9713 - val_loss: 0.0441 - val_acc: 0.9861
Epoch 2
60000/60000 [==============================] - 79s - loss: 0.0735 - acc: 0.9782 - val_loss: 0.0426 - val_acc: 0.9860
Epoch 3
60000/60000 [==============================] - 79s - loss: 0.0617 - acc: 0.9816 - val_loss: 0.0330 - val_acc: 0.9885
Epoch 4
60000/60000 [==============================] - 79s - loss: 0.0513 - acc: 0.9844 - val_loss: 0.0277 - val_acc: 0.9913
Epoch 5
60000/60000 [==============================] - 79s - loss: 0.0467 - acc: 0.9859 - val_loss: 0.0305 - val_acc: 0.9895
Epoch 6
60000/60000 [==============================] - 79s - loss: 0.0416 - acc: 0.9868 - val_loss: 0.0291 - val_acc: 0.9911
Epoch 7
60000/60000 [==============================] - 79s - loss: 0.0377 - acc: 0.9882 - val_loss: 0.0289 - val_acc: 0.9909
Epoch 8
60000/60000 [==============================] - 79s - loss: 0.0349 - acc: 0.9889 - val_loss: 0.0263 - val_acc: 0.9914
Epoch 9
60000/60000 [==============================] - 79s - loss: 0.0333 - acc: 0.9900 - val_loss: 0.0258 - val_acc: 0.9916
Epoch 10
60000/60000 [==============================] - 79s - loss: 0.0305 - acc: 0.9906 - val_loss: 0.0240 - val_acc: 0.9924
Epoch 11
60000/60000 [==============================] - 79s - loss: 0.0273 - acc: 0.9918 - val_loss: 0.0299 - val_acc: 0.9906
Test score: 0.0299028589356
Test accuracy: 0.9906

In [10]:

print(model.predict(X_test[1:5]))
print(Y_test[1:5])
[[ 0.  0.  1.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  1.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]]
[[ 0.  0.  1.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  1.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]]

In [11]:

Y_pred = model.predict(X_test)
# Convert one-hot to index
y_pred = np.argmax(Y_pred, axis=1)

In [12]:

from sklearn.metrics import classification_report
print(classification_report(y_test, y_pred))
             precision    recall  f1-score   support

          0       0.99      0.99      0.99       980
          1       0.99      1.00      1.00      1135
          2       0.99      0.99      0.99      1032
          3       0.98      1.00      0.99      1010
          4       0.99      0.99      0.99       982
          5       0.99      0.99      0.99       892
          6       0.99      0.99      0.99       958
          7       0.99      0.99      0.99      1028
          8       1.00      0.98      0.99       974
          9       0.99      0.98      0.99      1009

avg / total       0.99      0.99      0.99     10000

Convolution visualizations

In [13]:

convout1_f = theano.function([model.get_input(train=False)], convout1.get_output(train=False))
#convout2_f = theano.function([model.get_input(train=False)], convout2.get_output(train=False))

In [14]:

# utility functions
from mpl_toolkits.axes_grid1 import make_axes_locatable

def nice_imshow(ax, data, vmin=None, vmax=None, cmap=None):
    """Wrapper around pl.imshow"""
    if cmap is None:
        cmap = cm.jet
    if vmin is None:
        vmin = data.min()
    if vmax is None:
        vmax = data.max()
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="5%", pad=0.05)
    im = ax.imshow(data, vmin=vmin, vmax=vmax, interpolation=‘nearest‘, cmap=cmap)
    pl.colorbar(im, cax=cax)

In [15]:

i = 4600

# Visualize the first layer of convolutions on an input image
X = X_test[i:i+1]

pl.figure()
pl.title(‘input‘)
nice_imshow(pl.gca(), np.squeeze(X), vmin=0, vmax=1, cmap=cm.binary)

In [16]:

import numpy.ma as ma
def make_mosaic(imgs, nrows, ncols, border=1):
    """
    Given a set of images with all the same shape, makes a
    mosaic with nrows and ncols
    """
    nimgs = imgs.shape[0]
    imshape = imgs.shape[1:]

    mosaic = ma.masked_all((nrows * imshape[0] + (nrows - 1) * border,
                            ncols * imshape[1] + (ncols - 1) * border),
                            dtype=np.float32)

    paddedh = imshape[0] + border
    paddedw = imshape[1] + border
    for i in xrange(nimgs):
        row = int(np.floor(i / ncols))
        col = i % ncols

        mosaic[row * paddedh:row * paddedh + imshape[0],
               col * paddedw:col * paddedw + imshape[1]] = imgs[i]
    return mosaic

#pl.imshow(make_mosaic(np.random.random((9, 10, 10)), 3, 3, border=1))

In [17]:

# Visualize weights
W = model.layers[0].W.get_value(borrow=True)
W = np.squeeze(W)
print("W shape : ", W.shape)

pl.figure(figsize=(15, 15))
pl.title(‘conv1 weights‘)
nice_imshow(pl.gca(), make_mosaic(W, 6, 6), cmap=cm.binary)
W shape :  (32, 3, 3)

In [18]:

# Visualize convolution result (after activation)
C1 = convout1_f(X)
C1 = np.squeeze(C1)
print("C1 shape : ", C1.shape)

pl.figure(figsize=(15, 15))
pl.suptitle(‘convout1‘)
nice_imshow(pl.gca(), make_mosaic(C1, 6, 6), cmap=cm.binary)
C1 shape :  (32, 30, 30)

时间: 2024-11-05 12:10:53

CNN 可视化示例的相关文章

CNN 可视化结果分析

本文试图对CNN到底做了什么,给出一些分析解释.通过观察卷积层的输出构成的灰度图,试图寻找这个卷积在物理上有什么作用.训练数据是一些验证码图片,用了四个卷积层和两个全连接层的模型,在训练集上训练到98%的准确率,验证集80%.具体代码可参考我前面的这篇博文<CNN识别图形验证码>.图片似乎只有翻墙才能看到,不好意思. 可视化结果分别从以下几个角度做分析: 1 看每个卷积层经过激活函数(relu)后的输出图像 第一个卷积层的结果(相对比较容易懂): 为了方便人眼观察,对每一幅图的像素值都做了一个

CNN可视化可行性

一次卷积过程类似于下图,下面动图是GIF图(上下图数据一致,但是只看方法),不过是三维的(可以看做比二维的多一个颜色维,RGB).如果将上下图结合来看,下图只看两维的话,下面第一行就是把目标图(需要识别的图)利用过滤器求积,步长是2(每次向右向下移动两格).那么对上图的理解就是,有一个过滤器(曲线)对整个图进行每个局部的求积,从而找到与其对应的最相似的局部(积最大),每个layer层都有若干过滤器,过滤器的值需要机器学习进行一次一次迭代修改,从而让机器自己找到最理想的卷积核(过滤器)例如上图的曲

python数据可视化示例柱状图

from matplotlib import pyplot as plt import platform import pandas from pathlib import Path # 根据不同的平台设置字体,不然无法显示中文windows platform_dic = {"Darwin": "Arial Unicode MS", "Windows": "SimHei"} plt.rcParams['font.family'

深度学习-CNN tensorflow 可视化

tf.summary模块的简介 在TensorFlow中,最常用的可视化方法有三种途径,分别为TensorFlow与OpenCv的混合编程.利用Matpltlib进行可视化.利用TensorFlow自带的可视化工具TensorBoard进行可视化.这三种方法,在前面博客中都有过比较详细的介绍.但是,TensorFlow中最重要的可视化方法是通过tensorBoard.tf.summary和tf.summary.FileWriter这三个模块相互合作来完成的. tf.summary模块的定义位于s

深度神经网络可视化工具集锦

深度神经网络可视化工具集锦 雷锋网按:原文作者zhwhong,载于作者的个人博客,雷锋网(公众号:雷锋网)经授权发布.  TensorBoard:TensorFlow集成可视化工具 GitHub官方项目:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tensorboard TensorBoard 涉及到的运算,通常是在训练庞大的深度神经网络中出现的复杂而又难以理解的运算. 为了更方便 TensorFlow 程序的理

视觉机器学习笔记------CNN学习

卷积神经网络是第一个被成功训练的多层神经网络结构,具有较强的容错.自学习及并行处理能力. 一.基本原理 1.CNN算法思想 卷积神经网络可以看作为前馈网络的特例,主要在网络结构上对前馈网络进行简化和改进,从理论上讲,反向传播算法可以用于训练卷积神经网络.卷积神经网络被广泛用于语音识别和图像分类等问题. 2.CNN网络结构 卷积神经网络是一种多层前馈网络,每层由多个二维平面组成.每个平面由多个神经元组成. 网络输入为二维视觉模式,作为网络中间层的卷积层(C)和抽样层(S)交替出现.网络输出层为前馈

[译]matplotlib可视化教程 :绘制有关Turmp, Clinton 和Sanders的推特信息

使用 pandas 和 matplotlib 分析推特 Python有着各种各样的可视化库,其中包括了seaborn, networkx 和 vispy.大部分的可视化Python库都是基于或部分基于matplotlib, matplotlib往往是绘制一些简单图的首选,但是同时对于太过复杂的图往往无能为力而不得不借助于其他库. 在本篇的matplotlib教程中,我们将会涉及到该库的基础,并通过一些中等难度的可视化示例进行阐释. 我们所使用的数据集为大概240,000条推特,这些推特的内容都有

CNN中感受野的计算

感受野(receptive field)是怎样一个东西呢,从CNN可视化的角度来讲,就是输出featuremap某个节点的响应对应的输入图像的区域就是感受野. 比如我们第一层是一个3*3的卷积核,那么我们经过这个卷积核得到的featuremap中的每个节点都源自这个3*3的卷积核与原图像中3*3的区域做卷积,那么我们就称这个featuremap的节点感受野大小为3*3 如果再经过pooling层,假定卷积层的stride是1,pooling层大小2*2,stride是2,那么pooling层节点

图像处理关键词

高频分量: 形象一点说:亮度或灰度变化激烈的地方对应高频成分,如边缘:变化不大的地方对于低频成分,如大片色块区 画个直方图,大块区域是低频,小块或离散的是高频 <1>高通滤波:边缘提取与增强.边缘区域的灰度变换加大,也就是频率较高.所以,对于高通滤波,边缘部分将被保留,非边缘部分将被过滤: <2>低通滤波:边缘平滑,边缘区域将被平滑过渡 高斯滤波:高斯滤波是一种线性平滑滤波,适用于消除高斯噪声,广泛应用于图像处理的减噪过程.通俗的讲,高斯滤波就是对整幅图像进行加权平均的过程,每一个