TensorFlow 2.0學習筆記(1):服飾影像分類

安裝套件

pip install -U tensorflow_datasets #用來下載、讀取dataset

載入套件

import tensorflow as tf

本次的資料集選用Fashion MNIST dataset,該資料有70,000張灰階圖片,分成10個類別,28x28的圖片尺寸

載入資料

dataset, metadata = tfds.load('fashion_mnist', as_supervised=True, with_info=True)

建立標籤

class_names = ['T-shirt/top','Trouser','Pullover','Dress', 'Coat','Sandal','Shirt','Sneaker','Bag','Ankle boot']

檢視資料

num_train_examples = metadata.splits['train'].num_examples
num_test_examples = metadata.splits['test'].num_examples
print("Number of training examples: {}".format(num_train_examples))
print("Number of test examples: {}".format(num_test_examples))

預處理資料

#正規化將pixel從[0~255]變成[0~1]之間的範圍

檢視圖片

# 將圖片移除顏色

建立模型

model = tf.keras.Sequential([

編譯模型

model.compile(optimizer='adam',loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),metrics=['accuracy'])

訓練模型

BATCH_SIZE = 32

評估準確度

test_loss, test_accuracy = model.evaluate(test_dataset, steps=math.ceil(num_test_examples/32))

進行預測

for test_images, test_labels in test_dataset.take(1):

Written by

Machine Learning / Deep Learning / Python / Flutter cakeresume.com/yanwei-liu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store