VGG参考
先说结论
tensroflow2.0 GPU 版本
import csv
import tensorflow as tf
from tensorflow import keras
from keras import datasets, models, layers
import matplotlib.pyplot as plt
from keras import backend as K
import numpy as np
import os, time
from resnet import ResNet
from keras.preprocessing.image import ImageDataGenerator
os.environ['CUDA_VISIBLE_DEVICES'] = '/device:GPU:0' # use GPU
K.clear_session() # Some memory clean-up
def load_cifar_data():
(X_train_full, y_train_full), (X_test, y_test) = datasets.cifar10.load_data()
X_train_full, X_test = X_train_full / 255.0, X_test / 255.0
print(X_train_full.shape, y_train_full.shape)
print(X_test.shape, y_test.shape)
# shuffle the trianing dataset
idxs = np.arange(X_train_full.shape[0])
np.random.shuffle(idxs)
X_train_full = X_train_full[idxs]
y_train_full = y_train_full[idxs]
X_train, y_train = X_train_full[0:20000], y_train_full[0:20000]
X_valid, y_valid = X_train_full[20000:40000], y_train_full[20000:40000]
##############################################################################
# for test
# X_train, y_train = X_train_full[0:100], y_train_full[0:100]
# X_valid, y_valid = X_train_full[100:200], y_train_full[100:200]
# X_test, y_test = X_test[:100], y_test[:100]
##############################################################################
return X_train, y_train, X_valid, y_valid, X_test, y_test
使用2个3x3卷积核来代替5*5卷积核,这样做的主要目的是在保证具有相同感知野的条件下,提升了网络的深度,在一定程度上提升了神经网络的效果,全连接层减少为2个
def build_model():
vgg_model = models.Sequential()
vgg_model.add(layers.Conv2D(32, (3,3), padding="same",activation="relu", input_shape=(32,32,3)))
vgg_model.add(layers.BatchNormalization(momentum=0.9))
vgg_model.add(layers.Conv2D(32, (3,3), padding="same",activation="relu"))
vgg_model.add(layers.BatchNormalization(momentum=0.9))
vgg_model.add(layers.MaxPool2D(pool_size=(2,2), padding="same"))
vgg_model.add((layers.Dropout(rate=0.25)))
vgg_model.add(layers.Conv2D(64, (3, 3), padding="same", activation="relu"))
vgg_model.add(layers.BatchNormalization(momentum=0.9))
vgg_model.add(layers.Conv2D(64, (3, 3), padding="same", activation="relu"))
vgg_model.add(layers.BatchNormalization(momentum=0.9))
vgg_model.add(layers.MaxPool2D(pool_size=(2, 2), padding="same"))
vgg_model.add((layers.Dropout(rate=0.25)))
vgg_model.add(layers.Flatten())
vgg_model.add(layers.Dense(512, activation="relu"))
vgg_model.add(layers.BatchNormalization(momentum=0.9))
vgg_model.add((layers.Dropout(rate=0.25)))
vgg_model.add(layers.Dense(10, activation="softmax"))
# vgg_model.summary()
return vgg_model
训练模型,flag 选择是否使用data augmenta
def trian(vgg_model, X_train, y_train, X_valid, y_valid, X_test, y_test, aumentation_flag=None):
# batch_size = 32 (default)
opt = keras.optimizers.SGD(lr=0.001, decay=0.01 / 40, momentum=0.9, nesterov=True)
vgg_model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
if aumentation_flag:
# II. MiniVGG with data augmentation
aug = ImageDataGenerator(rotation_range=5, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.1,
zoom_range=0.1, horizontal_flip=True, fill_mode="nearest")
history = vgg_model.fit_generator(aug.flow(x=X_train, y=y_train, batch_size=32), epochs=20,
validation_data=(X_valid, y_valid))
else:
history = vgg_model.fit(x=X_train, y=y_train, batch_size=32, epochs=20, validation_data=(X_valid, y_valid))
test_loss, test_acc = vgg_model.evaluate(X_test, y_test, verbose=1, batch_size=64)
print("Test acc is {}".format(test_acc))
return history, test_loss, test_acc
cost_time, acc, loss = {
}, {
}, {
} # save training results
########################################################################################
# run each model to record the results
########################################################################################
# write to txt
data = [cost_time, loss, acc]
with open('model.txt', 'a', newline='', encoding='utf-8') as f:
for d in data:
for i, j in d.items():
f.writelines((i + " " + str(j) + " "))
f.write("\n")
f.close()
print("\n all done")
ResNet 保存模型的方式需要再查找资料
做其他类型的CNN如alexnet, lenet_5等比较,相同数据集