VGG16-1

from keras.models import Sequential
from keras.layers import Input, Dense, Dropout, Activation, Flatten
from keras.models import Model
from keras.optimizers import SGD
from keras.datasets import mnist
from keras.applications.vgg16 import VGG16
import numpy as np
import cv2
tBatchSize = 64
'''第一步:选择模型'''  # VGG16要求图片大小最少48
model_vgg = VGG16(include_top=False, input_shape=(48, 48, 3))
for layer in model_vgg.layers:
    layer.trainable = False

model = Flatten(name='flatten')(model_vgg.output)
model = Dense(500, activation='relu', name='fc1')(model)
model = Dense(500, activation='relu', name='fc2')(model)
model = Dropout(0.5)(model)
model = Dense(10, activation='softmax')(model)
model = Model(inputs=model_vgg.input,
              outputs=model, name='vgg16')

model.compile(loss='categorical_crossentropy', optimizer='adam')  # 使用交叉熵作为loss函数

(X_train, y_train), (X_test, y_test) = mnist.load_data()  # 使用Keras自带的mnist工具读取数据(第一次需要联网)

# 由于mist的输入数据维度是(num, 28, 28),vgg16 需要三维图像,因为扩充一下mnist的最后一维

X_train = [cv2.cvtColor(cv2.resize(i, (48, 48)), cv2.COLOR_GRAY2RGB) for i in X_train]
X_test = [cv2.cvtColor(cv2.resize(i, (48, 48)), cv2.COLOR_GRAY2RGB) for i in X_test]

X_train = np.array(X_train)
X_test = np.array(X_test)

# 这个能生成一个OneHot的10维向量,作为Y_train的一行,这样Y_train就有60000行OneHot作为输出
Y_train = (np.arange(10) == y_train[:, None]).astype(int)  # 整理输出
Y_test = (np.arange(10) == y_test[:, None]).astype(int)  # np.arange(5) = array([0,1,2,3,4])

# 非常重要! 虽然不至于会导致 不收敛,但是加入以下代码, 收敛速度会得到明显提升
X_train = X_train / 256.0
X_test = X_test / 256.0

model.fit(X_train, Y_train, batch_size=tBatchSize, epochs=10, shuffle=True, validation_split=0.3)

猜你喜欢

转载自blog.csdn.net/sunboyiris/article/details/89371244