Trial 1: Fashion MNIST dengan CNN

Setelah bergabung dengan group WA INAPR, group nya orang Indonesia yang tertarik bidang pattern recognition, saya menemukan hal-hal baru. Salah satunya adalah dataset Fashion MNIST dari Zalando, sebuah perusahaan fashion dari Jerman. Ketika ikut serta ICCV 2017 di Venice bulan lalu, saya juga hadir di workshop “Computer Vision for Fashion” yang diselenggarakan oleh Zalando ini. Tau Zalando ini ga sengaja juga, pergi ke booth mereka karena pengen kaos gratis. Maklum mahasiswa ­čśÇ

Sedikit informasi, Fashion MNIST ini karakteristiknya sama dengan MNIST dataset yang luar biasa terkenal. Orang yang baru belajar pattern recognition dan computer vision pasti pakai dataset ini ketika pertama kali simulasi. Jadi saya skip aja penjelasan panjangnya.

Berikut simulasi sederhana Fashion MNIST dengan CNN menggunaka Keras backend Tensor Flow.

  1. Load data Fashion MNIST
    link untuk mengunduh data:
    http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz
    http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-labels-idx1-ubyte.gz
    http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-images-idx3-ubyte.gz
    http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/t10k-labels-idx1-ubyte.Setelah didownload, simpan dataset di folder data (untuk kasus saya)

    import gzip
    import idx2numpy
    
    def load_data():
    with gzip.open('./data/train-images-idx3-ubyte.gz', 'rb') as f:
    x_train = idx2numpy.convert_from_string(f.read())
    
    with gzip.open('./data/train-labels-idx1-ubyte.gz', 'rb') as f:
    y_train = idx2numpy.convert_from_string(f.read())
    
    with gzip.open('./data/t10k-images-idx3-ubyte.gz', 'rb') as f:
    x_test = idx2numpy.convert_from_string(f.read())
    
    with gzip.open('./data/t10k-labels-idx1-ubyte.gz', 'rb') as f:
    y_test = idx2numpy.convert_from_string(f.read())
    
    return (x_train, y_train), (x_test, y_test)
    
  2. Membangun architecture
# Load Data
(x_train, y_train), (x_test, y_test) = load_data()
# Input keras library
import keras
import numpy
from matplotlib import pyplot
from keras.models import Sequential
from keras.layers import Dense, Flatten, Dropout
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K

# parameters
batch_size = 128
num_classes = 10
epochs = 12

# input image dimensions
img_rows, img_cols = 28, 28

# Backend options
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)

# Data normalization
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /=255
x_test /=255

# Print input samples
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

# convert class vectors to binary class matricess
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

# build simple CNN model
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=input_shape))
model.add(Conv2D(64, (3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.summary()

model.compile(loss=keras.losses.categorical_crossentropy, optimizer=keras.optimizers.Adadelta(), metrics=['accuracy'])

model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test))

score = model.evaluate(x_test, y_test, verbose=0)

print('Test loss:', score[0])
print('Test accuracy:', score[1])

Hasil simulasi di atas
(‘Test loss:’, 0.23328696813583374)
(‘Test accuracy:’, 0.91510000000000002)

That’s it! Mudah kan? Selamat mencoba. Next saya coba pakai sequence model, RNN!

 

Advertisements