之前一段时间里,学习过tensorflow和Pytorch也写了点心得,目前是因为项目原因用了一段时间Keras,觉得很不错啊,至少从入门来说对新手极度友好,由于keras是基于tensoflow的基础,相当于tensorflow的高级API吧!(如果理解有错可以在下方评论纠正博主哈!)
一、安装
安装很简单,也是基于tensorflow的前提下(如果有疑问,请参考博主之前的博客),
pip install keras
二、应用示例
# -*- coding: utf-8 -*-
# =============================================================================
# #在线下载
# from keras.datasets import mnist
# (X_train,Y_train),(X_test,Y_test) = mnist.load_data()
#
# print("train:%d imgs"%len(X_train))
# print("test:%d imgs"%len(X_test))
# =============================================================================
online_or_not =False
#本地读取
from tensorflow.examples.tutorials.mnist import input_data
import numpy as npimport matplotlib.pyplot as plt
from PIL import Imagedef show_mnist(train_image,train_labels):n = 3m = 3for i in range(n):for j in range(m):plt.subplot(n,m,i*n+j+1)#plt.subplots_adjust(wspace=0.2, hspace=0.8)index = i * n + j #当前图片的标号img_array = train_image[index]*255img = Image.fromarray(img_array)plt.title(train_labels[index])plt.imshow(img,cmap='Greys')plt.show()#show_mnist(x_train, y_train)# coding: utf-8
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation, Convolution2D, MaxPooling2D, Flatten
from keras.optimizers import Adam
np.random.seed(1337)"""mnist数据集的label本身进行了one-hot标签化处理"""
if online_or_not:# download the mnist(X_train, Y_train), (X_test, Y_test) = mnist.load_data()# data pre-processingX_train = X_train.reshape(-1, 1, 28, 28)/255X_test = X_test.reshape(-1, 1, 28, 28)/255Y_train = np_utils.to_categorical(Y_train, num_classes=10)Y_test = np_utils.to_categorical(Y_test, num_classes=10)
else:X_train = []X_test = []print("train:%d imgs"%len(X_train))
print("test:%d imgs"%len(X_test))# build CNN
model = Sequential()# conv layer 1 output shape(32, 28, 28)
model.add(Convolution2D(filters=32,kernel_size=5,strides=1,padding='same',batch_input_shape=(None, 1, 28, 28),data_format='channels_first'))
model.add(Activation('relu'))# pooling layer1 (max pooling) output shape(32, 14, 14)
model.add(MaxPooling2D(pool_size=2, strides=2, padding='same', data_format='channels_first'))# conv layer 2 output shape (64, 14, 14)
model.add(Convolution2D(64, 5, strides=1, padding='same', data_format='channels_first'))
model.add(Activation('relu'))# pooling layer 2 (max pooling) output shape (64, 7, 7)
model.add(MaxPooling2D(2, 2, 'same', data_format='channels_first'))# full connected layer 1 input shape (64*7*7=3136), output shape (1024)
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation('relu'))# full connected layer 2 to shape (10) for 10 classes
model.add(Dense(10))
model.add(Activation('softmax'))model.summary()
# define optimizer
adam = Adam(lr=1e-4)
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])# training
print ('Training')
model.fit(X_train, Y_train, epochs=1, batch_size=128)# testing
print ('Testing')
loss, accuracy = model.evaluate(X_test, Y_test)
print ('loss, accuracy: ', (loss, accuracy))
三、模型可视化
from tensorflow.examples.tutorials.mnist import input_data
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation, Convolution2D, MaxPooling2D, Flatten
from keras.optimizers import Adam
#模型可视化引入
import keras.callbacks
import tensorflow as tf
np.random.seed(1337)(X_train, Y_train), (X_test, Y_test) = mnist.load_data()# data pre-processing
X_train = X_train.reshape(-1, 1, 28, 28)/255
X_test = X_test.reshape(-1, 1, 28, 28)/255
Y_train = np_utils.to_categorical(Y_train, num_classes=10)
Y_test = np_utils.to_categorical(Y_test, num_classes=10)# build CNN
def build_model():model = Sequential()# conv layer 1 output shape(32, 28, 28)model.add(Convolution2D(filters=32,kernel_size=5,strides=1,padding='same',batch_input_shape=(None, 1, 28, 28),data_format='channels_first'))model.add(Activation('relu'))# pooling layer1 (max pooling) output shape(32, 14, 14)model.add(MaxPooling2D(pool_size=2, strides=2, padding='same', data_format='channels_first'))# conv layer 2 output shape (64, 14, 14)model.add(Convolution2D(64, 5, strides=1, padding='same', data_format='channels_first'))model.add(Activation('relu'))# pooling layer 2 (max pooling) output shape (64, 7, 7)model.add(MaxPooling2D(2, 2, 'same', data_format='channels_first'))# full connected layer 1 input shape (64*7*7=3136), output shape (1024)model.add(Flatten())model.add(Dense(1024))model.add(Activation('relu'))# full connected layer 2 to shape (10) for 10 classesmodel.add(Dense(10))model.add(Activation('softmax'))model.summary()return model
model = build_model()
# define optimizer
adam = Adam(lr=1e-4)
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])#模型可视化
tb_config = keras.callbacks.TensorBoard(log_dir= 'E:/python/kerascode/mnist_cnn/logs', write_images= 1, histogram_freq= 0)
cbks = [tb_config]# training
print ('Training')
#模型可视化需要加入“callbacks= cbks”
model.fit(X_train, Y_train, epochs=1, callbacks= cbks, batch_size=512)# testing
print ('Testing')
loss, accuracy = model.evaluate(X_test, Y_test)
print ('loss, accuracy: ', (loss, accuracy))
会在 log_dir= 'E:/python/kerascode/mnist_cnn/logs'该文件夹处生成events.out.tfevents.1566199816.ZD 训练日志,打开Anaconda Prompt,激活对应的环境,定位到logs文件的上一级目录,如下图所示,输入
tensorboard --logdir=AB
(此处AB即上文中的logs文件夹名称)。
将网址复制到谷歌浏览器中即可,结果如下图所示:
四、模型可视化踩坑
from keras.utils.vis_utils import plot_model
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.embeddings import Embedding
from keras.layers.recurrent import LSTMmodel = Sequential()
model.add(Embedding(input_dim=1024, output_dim=256, input_length=50))
model.add(LSTM(128)) # try using a GRU instead, for fun
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))plot_model(model, to_file='model1.png', show_shapes=True)
这个过程会报错,
raise ImportError('Failed to import `pydot`. ''Please install `pydot`. ''For example with `pip install pydot`.')
作为一个菜鸡,只能查到是导入pydot发生错误,很尴尬!还好有个博客大佬,教会如何改错。
打开vis_utils.py文件,将相应地方注释并加入下列代码
# `pydot` is an optional dependency,
# see `extras_require` in `setup.py`.
# =============================================================================
# try:
# import pydot
# except ImportError:
# pydot = None
# =============================================================================
try:import pydot_ng as pydot
except ImportError:try:import pydotplus as pydotexcept ImportError:try:import pydotexcept ImportError:pydot=None
参考文献:
https://blog.csdn.net/xu_haim/article/details/84981284