今天我們要實作CapsNet,並與第三天的結果比較。
傳送門
資料集使用fashion_mnist,為Keras內建的資料集。
訓練集為60,000 張28x28 像素灰度圖像,測試集為10,000 同規格圖像,總共10 類時尚物品標籤。該數據集可以用作MNIST 的直接替代品。類別標籤是:
類別 描述 中文
0 T-shirt/top T卹/上衣
1 Trouser 褲子
2 Pullover 套頭衫
3 Dress 連衣裙
4 Coat 外套
5 Sandal 涼鞋
6 Shirt 襯衫
7 Sneaker 運動鞋
8 Bag 背包
9 Ankle boot 短靴
測試的正確率有91.29%。
因為tf2 的版本我嘗試過後無法跑,就換舊版的來執行。code參考這篇實作
https://github.com/XifengGuo/CapsNet-Keras
我們使用Colab來當作我們的實作平台,並使用以下指令還原舊版。
!pip install tensorFlow==1.2
!pip install Keras==2.0.6
因為Keras2.0.6版沒有fashion_mnist,因此要自己先存下來資料集。
from keras.layers import Input, Dense, Conv1D, Conv2D, MaxPooling1D,\
MaxPooling2D, UpSampling1D, UpSampling2D, Dropout, Lambda, Convolution2D,\
Reshape, Activation, Flatten, add, concatenate, BatchNormalization
from keras.models import Model, Sequential
import numpy as np
import keras
nb_classes=10
x_train= np.load(workspace_dir+"/x_train.npy")
y_train=np.load(workspace_dir+"/y_train.npy")
x_test=np.load(workspace_dir+"/x_test.npy")
y_test=np.load(workspace_dir+"/y_test.npy")
x_train = x_train.astype('float32') / 255.0
x_test = x_test.astype('float32') / 255.0
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], x_train.shape[2], 1))
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], x_test.shape[2], 1))
我們需要此檔案來幫助我們建立Primary層與DigitCaps層
https://github.com/XifengGuo/CapsNet-Keras/blob/master/capsulelayers.py
可以依照自己想法建立,這邊我們使用三層的Convolutional層再接到Primary層。
input_shape=(28,28,1)
input = Input(input_shape, name='input')
layer=Conv2D(16, kernel_size=(2, 2), activation='relu', padding='same')(input)
layer=BatchNormalization()(layer)
layer=MaxPooling2D(pool_size=(2, 2))(layer)
layer=Conv2D(32, kernel_size=(2, 2), activation='relu', padding='same')(layer)
layer=BatchNormalization()(layer)
layer=MaxPooling2D(pool_size=(2, 2))(layer)
layer=Conv2D(64, kernel_size=(2, 2), activation='relu', padding='same')(layer)
layer=BatchNormalization()(layer)
layer=MaxPooling2D(pool_size=(2, 2))(layer)
primarycaps = PrimaryCap(layer, dim_capsule=4, n_channels=16, kernel_size=(
2, 2), strides=1, padding='valid')
digitcaps = CapsuleLayer(num_capsule=nb_classes, dim_capsule=4, routings=3, name='digitcaps')(primarycaps)
layer = Dropout(0.5)(digitcaps)
layer = Flatten(name='flatten')(layer)
output = Dense(nb_classes, name="Dense_10nb")(layer)
output=Activation(tf.nn.softmax)(output)
model = Model(inputs=[input], outputs=[output])
model.compile(loss='sparse_categorical_crossentropy',optimizer=keras.optimizers.Adam(lr=0.0001,decay=1e-6),metrics = ['accuracy'])
model.summary()
from random import sample
tag = 'Capsnet0907_fashion_mnist_{}'.format(0)
h5_weight_path = os.path.join(WEIGHT_DIR, './' + tag + '.h5')
nb_epoch=25
batch_size=150
model.fit(x=x_train, y=y_train,batch_size=batch_size, epochs=nb_epoch,verbose=1,validation_data=(x_test, y_test))
#儲存權重
model.save_weights(os.path.join(WEIGHT_DIR, tag + ".h5"))
model.load_weights(h5_weight_path)
score, acc = model.evaluate([x_test], [y_test])
print(' score= ', score, " acc= ", acc)
predict = model.predict(x_test)
9984/10000 [============================>.] - ETA: 0s score= 0.29407105964422225 acc= 0.8955
正確率有89.55%,略為上次的低。
因為使用舊版本Tensorflow,所以無法在Colab上使用GPU,因此訓練速度慢上很多,因此無法多次嘗試不同的Model,正確率就較上次的低。