CNN 1. 训练cifar10数据集 基于tensorflow2

CNN: MiniVGG

  • 结论
  • 代码 训练过程
    • 1.Import所需的库
    • 2. 获取数据
    • 3 建立模型 MiniVGG模型
    • 训练模型
    • 运行
    • 其他

此项目使用了 MiniVGG, MiniVGG with data augmentation 和 ResNet 三种CNN网络结构训练cifar10数据集,大概需要120多分钟(GTX1650, WIN10, I5-9300H),trian:20000,, valid: 20000, test:10000,在电脑上跑一跑熟悉CNN建立和不同网络比较还是比较练手的项目. VGG 网络论证了CNN 网络的深度对性能的影响。

VGG参考

结论

先说结论

  1. Accuracy: 当数据集小一点时,如10000,使用ImageDataAugmentation 差不多可以提高0.1%,当使用多一点的数据时,两者不相上下,都是在71% 左右,而ResNet为0.57,可能是构造的ResNet网络比较简单,此网络参数不到4000,远小于另外两个网络参数
  2. Time Cost: ResNet 运行时间最长,其次MiniVGG_data_aug,MiniVGG
  3. 构造稍多层的CNN网络结构,了解feature map, filter 作用,为减少运行时间,下次可以尝试使用 (9,9)的filter shape。
  4. 实现一个mini VGG 网络结构

代码 训练过程

1.Import所需的库

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

2. 获取数据

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

3 建立模型 MiniVGG模型

使用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等比较,相同数据集

你可能感兴趣的:(Deep,Learning,tensorflow,深度学习)