代码的运行环境
win10专业版
Anaconda2020.02 +tensorflow1.14.0 + keras2.2.5
源代码
源代码主要来自杨培文的《深度学习入门图像处理》这本书,进行了一些微小的改变:把数据集cifar-10改成cifar-100。
from __future__ import print_function
import numpy as np
from keras.callbacks import TensorBoard
from keras.models import Sequential
from keras.optimizers import Adam
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPool2D
from keras.utils import np_utils
from keras import backend as K
from keras.callbacks import ModelCheckpoint
from keras.datasets import cifar100
from keras.preprocessing.image import ImageDataGenerator
from keras.backend.tensorflow_backend import set_session
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
set_session(tf.Session(config=config))
np.random.seed(42)
print("Initialized!")
##############################################################
#定义变量
batch_size = 32 # 32
nb_classes = 100 # 类别
nb_epoch = 500
img_rows, img_cols = 32, 32 #图像的长宽像素大小
nb_filters = [32, 32, 64, 64]
pool_size = (2,2)
kernel_size = (3, 3) #卷积核大小
# (x_train, y_train), (x_test, y_test) = cifar100.load_data(label_mode='fine')
#通过下面这一语句可以将数据自动下载到C:\Users\dell.keras\datasets里
(X_train, y_train), (X_test, y_test) = cifar100.load_data(label_mode='fine')
X_train = X_train.astype("float32") / 255
X_test = X_test.astype("float32") / 255
y_train = y_train
y_test = y_test
input_shape = (img_rows, img_cols, 3)
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
##################################################################
#上游部分,基于生成器的批量生成输入模块
datagen = ImageDataGenerator(
featurewise_center = False,
samplewise_center = False,
featurewise_std_normalization = False,
samplewise_std_normalization = False,
zca_whitening = False,
rotation_range = 0,
width_shift_range = 0.1,
height_shift_range = 0.1,
horizontal_flip = True,
vertical_flip = False)
datagen.fit(X_train)
#用各种零件搭建深度神经网络
model = Sequential()
model.add(Conv2D(nb_filters[0], kernel_size, padding = 'same',
input_shape = X_train.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(nb_filters[1], kernel_size))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size = pool_size))
model.add(Dropout(0.01)) # 0.25
model.add(Conv2D(nb_filters[2], kernel_size, padding = 'same'))
model.add(Activation('relu'))
model.add(Conv2D(nb_filters[3], kernel_size))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size = pool_size))
model.add(Dropout(0.01)) # 0.25
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.01)) # 0.5
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
###############################################################
#下游部分,使用凸优化模块训练模型
adam = Adam(lr=0.0001)
model.compile(loss = 'categorical_crossentropy',
optimizer=adam,
metrics=['accuracy'])
################################################################3
#最后开始训练模型,并且评估模型的准确性
#训练模型
best_model = ModelCheckpoint("cifar100_best.h5", monitor='val_loss', verbose=0, save_best_only=True)
tb = TensorBoard(log_dir="./logs")
model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size),
steps_per_epoch=X_train.shape[0] // batch_size,
epochs=nb_epoch, verbose=1,
validation_data=(X_test, Y_test), callbacks=[best_model,tb])
###############################################################
# 模型评分
score = model.evaluate(X_test, Y_test, verbose=0)
# 输出结果
print('Test score:', score[0])
print("Accuracy: %.2f%%" % (score[1]*100))
print("Compiled!")
控制迭代次数
从书上最开始的迭代50次到500次,可以看到最后预测的结果会有一个提升。这次具体是从40%左右到50%,但是到50%之后基本上不会因为迭代次数的增加而提升预测率了。为了验证这个猜想,今天跑了迭代次数 =2000的实验,结果如下图:
可见预测的精确度达到50%,提升的空间很小。