对Fashion._mnist进行10分类ipynb

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'#设置tensorflow的日志级别
from tensorflow.python.platform import build_info

import tensorflow as tf

# 列出所有物理GPU设备  
gpus = tf.config.list_physical_devices('GPU')  
if gpus:  
    # 如果有GPU,设置GPU资源使用率  
    try:  
        # 允许GPU内存按需增长  
        for gpu in gpus:  
            tf.config.experimental.set_memory_growth(gpu, True)  
        # 设置可见的GPU设备(这里实际上不需要,因为已经通过内存增长设置了每个GPU)  
        # tf.config.set_visible_devices(gpus, 'GPU')  
        print("GPU可用并已设置内存增长模式。")  
    except RuntimeError as e:  
        # 虚拟设备未就绪时可能无法设置GPU  
        print(f"设置GPU时发生错误: {e}")  
else:  
    # 如果没有GPU  
    print("没有检测到GPU设备。")

import numpy as np
import matplotlib.pyplot as plt
print(tf.__version__)

fashion_mnist = tf.keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

# 将数据保存到 npz 文件中  
np.savez_compressed('./datasets/fashion_mnist.npz',  
                      train_images=train_images,  
                      train_labels=train_labels,  
                      test_images=test_images,  
                      test_labels=test_labels)

data=np.load('./datasets/fashion_mnist.npz')

train_images = data['train_images']  
train_labels = data['train_labels']  
test_images = data['test_images']  
test_labels = data['test_labels']

print(train_images.shape,train_labels.shape,np.unique(train_labels))

print(train_images.max(),train_images.min())

#数字标签对应的类别
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()

# 将这些值缩小至 0 到 1 之间,然后将其馈送到神经网络模型
#归一化
train_images = train_images / 255.0
test_images = test_images / 255.0

plt.figure(figsize=(10,10))
for i in range(25):#展示25张图片
    plt.subplot(5,5,i+1)#以子视图的形式展示
    plt.xticks([])#不带刻度
    plt.yticks([])
    plt.grid(False) #不带网格
    plt.imshow(train_images[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[train_labels[i]])#显示横轴标签为数字标签对应的真实类别
plt.show()

model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=((28,28))),
    tf.keras.layers.Flatten(),#扁平化处理成行向量
    tf.keras.layers.Dense(128, activation='relu'),#线性转换层
    tf.keras.layers.Dense(10)#输出层,10个分值,对应模型对于输入数据应该属于这10个类别的置信度
])

model.summary()

#优化器,损失函数,指标,这个损失会对标签做类似one-hot的处理
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=10)

test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print('\nTest accuracy:', test_acc)

#模型训练好后可以加一个概率层把logits转换成概率
probability_model = tf.keras.Sequential([model,
                                         tf.keras.layers.Softmax()])

predictions = probability_model.predict(test_images)

np.argmax(predictions[0])#获取其中最大值对应的索引下标

test_labels[0]

def plot_image(i, predictions_array, true_label, img):
  true_label, img = 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):
  true_label = true_label[i]
  plt.grid(False)
  plt.xticks(range(10))
  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, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i],  test_labels)
plt.show()

i = 12
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions[i], test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions[i],  test_labels)
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, predictions[i], test_labels, test_images)
  plt.subplot(num_rows, 2*num_cols, 2*i+2)
  plot_value_array(i, predictions[i], test_labels)
plt.tight_layout()
plt.show()

#最后,使用训练好的模型对单个图像进行预测
img = test_images[1]
print(img.shape)

#在0轴增加1个维度
img = (np.expand_dims(img,0))
print(img.shape)

predictions_single = probability_model.predict(img)
print(predictions_single)

plot_value_array(1, predictions_single[0], test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)
plt.show()

np.argmax(predictions_single[0])

test_labels[1]

相关推荐

  1. Fashion._mnist进行10分类ipynb

    2024-04-10 11:10:01       41 阅读
  2. 007.卷积网络-FashionMNIST-正确率90.180

    2024-04-10 11:10:01       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-10 11:10:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-10 11:10:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-10 11:10:01       87 阅读
  4. Python语言-面向对象

    2024-04-10 11:10:01       96 阅读

热门阅读

  1. js 基础作用域回顾以及数据类型的存放位置

    2024-04-10 11:10:01       35 阅读
  2. 【Python】如何在Ubuntu环境中切换Python版本

    2024-04-10 11:10:01       36 阅读
  3. 每天学习一个Linux命令之watch

    2024-04-10 11:10:01       35 阅读
  4. NIO简介

    2024-04-10 11:10:01       37 阅读
  5. 大语言模型本地化部署思路

    2024-04-10 11:10:01       35 阅读
  6. ARM CoreLink 系列的互连产品包括哪些?

    2024-04-10 11:10:01       31 阅读
  7. vue中使用use引入的svg怎么添加title

    2024-04-10 11:10:01       36 阅读
  8. git-es6-promisem面试

    2024-04-10 11:10:01       31 阅读
  9. ES6 => 箭头函数

    2024-04-10 11:10:01       30 阅读
  10. Hermite 多项式

    2024-04-10 11:10:01       26 阅读