ใช้ Tensorflow จำแนกข้อมูลด้วยเทคนิค Neural network

easywebsify
4 min readAug 12, 2018

ก่อนอื่นต้องบอกเลยว่าผมเป็น web developer มีความรู้เรื่อง Machine learning (ML) เบื้องต้นเท่านั้น แต่เนื่องจากมีความจำเป็นต้องใช้ ML ในการแข่ง hackathon รายการนึง ผมเลยเข้ามาทำความรู้จักกับ Tensorflow …

ในบทความนี้เป็นภาพรวมของการใช้ Tensoflow จำแนกข้อมูลชุดแต่งกาย โดยเราจะมีการสร้างสร้างโมเดล และใช้โมเดลที่สร้างจำแนกข้อมูลเสื้อผ้า รองเท้า ถ้าเปรียบเทียบกับการเขียนโปรแกรม นี่เปรียบเสมือนเพียงการเขียน Hello world

ข้อมูลจะใช้ dataset Fashion MNIST โดยจะมี 70000 รูป และแบ่งเป็น 10 ประเภท แต่ละรูปมีความละเอียด 28x28 pixels

Fashion-MNIST samples (by Zalando, MIT License).

ข้อมูลทั้งหมดจาก 70,000 เราจะแบ่งข้อมูลสำหรับใช้ train โมเดล 60000 รูป และใช้ test (ทดสอบความถูกต้องของโมเดล) 10,000 รูป

Fashion MNIST เราสามารถนำมาใช้งานได้ผ่าน tensorflow เลยด้วย เพราะชุดข้อมูลนี้เป็นชุดข้อมูลเริ่มต้นที่เอาไว้สำหรับให้เรามาลองเล่นกับอัลกอริทึมของเรา และเอาไว้สำหรับให้เราเริ่มต้นทดลองทำ ไว้เทส และดีบัคโค้ด

ต่อไปเราจะทำการเตรียมข้อมูลกันนะครับ โดยชุดโค้ดข้างล่างนี้จะแบ่งออกเป็น 3 เรื่องคือ 1 import libs 2 โหลดข้อมูล และ 3 สำรวจข้อมูล

# 1 Import
import tensorflow as tf
from tensorflow import keras
# Helper libraries
import numpy as np
import matplotlib.pyplot as plt
# 2 Download data
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()# 3 Explore the data
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.gca().grid(False)
plt.figure()
plt.imshow(train_images[26])
plt.colorbar()
plt.gca().grid(False)

จากรูปจะเราจะเห็นว่าค่าของ pixel จะอยู่ในช่วง 0–255 เราจะทำการแปลงข้อมูล pixel ให้อยู่ในช่วง 0–1 ทั้งข้อมูลสำหรับ train และ test

train_images = train_images / 255.0

test_images = test_images / 255.0

จากนั้นลองสำรวจรูปดูอีกครั้ง

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

ขั้นตอนต่อมาเราจะ map แต่ละ label ให้เป็นชื่อตามรูปที่แสดง ละรูปจะมีได้ 1 label โดยที่มีทั้งหมด 10 label มีค่าคือ 0–9 ดังนั้นเราจะกำหนดชื่อให้แต่ละ label ด้วยการสร้าง class_names ไว้ map กับ label ของข้อมูล

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

ต่อมาเราจะลองดูว่าที่เรา map label ให้เป็นชื่อนั้น ผลเป็นอย่างไร เราจะลองแสดงรูปจำนวนนึงพร้อมชื่อ (map label)

import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(12,12))
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]])

จบขั้นตอนนี้เป็นเพียงส่วนของการเตรียมและตรวจสอบข้อมูล ขั้นตอนต่อไปเราจะทำการสร้าง model

การสร้างโมเดล neural network

เราจะใช้เทคนิค neural network ซึ่งเราจะต้องทำการตั้งค่า layers และการ compile โมเดล

model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer=tf.train.AdamOptimizer(),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

หลังจากตั้งค่าให้ model แล้วเราจะใช้ .fit เพื่อเป็นการ train ตัว model ซึ่งจะต้องส่งชุด train_images, train_labels และ epochs

model.fit(train_images, train_labels, epochs=10)# output
# Epoch 1/10
# 60000/60000 [==============================] - 3s 43us/step - loss: 0.5000 - acc: 0.8246
# Epoch 2/10
# 60000/60000 [==============================] - 2s 40us/step - loss: 0.3776 - acc: 0.8632
# Epoch 3/10
# 60000/60000 [==============================] - 2s 40us/step - loss: 0.3402 - acc: 0.8769
# Epoch 4/10
# 60000/60000 [==============================] - 2s 41us/step - loss: 0.3132 - acc: 0.8850
# Epoch 5/10
# 60000/60000 [==============================] - 2s 40us/step - loss: 0.2937 - acc: 0.8929
# Epoch 6/10
# 60000/60000 [==============================] - 2s 41us/step - loss: 0.2801 - acc: 0.8957
# Epoch 7/10
# 60000/60000 [==============================] - 3s 43us/step - loss: 0.2700 - acc: 0.8993
# Epoch 8/10
# 60000/60000 [==============================] - 2s 39us/step - loss: 0.2571 - acc: 0.9037
# Epoch 9/10
# 60000/60000 [==============================] - 2s 38us/step - loss: 0.2461 - acc: 0.9077
# Epoch 10/10
# 60000/60000 [==============================] - 2s 39us/step - loss: 0.2375 - acc: 0.9108

As the model trains, the loss and accuracy metrics are displayed. This model reaches an accuracy of about 0.88 (or 88%) on the training data.

ในการ trains จะมีผลแสดงความผิดพลาด (loss) และความถูกต้อง (accuracy) จากการเทรนตามตัวอย่างนี้ แสดงรอบสุดท้ายเราได้ความถูกต้อง 0.91 หรือ 91%

จากเทรนโมเดลเสร็จแล้ว เราจะนำชุดข้อมูล 10,000 รูป ที่เตรียมไว้สำหรับ test ตัวโมเดล จะใช้ .evaluate เพื่อทดสอบผลกับข้อมูล test

test_loss, test_acc = model.evaluate(test_images, test_labels)print('Test accuracy:', test_acc)# output
# 10000/10000 [==============================] - 0s 21us/step
# ('Test accuracy:', 0.8899)

ผลจากการ test โมเดล เราได้ค่าความถูกต้อง 88%

หลังจาก trains โมเดลและ test โมเดลแล้ว เราสามารถลองทดสอบโมเดลกับแต่ละรูปได้ จากโค้ดข้างล่างเราจะลองดูผล predict ของโมเดลที่ 100 (index 99) จากโค้ดตามนี้

predictions = model.predict(test_images)
predicted = (predictions[99])
plt.figure()
plt.imshow(test_images[99])
class_names[np.argmax(predicted)]# output
# 'Pullover'

หรือเราจะแสดงผลการ predict หลายๆรูปเลย ตามนี้

plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid('off')
plt.imshow(test_images[i], cmap=plt.cm.binary)
predicted_label = np.argmax(predictions[i])
true_label = test_labels[i]
if predicted_label == true_label:
color = 'green'
else:
color = 'red'
plt.xlabel("{} ({})".format(class_names[predicted_label],
class_names[true_label]),
color=color)

Output ตามรูปข้างบน รูปที่เป็นสีแดง คือรูปที่ predict ผิดพลาด ซึ่งผลลัพธ์ที่ถูกต้องจะอยู่ในวงเล็บครับ

Credit https://www.tensorflow.org/tutorials/keras/basic_classification

จากการศึกษา tensorflow วันแรกของผม ก็ยังไม่สามารถลงรายละเอียดบางส่วนได้ สามารถอธิบายได้แต่ภาพรวมการใช้งาน tensorflow แต่ผมจะหาเวลารีบทำความเข้าใจในเร็วๆนี้ ถ้ามีคำถามอะไรสามารถถามได้เลยครับ

--

--

easywebsify

Web developer, Full stack developer, React, Nextjs, Nodejs and Golang