やってみよう!

プログラミングとかでぃーぷらーにんぐとかVRとか気になったものをやってみる予定

画像分類メモ

続き

blog.ascreit.com

オートエンコーダから一旦離れて普通にCNN(facenet)でやってみたけどあまりうまく行かなかった

CNNでやってみる。

前回オートエンコーダVAEでやってみて、やっぱり教師データつけて「こんな感じの分類してよ」って教えてあげないと無理かなと思ったので、CVAEだとかAAEだとかで半教師学習を試そうとしてみたけど理論の理解が不十分でうまく実装できなかった。

教師あり学習のCVAEだとかAAEだとかは実装落ちててトレーニングもうまく行くっぽいけど、やりたいことは「データセットのラベル付けが面倒だから簡略化したい」ということであって、大量の教師データなんて無いというか教師データを作ることが目的だからせめて半教師あり学習でやりたい。

じゃあ半教師あり学習ってどうやって実装するの?ってところが分からなくて一旦挫折します。

かわりに画像分類といえばCNNでしょ。ということでCNNでやってみました。

学習済みモデルを探す

やりたいこととしては

青山ブルーマウンテン

f:id:ascreit:20181014141612j:plain

チノちゃん

f:id:ascreit:20181014141738j:plain

マヤさん

f:id:ascreit:20181014141759j:plain

とこんな感じの画像をキャラごとに分類してフォルダ分けしてほしいということなので人の顔判別モデルとかがあれば使えるのではないかということで探してみると

データセットとしてはVGGFace2というのがあった。

これを自前で頑張って学習させてもいいのですが、流石にそれは設備的に辛いので誰か優しい人がVGGFace2を使って学習させたモデルを公開していないか探したところ

github.com

ここにTensorFlowでFacenetっていうネットワークを使ってVGGFace2を学習させたモデルを公開してるっぽい。

でも、ナマのTensorFlowって使ったこと無いからKerasで無いかなと探してみたらあったので、これを使ってみることに。 Face Recognition with FaceNet in Keras - Sefik Ilkin Serengil

ネットワークを見てみる

よくわからないけど学習済みモデルの最後の部分をsoftmaxで10クラスに分類するようにすればオッケーなんだろ?という安易な考えの元、まずはfacenetとやらのsummary() を見てみます。

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import *
# from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.preprocessing.image import array_to_img, img_to_array,  load_img
import numpy as np
from sklearn.model_selection import train_test_split

import os,glob


#facenet model structure: https://github.com/serengil/tensorflow-101/blob/master/model/facenet_model.json
facenet_model = model_from_json(open("model/facenet_model.json", "r").read())

#pre-trained weights https://drive.google.com/file/d/1971Xk5RwedbudGgTIrGAL4F7Aifu7id1/view?usp=sharing
facenet_model.load_weights('weight/facenet_weights.h5')

facenet_model.summary()

これを実行すると

Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            (None, 160, 160, 3)  0                                            
__________________________________________________________________________________________________
Conv2d_1a_3x3 (Conv2D)          (None, 79, 79, 32)   864         input_1[0][0]                    
__________________________________________________________________________________________________
Conv2d_1a_3x3_BatchNorm (BatchN (None, 79, 79, 32)   96          Conv2d_1a_3x3[0][0]              
__________________________________________________________________________________________________
Conv2d_1a_3x3_Activation (Activ (None, 79, 79, 32)   0           Conv2d_1a_3x3_BatchNorm[0][0]    
__________________________________________________________________________________________________
Conv2d_2a_3x3 (Conv2D)          (None, 77, 77, 32)   9216        Conv2d_1a_3x3_Activation[0][0]   
__________________________________________________________________________________________________
Conv2d_2a_3x3_BatchNorm (BatchN (None, 77, 77, 32)   96          Conv2d_2a_3x3[0][0]              
__________________________________________________________________________________________________
Conv2d_2a_3x3_Activation (Activ (None, 77, 77, 32)   0           Conv2d_2a_3x3_BatchNorm[0][0]    
__________________________________________________________________________________________________
Conv2d_2b_3x3 (Conv2D)          (None, 77, 77, 64)   18432       Conv2d_2a_3x3_Activation[0][0]   
__________________________________________________________________________________________________
Conv2d_2b_3x3_BatchNorm (BatchN (None, 77, 77, 64)   192         Conv2d_2b_3x3[0][0]              
__________________________________________________________________________________________________
Conv2d_2b_3x3_Activation (Activ (None, 77, 77, 64)   0           Conv2d_2b_3x3_BatchNorm[0][0]    
_________________________________________________________________________________________________




(中略!!!!!)



______________________
Block8_6_Branch_1_Conv2d_0a_1x1 (None, 3, 3, 192)    344064      Block8_5_Activation[0][0]        
__________________________________________________________________________________________________
Block8_6_Branch_1_Conv2d_0a_1x1 (None, 3, 3, 192)    576         Block8_6_Branch_1_Conv2d_0a_1x1[0
__________________________________________________________________________________________________
Block8_6_Branch_1_Conv2d_0a_1x1 (None, 3, 3, 192)    0           Block8_6_Branch_1_Conv2d_0a_1x1_B
__________________________________________________________________________________________________
Block8_6_Branch_1_Conv2d_0b_1x3 (None, 3, 3, 192)    110592      Block8_6_Branch_1_Conv2d_0a_1x1_A
__________________________________________________________________________________________________
Block8_6_Branch_1_Conv2d_0b_1x3 (None, 3, 3, 192)    576         Block8_6_Branch_1_Conv2d_0b_1x3[0
__________________________________________________________________________________________________
Block8_6_Branch_1_Conv2d_0b_1x3 (None, 3, 3, 192)    0           Block8_6_Branch_1_Conv2d_0b_1x3_B
__________________________________________________________________________________________________
Block8_6_Branch_0_Conv2d_1x1 (C (None, 3, 3, 192)    344064      Block8_5_Activation[0][0]        
__________________________________________________________________________________________________
Block8_6_Branch_1_Conv2d_0c_3x1 (None, 3, 3, 192)    110592      Block8_6_Branch_1_Conv2d_0b_1x3_A
__________________________________________________________________________________________________
Block8_6_Branch_0_Conv2d_1x1_Ba (None, 3, 3, 192)    576         Block8_6_Branch_0_Conv2d_1x1[0][0
__________________________________________________________________________________________________
Block8_6_Branch_1_Conv2d_0c_3x1 (None, 3, 3, 192)    576         Block8_6_Branch_1_Conv2d_0c_3x1[0
__________________________________________________________________________________________________
Block8_6_Branch_0_Conv2d_1x1_Ac (None, 3, 3, 192)    0           Block8_6_Branch_0_Conv2d_1x1_Batc
__________________________________________________________________________________________________
Block8_6_Branch_1_Conv2d_0c_3x1 (None, 3, 3, 192)    0           Block8_6_Branch_1_Conv2d_0c_3x1_B
__________________________________________________________________________________________________
Block8_6_Concatenate (Concatena (None, 3, 3, 384)    0           Block8_6_Branch_0_Conv2d_1x1_Acti
                                                                 Block8_6_Branch_1_Conv2d_0c_3x1_A
__________________________________________________________________________________________________
Block8_6_Conv2d_1x1 (Conv2D)    (None, 3, 3, 1792)   689920      Block8_6_Concatenate[0][0]       
__________________________________________________________________________________________________
lambda_21 (Lambda)              (None, 3, 3, 1792)   0           Block8_6_Conv2d_1x1[0][0]        
__________________________________________________________________________________________________
add_21 (Add)                    (None, 3, 3, 1792)   0           Block8_5_Activation[0][0]        
                                                                 lambda_21[0][0]                  
__________________________________________________________________________________________________
AvgPool (GlobalAveragePooling2D (None, 1792)         0           add_21[0][0]                     
__________________________________________________________________________________________________
Dropout (Dropout)               (None, 1792)         0           AvgPool[0][0]                    
__________________________________________________________________________________________________
Bottleneck (Dense)              (None, 128)          229376      Dropout[0][0]                    
__________________________________________________________________________________________________
Bottleneck_BatchNorm (BatchNorm (None, 128)          384         Bottleneck[0][0]                 
==================================================================================================
Total params: 22,808,144
Trainable params: 22,779,312
Non-trainable params: 28,832

長いので中略しましたが、こんな感じの結果が出ます。

最後が(None, 128) なので、これに分類機繋いで10クラスに分類すればイケるのでは?

ということでやってみます。

Training

'''Trains a simple convnet on the MNIST dataset.

Gets to 99.25% test accuracy after 12 epochs
(there is still a lot of margin for parameter tuning).
16 seconds per epoch on a GRID K520 GPU.
'''

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import *
# from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
from keras.preprocessing.image import array_to_img, img_to_array,  load_img
import numpy as np
from sklearn.model_selection import train_test_split

import os,glob

class Train():

    def __init__(self):
        self.batch_size = 10
        self.num_classes = 10
        self.epochs = 100
        # input image dimensions
        self.img_rows, self.img_cols, self.img_color = 160, 160, 3
        self.input_shape = (self.img_rows, self.img_cols, self.img_color)

    def load_data(self):
        filename = ("labeld_data")
        x=[]
        y=[]
        labels = os.listdir(filename)
        for i,label in enumerate(labels):
            f = glob.glob(filename + "/" + label + "/*.jpg")
            for file in f:
                img = img_to_array(load_img(file, target_size=self.input_shape))
                x.append(img)
                y.append(i)
        x = np.asarray(x, 'float32') / 255.0
        y = np.asarray(y)
        y =  keras.utils.to_categorical(y, len(labels))

        self.labels = labels

        # 学習用データとテストデータに分ける
        x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1, random_state=111)
        return (x_train, y_train), (x_test, y_test)


    def make_model(self):
        #facenet model structure: https://github.com/serengil/tensorflow-101/blob/master/model/facenet_model.json
        facenet_model = model_from_json(open("model/facenet_model.json", "r").read())

        #pre-trained weights https://drive.google.com/file/d/1971Xk5RwedbudGgTIrGAL4F7Aifu7id1/view?usp=sharing
        facenet_model.load_weights('weight/facenet_weights.h5')

        facenet_model.summary()

        top_model = Sequential()
        print(facenet_model.output_shape)
        # top_model.add(Flatten(input_shape=facenet_model.output_shape[1:]))
        top_model.add(Dense(128, activation='relu'))
        top_model.add(Dropout(0.5))
        top_model.add(Dense(10, activation='softmax'))
        model = Model(input=facenet_model.input, output=top_model(facenet_model.output))
        model.summary()


        return model

    def train(self):
        (x_train, y_train), (x_test, y_test) = self.load_data()
        model = self.make_model()

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

        model.fit(x_train, y_train,
                  batch_size=self.batch_size,
                  epochs=self.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])
        model.save("my_model.h5")

if __name__ == '__main__':
    t=Train()
    t.train()
Bottleneck_BatchNorm (BatchNorm (None, 128)          384         Bottleneck[0][0]                 
__________________________________________________________________________________________________
sequential_1 (Sequential)       (None, 10)           17802       Bottleneck_BatchNorm[0][0]       
==================================================================================================
Total params: 22,825,946
Trainable params: 22,797,114
Non-trainable params: 28,832

モデルは最後に10クラスに分類する層を1層加えただけ。

とりあえず100回回してみる

Epoch 1/100
75/75 [==============================] - 15s 205ms/step - loss: 2.8058 - acc: 0.1733 - val_loss: 2.7144 - val_acc: 0.1111
Epoch 2/100
75/75 [==============================] - 1s 9ms/step - loss: 2.0051 - acc: 0.3200 - val_loss: 2.2565 - val_acc: 0.4444
Epoch 3/100
75/75 [==============================] - 1s 9ms/step - loss: 1.1711 - acc: 0.6000 - val_loss: 1.8769 - val_acc: 0.3333
Epoch 4/100
75/75 [==============================] - 1s 9ms/step - loss: 0.7855 - acc: 0.7867 - val_loss: 1.6712 - val_acc: 0.5556
Epoch 5/100
75/75 [==============================] - 1s 9ms/step - loss: 0.5127 - acc: 0.8800 - val_loss: 1.5065 - val_acc: 0.4444
Epoch 6/100
75/75 [==============================] - 1s 9ms/step - loss: 0.5580 - acc: 0.8000 - val_loss: 1.8304 - val_acc: 0.4444
Epoch 7/100
75/75 [==============================] - 1s 9ms/step - loss: 0.2688 - acc: 0.9600 - val_loss: 1.6407 - val_acc: 0.4444
Epoch 8/100
75/75 [==============================] - 1s 9ms/step - loss: 0.3237 - acc: 0.9467 - val_loss: 1.8080 - val_acc: 0.4444
Epoch 9/100
75/75 [==============================] - 1s 9ms/step - loss: 0.2902 - acc: 0.9333 - val_loss: 1.5757 - val_acc: 0.4444
Epoch 10/100
75/75 [==============================] - 1s 9ms/step - loss: 0.1205 - acc: 1.0000 - val_loss: 1.5502 - val_acc: 0.4444
Epoch 11/100
75/75 [==============================] - 1s 9ms/step - loss: 0.1448 - acc: 0.9733 - val_loss: 1.5246 - val_acc: 0.5556
Epoch 12/100
75/75 [==============================] - 1s 9ms/step - loss: 0.2240 - acc: 0.9333 - val_loss: 1.3222 - val_acc: 0.4444
Epoch 13/100
75/75 [==============================] - 1s 9ms/step - loss: 0.1320 - acc: 0.9867 - val_loss: 1.3937 - val_acc: 0.4444
Epoch 14/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0757 - acc: 1.0000 - val_loss: 1.4126 - val_acc: 0.4444
Epoch 15/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0566 - acc: 1.0000 - val_loss: 1.3156 - val_acc: 0.5556
Epoch 16/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0851 - acc: 0.9867 - val_loss: 1.5393 - val_acc: 0.4444
Epoch 17/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0571 - acc: 1.0000 - val_loss: 1.5832 - val_acc: 0.4444
Epoch 18/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0444 - acc: 1.0000 - val_loss: 1.7227 - val_acc: 0.4444
Epoch 19/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0659 - acc: 1.0000 - val_loss: 1.4529 - val_acc: 0.4444
Epoch 20/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0636 - acc: 1.0000 - val_loss: 1.3958 - val_acc: 0.5556
Epoch 21/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0276 - acc: 1.0000 - val_loss: 1.2443 - val_acc: 0.4444
Epoch 22/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0788 - acc: 0.9733 - val_loss: 1.2530 - val_acc: 0.5556
Epoch 23/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0558 - acc: 1.0000 - val_loss: 1.3870 - val_acc: 0.4444
Epoch 24/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0308 - acc: 1.0000 - val_loss: 1.4961 - val_acc: 0.4444
Epoch 25/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0831 - acc: 0.9867 - val_loss: 1.7681 - val_acc: 0.5556
Epoch 26/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0540 - acc: 0.9867 - val_loss: 1.8192 - val_acc: 0.4444
Epoch 27/100
75/75 [==============================] - 1s 9ms/step - loss: 0.1100 - acc: 0.9600 - val_loss: 1.6546 - val_acc: 0.5556
Epoch 28/100
75/75 [==============================] - 1s 9ms/step - loss: 0.1447 - acc: 0.9867 - val_loss: 1.1660 - val_acc: 0.5556
Epoch 29/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0830 - acc: 0.9867 - val_loss: 1.5166 - val_acc: 0.4444
Epoch 30/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0648 - acc: 0.9867 - val_loss: 1.9588 - val_acc: 0.4444
Epoch 31/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0890 - acc: 0.9733 - val_loss: 2.6727 - val_acc: 0.3333
Epoch 32/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0823 - acc: 0.9867 - val_loss: 2.8407 - val_acc: 0.3333
Epoch 33/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0330 - acc: 1.0000 - val_loss: 2.8952 - val_acc: 0.5556
Epoch 34/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0372 - acc: 0.9867 - val_loss: 2.5468 - val_acc: 0.4444
Epoch 35/100
75/75 [==============================] - 1s 9ms/step - loss: 0.1286 - acc: 0.9733 - val_loss: 2.4279 - val_acc: 0.4444
Epoch 36/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0817 - acc: 0.9733 - val_loss: 2.4916 - val_acc: 0.5556
Epoch 37/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0369 - acc: 1.0000 - val_loss: 2.4227 - val_acc: 0.4444
Epoch 38/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0987 - acc: 0.9600 - val_loss: 2.1855 - val_acc: 0.4444
Epoch 39/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0683 - acc: 0.9867 - val_loss: 2.5118 - val_acc: 0.2222
Epoch 40/100
75/75 [==============================] - 1s 9ms/step - loss: 0.1048 - acc: 0.9600 - val_loss: 1.7527 - val_acc: 0.5556
Epoch 41/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0425 - acc: 0.9867 - val_loss: 1.3902 - val_acc: 0.6667
Epoch 42/100
75/75 [==============================] - 1s 9ms/step - loss: 0.1487 - acc: 0.9333 - val_loss: 1.6396 - val_acc: 0.5556
Epoch 43/100
75/75 [==============================] - 1s 9ms/step - loss: 0.2084 - acc: 0.9333 - val_loss: 1.0733 - val_acc: 0.5556
Epoch 44/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0650 - acc: 0.9867 - val_loss: 1.2213 - val_acc: 0.4444
Epoch 45/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0188 - acc: 1.0000 - val_loss: 1.2083 - val_acc: 0.5556
Epoch 46/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0274 - acc: 1.0000 - val_loss: 1.0760 - val_acc: 0.6667
Epoch 47/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0995 - acc: 0.9600 - val_loss: 1.4213 - val_acc: 0.5556
Epoch 48/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0679 - acc: 0.9733 - val_loss: 1.4774 - val_acc: 0.5556
Epoch 49/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0280 - acc: 1.0000 - val_loss: 1.3433 - val_acc: 0.4444
Epoch 50/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0522 - acc: 0.9733 - val_loss: 1.6548 - val_acc: 0.4444
Epoch 51/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0704 - acc: 0.9867 - val_loss: 1.6029 - val_acc: 0.4444
Epoch 52/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0208 - acc: 1.0000 - val_loss: 1.7523 - val_acc: 0.5556
Epoch 53/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0522 - acc: 0.9867 - val_loss: 1.6319 - val_acc: 0.5556
Epoch 54/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0534 - acc: 0.9867 - val_loss: 1.6531 - val_acc: 0.6667
Epoch 55/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0300 - acc: 0.9867 - val_loss: 1.7899 - val_acc: 0.6667
Epoch 56/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0269 - acc: 1.0000 - val_loss: 1.7068 - val_acc: 0.4444
Epoch 57/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0110 - acc: 1.0000 - val_loss: 1.5507 - val_acc: 0.4444
Epoch 58/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0102 - acc: 1.0000 - val_loss: 1.5563 - val_acc: 0.4444
Epoch 59/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0512 - acc: 0.9867 - val_loss: 2.7963 - val_acc: 0.3333
Epoch 60/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0383 - acc: 1.0000 - val_loss: 2.2296 - val_acc: 0.4444
Epoch 61/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0430 - acc: 0.9867 - val_loss: 1.8682 - val_acc: 0.4444
Epoch 62/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0179 - acc: 1.0000 - val_loss: 1.7398 - val_acc: 0.4444
Epoch 63/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0076 - acc: 1.0000 - val_loss: 1.6328 - val_acc: 0.4444
Epoch 64/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0234 - acc: 0.9867 - val_loss: 1.7496 - val_acc: 0.4444
Epoch 65/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0199 - acc: 1.0000 - val_loss: 1.6652 - val_acc: 0.4444
Epoch 66/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0079 - acc: 1.0000 - val_loss: 1.6236 - val_acc: 0.4444
Epoch 67/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0096 - acc: 1.0000 - val_loss: 1.4793 - val_acc: 0.4444
Epoch 68/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0169 - acc: 1.0000 - val_loss: 1.5936 - val_acc: 0.4444
Epoch 69/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0081 - acc: 1.0000 - val_loss: 1.6317 - val_acc: 0.4444
Epoch 70/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0329 - acc: 0.9867 - val_loss: 1.9590 - val_acc: 0.4444
Epoch 71/100
75/75 [==============================] - 1s 9ms/step - loss: 0.1226 - acc: 0.9867 - val_loss: 2.1834 - val_acc: 0.3333
Epoch 72/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0150 - acc: 1.0000 - val_loss: 2.1490 - val_acc: 0.4444
Epoch 73/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0186 - acc: 1.0000 - val_loss: 2.1406 - val_acc: 0.3333
Epoch 74/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0130 - acc: 1.0000 - val_loss: 2.1410 - val_acc: 0.3333
Epoch 75/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0220 - acc: 0.9867 - val_loss: 2.1067 - val_acc: 0.4444
Epoch 76/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0215 - acc: 0.9867 - val_loss: 2.2418 - val_acc: 0.4444
Epoch 77/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0231 - acc: 0.9867 - val_loss: 2.0183 - val_acc: 0.4444
Epoch 78/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0318 - acc: 1.0000 - val_loss: 2.1473 - val_acc: 0.4444
Epoch 79/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0269 - acc: 1.0000 - val_loss: 2.1077 - val_acc: 0.4444
Epoch 80/100
75/75 [==============================] - 1s 9ms/step - loss: 0.1041 - acc: 0.9733 - val_loss: 2.8066 - val_acc: 0.3333
Epoch 81/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0168 - acc: 1.0000 - val_loss: 3.0465 - val_acc: 0.3333
Epoch 82/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0481 - acc: 0.9733 - val_loss: 3.2850 - val_acc: 0.4444
Epoch 83/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0180 - acc: 1.0000 - val_loss: 2.5361 - val_acc: 0.4444
Epoch 84/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0666 - acc: 0.9733 - val_loss: 4.0984 - val_acc: 0.3333
Epoch 85/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0591 - acc: 0.9867 - val_loss: 4.5535 - val_acc: 0.3333
Epoch 86/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0266 - acc: 1.0000 - val_loss: 3.7080 - val_acc: 0.3333
Epoch 87/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0224 - acc: 1.0000 - val_loss: 2.8268 - val_acc: 0.3333
Epoch 88/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0127 - acc: 1.0000 - val_loss: 2.6777 - val_acc: 0.3333
Epoch 89/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0090 - acc: 1.0000 - val_loss: 2.3840 - val_acc: 0.3333
Epoch 90/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0444 - acc: 0.9733 - val_loss: 2.0901 - val_acc: 0.4444
Epoch 91/100
75/75 [==============================] - 1s 9ms/step - loss: 0.1060 - acc: 0.9733 - val_loss: 2.0723 - val_acc: 0.5556
Epoch 92/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0158 - acc: 1.0000 - val_loss: 1.7588 - val_acc: 0.5556
Epoch 93/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0617 - acc: 0.9867 - val_loss: 1.8454 - val_acc: 0.4444
Epoch 94/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0338 - acc: 1.0000 - val_loss: 2.0064 - val_acc: 0.4444
Epoch 95/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0153 - acc: 1.0000 - val_loss: 1.9965 - val_acc: 0.5556
Epoch 96/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0208 - acc: 1.0000 - val_loss: 1.8009 - val_acc: 0.5556
Epoch 97/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0233 - acc: 1.0000 - val_loss: 2.0628 - val_acc: 0.5556
Epoch 98/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0306 - acc: 0.9867 - val_loss: 2.4620 - val_acc: 0.4444
Epoch 99/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0140 - acc: 1.0000 - val_loss: 2.5765 - val_acc: 0.4444
Epoch 100/100
75/75 [==============================] - 1s 9ms/step - loss: 0.0291 - acc: 1.0000 - val_loss: 2.4010 - val_acc: 0.5556
Test loss: 2.401033639907837
Test accuracy: 0.5555555820465088

レーニングデータの正解率(acc)は100%になっているのにテストデータの正解率(val_acc)が55%...過学習ってやつなのでは??というか学習できてないのでは?

とりあえず次に進みます。

使ってみる

レーニングの段階でだめな気しかしませんが一応使ってみます

from test import Train
import keras,os,glob,cv2
img_cols = 160
img_rows = 160
#モデルロード
t = Train()
model = t.make_model()
model.load_weights("my_model.h5")

#テスト画像パス取得
from PIL import Image
import numpy as np
file_path = "../cvae2/datasets/croped_data/*.jpg"
files = glob.glob(file_path)
labels = os.listdir("labeld_data")
print(labels)

for file in files:
    # 画像読み込み
    img = Image.open(file)
    img = img.resize((img_cols,img_rows))
    x = np.array(img, dtype=np.float32)
    x = x / 255.

    x = x.reshape(1,img_cols,img_rows,3)
    print(x.shape)
    pred = model.predict(x, batch_size=1, verbose=0)
    score = np.max(pred)
    pred_label = np.argmax(pred)
    result = labels[pred_label] + " " + str(score*100) + "%"
    print(result)
    # print(pred,score,pred_label)

    img = np.asarray(img)
    #PILはBGR,cv2はRGBで、色がおかしくなるので修正
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    cv2.imshow(result, img)
    key = cv2.waitKey(0)
    # ESCで終了
    if key==27:
        print("exit")
        cv2.destroyAllWindows()
        break

    cv2.destroyAllWindows()

バーの上部にクラス名と確率が出るようにしました。

これだと チノ 72.9%

f:id:ascreit:20181014152152p:plain

f:id:ascreit:20181014152154p:plainf:id:ascreit:20181014152156p:plainf:id:ascreit:20181014152159p:plainf:id:ascreit:20181014152202p:plainf:id:ascreit:20181014152206p:plainf:id:ascreit:20181014152209p:plainf:id:ascreit:20181014152212p:plainf:id:ascreit:20181014152215p:plain

ダメそうですね。

モデルの作り方とかもっと基礎的な部分を勉強しないとだめですね。

どうやって勉強すればいいんだろう...

英語分からなくて辛いけどfast.aiとか Kaggleのカーネル(?) とかをみて勉強するのが一般的なのかな?