Introduction to Computer Vision(1.5hrs/7hrs)
課堂目標要訓練一個fashion MNIST, 用來辨識衣著種類的MNIST (70000x28x28)的圖庫
相較於簡單的MNIST數字集, 提出了使用fashion MNIST來預測該圖片是鞋子還是衣服還是褲子之類的問題
一樣是28x28但是圖片變得比較複雜
課程給出的程式碼如下
跟第一個禮拜的課程相比增加了loaddata, normalize data, callback的技巧
import keras
import tensorflow as tf
mnist = tf.keras.datasets.fashion_mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
import matplotlib.pyplot as plt
# plt.imshow(training_images[0])
# plt.show()
training_images = training_images / 255
test_images = test_images / 255
model = keras.Sequential([keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(64, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)])
model.compile(optimizer=tf.optimizers.Adam(),
loss='sparse_categorical_crossentropy')
model.fit(training_images, training_labels, epochs=500)
acc = model.evaluate(test_images, test_labels)
print(acc)
最後教導我們怎麼使用callback來提前停止訓練,
先建立一個Callback function繼承tf.keras.callbacks.Callback
建立一個myCallback物件, 最後把他加入在fit裡面
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
if (logs.get("loss")<0.4):
print("\nLoss is low so cancelling training!")
self.model.stop_training = True
callbacks = myCallback()
model.fit(training_images, training_labels, epochs=500, callbacks=[callbacks])
第二周的Quiz也不難, Exercise是利用先前學到的東西在10個epoch內把手寫數字MNIST訓練到99%
確保掌握了loaddata, normalize data, callback的技巧
對於我自己來說是學到了callback這個物件的用法, 覺得對於實際應用也算很實用
花了差不多1.5hrs就結束課程了
Enhancing Vision with Convolutional Neural Networks(2hrs/8hrs)
主要在學習使用CNN來做圖像辨識
先前的NN會發現訓練很不錯, 但是實際Test的時候準確率卻偏低
卷積主要做的是讓機器自動去學習我們傳統在做影像處理的filter kernel, 以達到提取feature的作用
Pooling主要是壓縮圖像的方式
介紹了Conv2D 3x3會在x,y方向減少2個pixel, 5x5會在x,y方向減少4個pixel
Pooling 2x2則是會減少1/2的pixel
要特別注意使用Conv2D需要把input加入channel, 所以需要reshape
import tensorflow as tf
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
if (logs.get("loss")<0.4):
print("\nLoss is low so cancelling training!")
self.model.stop_training = True
callbacks = myCallback()
mnist = tf.keras.datasets.fashion_mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
import matplotlib.pyplot as plt
# plt.imshow(training_images[0])
# plt.show()
training_images = training_images / 255
test_images = test_images / 255
training_images = training_images.reshape(training_images.shape[0], training_images.shape[1], training_images.shape[2], 1)
test_images = test_images.reshape(test_images.shape[0], test_images.shape[1], test_images.shape[2], 1)
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(64, (3,3), activation="relu", input_shape=(28, 28, 1)),
tf.keras.layers.MaxPool2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation="relu"),
tf.keras.layers.MaxPool2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)])
model.summary()
model.compile(optimizer=tf.optimizers.Adam(),
loss='sparse_categorical_crossentropy')
model.fit(training_images, training_labels, epochs=500, callbacks=[callbacks])
acc = model.evaluate(test_images, test_labels)
print(acc)
課堂上展示了透過convolution + maxpooling不只找出特徵, 而且還強化特徵的突出
第三周的Quiz也並不難, Exercise也不難, 但我覺得課程中間可視化有點困難, 但是應該是值得學習的
第三周大概花了2hrs, 練習到這裡發現, 其實大多並不難, 比較要特別注意的是幾個2.x版本跟1.x版本的不同 (尤其是keras)