使用AlexNet训练mnist(面向对象)

from keras.callbacks import TensorBoard
from keras.models import Sequential
from keras.optimizers import SGD, Adam
from keras.layers import Dense, Flatten, Dropout
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.models import load_model
import keras
import numpy as np
from keras.applications.imagenet_utils import preprocess_input
from keras import backend as K
from keras.datasets import cifar10
from tensorflow.examples.tutorials.mnist import input_data
K.clear_session()
mnist = input_data.read_data_sets("MNIST_DATA", one_hot=True)
class AlexModel:
    #初始化参数
    def __init__(self, epochs, batch_size):
        """
        :param epochs: 训练集迭代的轮数
        :param batch_size: 每次训练的样本的个数
        """
        self.epochs = epochs
        self.batch_size = batch_size
        # 存储训练过程中的精度和误差
        self.train_accuracy_and_loss = None
    # 创建模型
    def build_model(self):
        """
        创建模型, 基于alexnet
        :return: 
        """

        model = Sequential()
        #第一层卷积网络,使用96个卷积核,大小为11x11步长为4, 要求输入 1个通道,激活函数使用relu
        model.add(Conv2D(96, (11, 11), strides=(4, 4), input_shape=(28, 28, 1), padding='valid', activation='relu',
                         kernel_initializer='uniform'))
        # 池化层
        model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='valid'))

        # 第二层加边使用256个5x5的卷积核,加边,激活函数为relu
        model.add(Conv2D(256, (5, 5), strides=(1, 1), padding='same', activation='relu', kernel_initializer='uniform'))
        #使用池化层,步长为2
        model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same'))

        # 第三层卷积,大小为3x3的卷积核使用384个
        model.add(Conv2D(384, (3, 3), strides=(1, 1), padding='same', activation='relu', kernel_initializer='uniform'))
        # 第四层卷积,同第三层
        model.add(Conv2D(384, (3, 3), strides=(1, 1), padding='same', activation='relu', kernel_initializer='uniform'))
        # 第五层卷积使用的卷积核为256个,其他同上
        model.add(Conv2D(256, (3, 3), strides=(1, 1), padding='same', activation='relu', kernel_initializer='uniform'))
        model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same'))
        # 将卷积展开为神经元
        model.add(Flatten())
        # 第1层隐藏全连接层使用4096个神经元
        model.add(Dense(4096, activation='relu'))
        # dropout正则化
        model.add(Dropout(0.5))
        # 第2层隐藏使用4096个神经元
        model.add(Dense(4096, activation='relu'))
        model.add(Dropout(0.5))
        # 输出层输出类别个数
        model.add(Dense(10, activation='softmax'))
        # 选用adam优化器,学习率为0.0003
        adam = Adam(lr=0.0003, decay=1e-6)
        # 编译模型
        model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
        return model
    # 保存模型
    def save_model_after_train(self):
        model = self.build_model()
        x_train, y_train = mnist.train.images, mnist.train.labels
        x_train = x_train.reshape(55000, 28, 28, 1)
        self.train_accuracy_and_loss = model.fit(x_train, y_train, batch_size=self.batch_size, epochs=self.epochs)
        model.save("model.h5")
    # 加载模型
    def load_model(self):
        return load_model("model.h5")
    # 训练模型
    def train(self, mnist):
        modle = self.build_model()

        x_train, y_train = mnist.train.images, mnist.train.labels
        x_train = x_train.reshape(55000, 28, 28, 1)
        # {'acc': [], 'loss': []}
        self.train_accuracy_and_loss = modle.fit(x_train, y_train, batch_size=self.batch_size,
                                                 epochs=self.epochs,
                                                 callbacks=[TensorBoard(log_dir='mytensorboard/3')])

        # 获取训练过程中的损失(每个epoch)
    def get_train_loss(self):
        return self.train_accuracy_and_loss.history["loss"]
    # 获取训练过程中的精度(每个epoch)
    def get_train_accuracy(self):
        return self.train_accuracy_and_loss.history["acc"]
    # 测试集的精度和误差
    def test_accuracy_and_loss(self):
        """"将训练好的模型直接拿过来用"
        :return: 返回精度和损失
        """
        model = self.load_model()
        x_test, y_test= mnist.test.images, mnist.test.labels
        x_test = x_test.reshape(10000, 28, 28, 1)
        score = model.evaluate(x_test, y_test, batch_size=32)
        return score[1], score[0]

    
model = AlexModel(epochs=2, batch_size=256)
model.train(mnist)
loss = model.get_train_loss()
acc = model.get_train_accuracy()
print(acc)

你可能感兴趣的:(深度学习)