美文网首页
2021-05-24

2021-05-24

作者: __method__ | 来源:发表于2021-05-24 18:59 被阅读0次
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()

相关文章

  • bitshares比特股数据20210524

    2021-05-24比特股BTS大额转账的记录 时间转出转入BTS数量11:02:48zbbts001zbsend...

  • 2021-05-24 赋予意义的价值

    2021-05-24日精进 第327天 评估:9 分 儿子发红包,说进入状态太难,今天打赌失败。看见了孩子的沮丧...

  • 无期待

    我怎么如此幸运-99将帅挑战赛81-重生251-戴红霞(2021-05-24) 我怎么如此幸运-无期待 1.我怎么...

  • 守得云开见月明。

    2021-05-24 看了老番茄的一期视频、人家那视频才叫视频、做的是真的好。 好像无论如何都睡不醒。尤其是眼睛,...

  • 好大的风啊

    2021-05-24 晴 (上午10:13)今天的风好大,坐在办公室里,风的号角声依然听的清清楚楚。 同事们一起聊...

  • 矫枉过正一句丁宁君记取,神仙须是闲人做

    原创 山之石 天生反骨山之石 2021-05-24 10:07 鸿桥镇有近千年历史,主街中央的松露寺始建于北宋仁宗...

  • 2021-05-24

    【打卡】第151天 20210524 【书目8】《最美的教育最简单》 【作者】尹建莉 【页数】361页 【进度】2...

  • 2021-05-24

    跟Z一起过了周末,开心开心开心。周六出了魔都,去了一个网红点,玩到半夜,然后吃吃夜宵,一天就这么过去了。周日逛商场...

  • 2021-05-24

    中午回来,俩小孩在讨论动画片,到了激动处,永烨突然找到了自己的梦想:妈妈,我决定当一名魂师,再也不想生活在这个普通...

  • 2021-05-24

    这个星期我们进行了电工实训,只训练上午,然后刚开始还挺顺利的,老师讲的时候也听的懂,然后动手操作的时候我失误了一下...

网友评论

      本文标题:2021-05-24

      本文链接:https://www.haomeiwen.com/subject/lnxfsltx.html