• TensorFlow图像分类


    参考文章:https://zhuanlan.zhihu.com/p/59506238

    import tensorflow as tf
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    (train_image,train_label),(test_image,test_label) = tf.keras.datasets.fashion_mnist.load_data()
    class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
    train_image = train_image/255
    test_image = test_image/255
    #显示数据
    plt.figure(figsize=(10,10))
    for i in range(25):
        plt.subplot(5,5,i+1)
        plt.xticks([])
        plt.yticks([])
        plt.grid(False)
        plt.imshow(train_image[i], cmap=plt.cm.binary)
        plt.xlabel(class_names[train_label[i]])
    plt.show()
    #构建网络
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
    model.add(tf.keras.layers.Dense(128,activation='relu'))
    model.add(tf.keras.layers.Dense(10,activation='softmax'))
    #预测
    model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['acc'])
    model.fit(train_image,train_label,epochs=5)
    print(model.evaluate(test_image,test_label))
    predict = model.predict(test_image)
    print(np.argmax(predict[0]))
    print(test_label[0])
    #显示单张图片
    def plot_image(i, predictions_array, true_label, img):
      predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
      plt.grid(False)
      plt.xticks([])
      plt.yticks([])
      plt.imshow(img, cmap=plt.cm.binary)
      predicted_label = np.argmax(predictions_array)
      if predicted_label == true_label:
        color = 'blue'
      else:
        color = 'red'
      plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                    100*np.max(predictions_array),
                                    class_names[true_label]),
                                    color=color)
    #显示数组全部图片
    def plot_value_array(i, predictions_array, true_label):
      predictions_array, true_label = predictions_array[i], true_label[i]
      plt.grid(False)
      plt.xticks([])
      plt.yticks([])
      thisplot = plt.bar(range(10), predictions_array, color="#777777")
      plt.ylim([0, 1])
      predicted_label = np.argmax(predictions_array)
      thisplot[predicted_label].set_color('red')
      thisplot[true_label].set_color('blue')
    i = 0
    plt.figure(figsize=(6,3))
    plt.subplot(1,2,1)
    plot_image(i, predict, test_label, test_image)
    plt.subplot(1,2,2)
    plot_value_array(i, predict,  test_label)
    plt.show()
    #可视化结果
    num_rows = 5
    num_cols = 3
    num_images = num_rows*num_cols
    plt.figure(figsize=(2*2*num_cols, 2*num_rows))
    for i in range(num_images):
      plt.subplot(num_rows, 2*num_cols, 2*i+1)
      plot_image(i, predict, test_label, test_image)
      plt.subplot(num_rows, 2*num_cols, 2*i+2)
      plot_value_array(i, predict, test_label)
    plt.show()
    
    img = test_image[0]
    img = (np.expand_dims(img,0))
    print(img.shape)
    predictions_single = model.predict(img)
    print(predictions_single)
    plot_value_array(0, predictions_single, test_label)
    _ = plt.xticks(range(10), class_names, rotation=45)
    (1, 28, 28)

     

     

  • 相关阅读:
    npm查找全局安装包命令
    sublime3添加对react代码检查
    Android开发——搭建最新版本的Android开发环境
    git无法定位程序输入点libiconv
    Java学习----finally块
    Java学习----有风险的代码(异常)
    Java学习----集合函数
    Java学习----Math函数
    Java学习----日期函数
    Java学习----字符串函数
  • 原文地址:https://www.cnblogs.com/xhj1074376195/p/14299793.html
Copyright © 2020-2023  润新知