# 使用迁移学习的思想,以VGG16作为模板搭建模型,训练识别手写字体
# 引入VGG16模块
from keras.applications.vgg16 import VGG16
# 其次加载其他模块
from keras.layers import Input #输入层
from keras.layers import Flatten #压扁它
from keras.layers import Dense #全连接层
from keras.layers import Dropout #防止过拟合
from keras.models import Model #创建模型
from keras.optimizers import SGD #我们这里用随机梯度下降,当然我们也可以用其他的优化器
# 加载字体库作为训练样本
from keras.datasets import mnist
# 加载OpenCV(在命令行中窗口中输入pip install opencv-python),这里为了后期对图像的处理,
# 大家使用pip install C:\Users\28542\Downloads\opencv_python-3.4.1+contrib-cp36-cp36m-win_amd64.whl
# 比如尺寸变化和Channel变化。这些变化是为了使图像满足VGG16所需要的输入格式
import cv2
import h5py as h5py
import numpy as np
# 建立一个模型,其类型是Keras的Model类对象,我们构建的模型会将VGG16顶层去掉,只保留其余的网络
# 结构。这里用include_top = False表明我们迁移除顶层以外的其余网络结构到自己的模型中
# VGG模型对于输入图像数据要求高宽至少为48个像素点,由于硬件配置限制,我们选用48个像素点而不是原来
# VGG16所采用的224个像素点。即使这样仍然需要24GB以上的内存,或者使用数据生成器
model_vgg = VGG16(include_top=False, pooling='avg', weights='imagenet', input_shape=(48, 48, 3))
""" weights='imagenet'是将大赛用的那份权重数据给放到这里用"""
for layer in model_vgg.layers:
layer.trainable = False
"""layer.trainable = False 这里设置为false之后就可以往原来的数据集增加新的数据"""
model = Flatten(name = 'flatten')(model_vgg.output)#这里他可以拿到全连接层输出的一些数据
model = Dense(4096, activation='relu', name='fc1')(model)
"""从第一个dense开始之前去掉的全连接层,从这里接入新的全连接层,节点数设置为4096个"""
model = Dense(4096, activation='relu', name='fc2')(model)
"""然后在把上面做了全连接的这一层 在接在新的全连接层"""
model = Dropout(0.5)(model)#这里呢就是在做一个dropout防止过拟合
model = Dense(10, activation='softmax')(model)#手写数字只有十个类别,所以,这里最后的输出层只设置为10个隐藏节点
model_vgg_mnist = Model(inputs=model_vgg.input, outputs=model, name='vgg16')
"""这里我们创建一个model模型,然后整个model模型的输入设置为inputs=model_vgg.input,也就是输入层是继承model_vgg.input,
输出层继承outputs=model"""
# 打印模型结构,包括所需要的参数
model_vgg_mnist.summary()
'''
model_vgg = VGG16(include_top=False, weights='imagenet', input_shape=(224, 224, 3))
for layer in model_vgg.layers:
layer.trainable = False
model = Flatten()(model_vgg.output)
model = Dense(4096, activation='relu', name='fc1')(model)
model = Dense(4096, activation='relu', name='fc2')(model)
model = Dropout(0.5)(model)
model = Dense(10, activation='softmax', name='prediction')(model)
model_vgg_mnist_pretrain = Model(model_vgg.input, model, name='vgg16_pretrain')
model_vgg_mnist_pretrain.summary()
'''
"""
# 新的模型不需要训练原有卷积结构里面的1471万个参数,但是注意参数还是来自于最后输出层前的两个
# 全连接层,一共有1.2亿个参数需要训练
sgd = SGD(lr=0.05, decay=1e-5)
model_vgg_mnist.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
# 因为VGG16对网络输入层的要求,我们用OpenCV把图像从32*32变成224*224,把黑白图像转成RGB图像
# 并把训练数据转化成张量形式,供keras输入
(X_train, y_train), (X_test, y_test) = mnist.load_data("../test_data_home")
# X_train, y_train = X_train[:10000], y_train[:10000]
# X_test, y_test = X_test[:1000], y_test[:1000]
X_train = [cv2.cvtColor(cv2.resize(i, (48, 48)), cv2.COLOR_GRAY2RGB)
for i in X_train]
# 下面concatenate做的事情是把每个样本按照行堆叠在一起,因为是np下面的方法,所以返回的是ndarray
# np.newaxis它本质是None,arr是(48,48,3),arr[None]是(1,48,48,3)
X_train = np.concatenate([arr[np.newaxis] for arr in X_train]).astype('float32')
X_test = [cv2.cvtColor(cv2.resize(i, (48, 48)), cv2.COLOR_GRAY2RGB)
for i in X_test]
X_test = np.concatenate([arr[np.newaxis] for arr in X_test]).astype('float32')
print(X_train.shape)
print(X_test.shape)
X_train /= 255
X_test /= 255
def tran_y(y):
y_ohe = np.zeros(10)
y_ohe[y] = 1
return y_ohe
y_train_ohe = np.array([tran_y(y_train[i]) for i in range(len(y_train))])
y_test_ohe = np.array([tran_y(y_test[i]) for i in range(len(y_test))])
model_vgg_mnist.fit(X_train, y_train_ohe, validation_data=(X_test, y_test_ohe),
epochs=100, batch_size=100)
# model_vgg_mnist_pretrain.summary()
"""

老师,我一直登录不了这个网站下载这个文件,怎么办?