Home > other >  Modified algorithm is used to identify the mnist VGG16 (learning)
Modified algorithm is used to identify the mnist VGG16 (learning)

Time:05-20

The from keras. Applications import VGG16
The from keras. Datasets import mnist
The from keras. Utils import to_categorical
The from keras import models
The from keras. The layers. The core import Dense, Flatten, Dropout
The import cv2
The import numpy as np

# load data
(x_train y_train), (x_test, y_test)=mnist. Load_data ()
# VGG16 models, weighted by ImageNet training, and the model's default input size is 224 x224, but the minimum is 48 x48
# change the size of the data set, the gray image is converted to RGB image
X_train=[cv2 cvtColor (cv2. Resize (I, (48) 13), cv2. COLOR_GRAY2BGR) for I x_train] in
X_test=[cv2 cvtColor (cv2. Resize (I, (48) 13), cv2. COLOR_GRAY2BGR) for I x_test] in
# the first step: by np. Newaxis function turn each picture to add a dimension to,48,48,3 (1), there has been in the program so arr [np. Newaxis],
Step # 2: by np. Concatenate connect each array to form a new x_train array, connection x_train array shape for,48,48,3 (10000)
X_train=np. Concatenate ([arr [np. Newaxis] for arr in x_train])
X_test=np. Concatenate ([arr [np. Newaxis] for arr in x_test])


X_train=x_train. Astype (" float32 ")/255
X_train=x_train. Reshape (,48,48,3 (60000))
X_test=x_test. Astype (" float32 ")/255
X_test=x_test. Reshape (,48,48,3 (10000))
Y_train=to_categorical (y_train)
Y_test=to_categorical (y_test)

# create validation set
X_val=x_train [: 10000]
Y_val=y_train [: 10000]
X_train=x_train/10000:
Y_train=y_train/10000:

# model
='imagenet' conv_base=VGG16 (weights,
Include_top=False,
Input_shape=(48,48,3))
Conv_base. Trainable=False
The model=models. Sequential ()
Model. The add (conv_base)
Model. The add (Flatten ())
Model. The add (Dense (4096, activation="relu"))
Model. The add (Dropout (0.5))
# layer 14
Model. The add (Dense (4096, activation="relu"))
Model. The add (Dropout (0.5))
# layer 15
Model. The add (Dense (10, activation="softmax"))
Model. The summary ()

# compiler model
Model.com from running (optimizer="rmsprop", loss="categorical_crossentropy", the metrics=[" accuracy "])

# training model
The model fit (x_train y_train, batch_size=64, epochs=5, validation_data=https://bbs.csdn.net/topics/(x_val y_val))

# evaluation model
Test_loss, test_acc=model. The evaluate (x_test y_test, batch_size=64)
Print (" The accuracy is: "+ STR (test_acc))
  • Related