CODEARTS IDE ONLINE-导入和预处理训练数据集

时间:2024-07-19 11:30:50

导入和预处理训练数据集

参考TensorFlow官网的教程,创建一个简单的图片分类模型。

查看当前TensorFlow版本,单击或者敲击Shift+Enter运行cell。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from __future__ import absolute_import, division, print_function, unicode_literals

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
# print tensorflow version
print(tf.__version__)

下载Fashion MNIST图片数据集,该数据集包含了10个类型共60000张训练图片以及10000张测试图片。

1
2
3
# download Fashion MNIST dataset
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

对训练数据做预处理,并查看训练集中最开始的25个图片。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
# preprocessing
train_images = train_images / 255.0
test_images = test_images / 255.0
# display first 25 images
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_images[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[train_labels[i]])
plt.show()

support.huaweicloud.com/bestpractice-cloudide/develop_ai_import_preprocess.html