2021-05-24

2021-05-24  本文已影响0人  __method__
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
mnist = keras.datasets.mnist


def get_train_val(mnist_path):
    (train_images, train_labels), (test_images, test_labels) = mnist.load_data(mnist_path)
    print("train_images nums:{}".format(len(train_images)))
    print("test_images nums:{}".format(len(test_images)))
    return train_images, train_labels, test_images, test_labels


def show_mnist(images, labels):
    for i in range(25):
        plt.subplot(5, 5, i + 1)
        plt.xticks([])
        plt.yticks([])
        plt.grid(False)
        plt.imshow(images[i], cmap=plt.cm.gray)
        plt.xlabel(str(labels[i]))
    plt.show()


def one_hot(labels):
    onehot_labels = np.zeros(shape=[len(labels), 10])
    for i in range(len(labels)):
        index = labels[i]
        onehot_labels[i][index] = 1
    return onehot_labels


def mnist_net(input_shape):

    model = keras.Sequential()
    model.add(keras.layers.Flatten(input_shape=input_shape))
    model.add(keras.layers.Dense(units=120, activation=tf.nn.relu))
    model.add(keras.layers.Dense(units=10, activation=tf.nn.softmax))
    return model


def mnist_cnn(input_shape):

    model = keras.Sequential()
    model.add(keras.layers.Conv2D(filters=32, kernel_size=5, strides=(1, 1),
                                  padding='same', activation=tf.nn.relu, input_shape=input_shape))
    model.add(keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='valid'))
    model.add(keras.layers.Conv2D(filters=64, kernel_size=3, strides=(1, 1), padding='same', activation=tf.nn.relu))
    model.add(keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='valid'))
    model.add(keras.layers.Dropout(0.25))
    model.add(keras.layers.Flatten())
    model.add(keras.layers.Dense(units=128, activation=tf.nn.relu))
    model.add(keras.layers.Dropout(0.5))
    model.add(keras.layers.Dense(units=10, activation=tf.nn.softmax))
    return model
def trian_model(train_images, train_labels, test_images, test_labels):
    # Normalize
    train_images = train_images / 255.0
    test_images = test_images / 255.0
    train_images = np.expand_dims(train_images, axis=3)
    test_images = np.expand_dims(test_images, axis=3)
    print("train_images :{}".format(train_images.shape))
    print("test_images :{}".format(test_images.shape))

    train_labels = one_hot(train_labels)
    test_labels = one_hot(test_labels)

    model = mnist_cnn(input_shape=(28, 28, 1))
    model.compile(optimizer=tf.train.AdamOptimizer(), loss="categorical_crossentropy", metrics=['accuracy'])
    model_history =  model.fit(x=train_images, y=train_labels, epochs=5, validation_data=(test_images, test_labels))

    test_loss, test_acc = model.evaluate(x=test_images, y=test_labels)
    print("Test Accuracy %.2f" % test_acc)

    cnt = 0
    predictions = model.predict(test_images)
    for i in range(len(test_images)):
        target = np.argmax(predictions[i])
        label = np.argmax(test_labels[i])
        if target == label:
            cnt += 1
    print("correct prediction of total : %.2f" % (cnt / len(test_images)))
    model.save('mnist-model.h5')
    return model_history


if __name__ == "__main__":
    mnist_path = './mnist.npz'
    train_images, train_labels, test_images, test_labels = get_train_val(mnist_path)

    show_mnist(train_images, train_labels)
    model_history = trian_model(train_images, train_labels, test_images, test_labels)
    print(model_history.history)
    epochs = range(5)
    loss = model_history.history['loss']
    val_loss = model_history.history['val_loss']
    acc = model_history.history['acc']
    plt.figure()
    plt.plot(epochs, loss, 'r', label='Training loss')
    plt.plot(epochs, val_loss, 'b', label='Validation loss')
    plt.plot(epochs, acc, 'g', label='acc')
    plt.title('Training and Validation Loss')
    plt.xlabel('Epoch')
    plt.ylabel('Loss Value')
    plt.ylim([0, 1])
    plt.legend()
    plt.show()

修改版

from keras.datasets import mnist
import tensorflow as tf
import numpy as np
from tensorflow import keras
import matplotlib.pyplot as plt

(train_images,train_labels),(test_images,test_labels)=mnist.load_data()
print(train_images.shape,train_labels.shape,test_images.shape,test_labels.shape)

# 搭建手写数字CNN模型
def mnist_cnn(input_shape):
    model = keras.Sequential()
    # 第一层卷积
    model.add(keras.layers.Conv2D(filters=32, kernel_size=5, strides=(1, 1),
                                  padding="same", activation=tf.nn.relu, input_shape =input_shape))
    # 池化
    model.add(keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='valid'))
    # 卷积
    model.add(keras.layers.Conv2D(filters=64, kernel_size=3, strides=(1, 1),
                                  padding="same", activation=tf.nn.relu))
    # 池化
    model.add(keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2), padding='valid'))
    # 防止过拟合
    model.add(keras.layers.Dropout(0.25))
    # 展平
    model.add(keras.layers.Flatten())
    # 全连接层
    model.add(keras.layers.Dense(units=128, activation=tf.nn.relu))
    model.add(keras.layers.Dropout(0.5))
    model.add(keras.layers.Dense(units=10, activation=tf.nn.softmax))
    return model

def trian_model(train_images, train_labels, test_images, test_labels):
    # Normalize
    train_images = train_images / 255.0
    test_images = test_images / 255.0
    train_images = np.expand_dims(train_images, axis=3)
    test_images = np.expand_dims(test_images, axis=3)
    print("train_images :{}".format(train_images.shape))
    print("test_images :{}".format(test_images.shape))

    train_labels = one_hot(train_labels)
    test_labels = one_hot(test_labels)

    model = mnist_cnn(input_shape=(28, 28, 1))
    model.compile(optimizer=tf.train.AdamOptimizer(), loss="categorical_crossentropy", metrics=['accuracy'])
    model_history =  model.fit(x=train_images, y=train_labels, epochs=5, validation_data=(test_images, test_labels))

    test_loss, test_acc = model.evaluate(x=test_images, y=test_labels)
    print("Test Accuracy %.2f" % test_acc)

    cnt = 0
    predictions = model.predict(test_images)
    for i in range(len(test_images)):
        target = np.argmax(predictions[i])
        label = np.argmax(test_labels[i])
        if target == label:
            cnt += 1
    print("correct prediction of total : %.2f" % (cnt / len(test_images)))
    model.save('mnist-model.h5')
    return model_history

def one_hot(labels):
    onehot_labels = np.zeros(shape=[len(labels), 10])
    for i in range(len(labels)):
        index = labels[i]
        onehot_labels[i][index] = 1
    return onehot_labels


def mnist_net(input_shape):

    model = keras.Sequential()
    model.add(keras.layers.Flatten(input_shape=input_shape))
    model.add(keras.layers.Dense(units=120, activation=tf.nn.relu))
    model.add(keras.layers.Dense(units=10, activation=tf.nn.softmax))
    return model


if __name__ == "__main__":

    model_history = trian_model(train_images, train_labels, test_images, test_labels)
    print(model_history.history)
    epochs = range(5)
    loss = model_history.history['loss']
    val_loss = model_history.history['val_loss']
    acc = model_history.history['acc']
    plt.figure()
    plt.plot(epochs, loss, 'r', label='Training loss')
    plt.plot(epochs, val_loss, 'b', label='Validation loss')
    plt.plot(epochs, acc, 'g', label='acc')
    plt.title('Training and Validation Loss')
    plt.xlabel('Epoch')
    plt.ylabel('Loss Value')
    plt.ylim([0, 1])
    plt.legend()
    plt.show()
上一篇下一篇

猜你喜欢

热点阅读