from tensorflow.keras.datasets import cifar10
import numpy as np
(x_train,y_train),(x_test,y_test)=cifar10.load_data()
x_train.shape,y_train.shape
>>> ((50000, 32, 32, 3), (50000, 1))
x_train_data=x_train.astype('float32')/255.0
x_test_data=x_test.astype('float32')/255.0
标签值转化为onehot编码
from tensorflow.python.keras.utils import np_utils
y_train_onehot=np_utils.to_categorical(y_train)
y_test_onehot=np_utils.to_categorical(y_test)
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dropout,Activation,Flatten,Dense
from tensorflow.keras.layers import Conv2D,MaxPooling2D,ZeroPadding2D
线性序列模型
model=Sequential()
# 卷积层1
model.add(Conv2D(filters=32,kernel_size=(3,3),input_shape=(32,32,3), activation='relu',padding="same"))
model.add(Dropout(0.25))
# 池化层1
model.add(MaxPooling2D(pool_size=(2,2)))
# 卷积层2
model.add(Conv2D(filters=64,kernel_size=(3,3),activation='relu',
padding='same'))
model.add(Dropout(0.25))
# 池化层2
model.add(MaxPooling2D(pool_size=(2,2)))
# 平坦层
model.add(Flatten())
model.add(Dropout(0.25))
# 全连接层(隐藏层)
model.add(Dense(1024,activation='relu'))
model.add(Dropout(0.25))
# 输出层
model.add(Dense(10,activation='softmax'))
Dropout随机选择一些神经元丢弃,用于防止模型过拟合
过拟合:,由于训练集的数据太少 、数据的特征分布不一致等原因造,使得训练出来的模型对于新的数据表现的不好
查看模型各层参数情况
model.summary()
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
import matplotlib.pyplot as plt
# 定义可视化函数
def train_history_show(train_history,train,validation):
plt.plot(train_history.history[train])
plt.plot(train_history.history[validation])
plt.title('train_history')
plt.xlabel('epoch')
plt.ylabel('train history')
plt.legend(['train','validation'],loc='upper left')
plt.show()
模型准确率
train_history_show(train_history,'accuracy','val_accuracy')
train_history_show(train_history,'loss','val_loss')
score1=model.evaluate(x_test_data,y_test_onehot,verbose=0)
score1[0],score1[1]
>>> (0.803378701210022, 0.7249000072479248)
# np.argmax(a)#取出a中元素最大值所对应的索引
prediction=np.argmax(model.predict(x_test_data),axis=-1)
prediction.shape
>>> (10000,)
prediction[:9]
>>> array([3, 8, 8, 0, 6, 6, 1, 6, 3], dtype=int64)
# 定义可视化函数
def plot_image_label_prediction(images,labels,prediction,idx,num=10):
'''
images:图片的像素值(通常是x_test/x_train)
labels:图片标签(通常是y_test/y_train)
prediction:模型预测的标签值
idx:起始图片索引
num:生成图片的总数量
'''
# 生成一个绘图区域
fig=plt.gcf()
label_dict={0:'airplane',1:'automobile',2:'bird',3:'cat',4:'deer',
5:'dog',6:'frog',7:'horse',8:'ship',9:'truck'}
# 以英寸为单位设置图形大小
fig.set_size_inches(12,14)
# 最多生成20张图片
if num>20:num=20
for i in range(0,num):
# 生成子绘图区域
ax=plt.subplot(5,5,1+i)
# 将数组的值以图片的形式展示出来
ax.imshow(images[idx],cmap='binary')
# 将实际值与模型预测值显示
title=str(i)+','+label_dict[labels[i][0]]
if len(prediction)>0:
title+='=>'+label_dict[prediction[i]]
ax.set_title(title,fontsize=10)
# 设置x、y轴刻度
ax.set_xticks([])
ax.set_yticks([])
idx+=1
plt.show()
plot_image_label_prediction(x_test,y_test,prediction,0,num=10)
y_test_1=y_test.reshape(-1)
import pandas as pd
pd.crosstab(y_test_1,prediction,rownames=['label'],colnames=['predict'])
crosstab()函数的 第一个参数是列, 第二个参数是行,由于y_test和prediction的形状都是(10000,0),但是y_test要被展示为列,所以要使用reshape()函数转换一下形状
在这里只保存了模型的参数,也可以使用save()函数保存整个模型,注意,使用load_wights()函数仅保存了网络的权重。在调用load_weights之前,仍然需要定义其体系结构:,否则会报出下面错误:ValueError: Unable to load weights saved in HDF5 format into a subclassed Model which has not created its variables yet. Call the Model first, then load the weights
model.save_weights('model/weights/2卷积层+4096+1024+10.h5')
# 可以将整个模型使用save('model.h5')函数保存起来,但是文件会比较大,下次调用时直接使用load_model()函数即可,无需再次定义体系结构
# from keras.models import load_model
# model = load_model('model.h5')
本博客参考:《tensorflow+keras深度学习人工智能实践应用 林大贵著》
笔者已将本博客代码共享至:码云