MVtecADデータセットでCNNを雑に構築してみる　カラー失敗編

# MVtecADデータセットを利用してCNNの実装を行う（カラー画像編）



今回はMVtecADのtileを使用する
## 使用ライブラリ
```
import keras
from keras.layers import Input, Dense, Conv2D, MaxPooling2D,  BatchNormalization, Activation, Flatten,Dropout 
from keras.models import Model
from keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.keras.optimizers import Adam
import os
import pickle
import numpy as np
import glob
from matplotlib import pyplot as plt
import cv2
```

### 使用画像
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3079535/74ebb62e-e7ff-99f2-9e16-ec5b5fe7018a.png)

```

#正常学習
true_train=glob.glob(os.path.join("/content/drive/MyDrive/mvtec/tile/train/good", '*'))

#正常テスト
true_test=glob.glob(os.path.join("/content/drive/MyDrive/mvtec/tile/test/good" , '*'))

#異常学習
bad_train=glob.glob(os.path.join("/content/drive/MyDrive/mvtec/tile/test/glue_strip" ,'*'))+glob.glob(os.path.join("/content/drive/MyDrive/mvtec/tile/test/gray_stroke" ,'*'))+glob.glob(os.path.join("/content/drive/MyDrive/mvtec/bottle/tile/test/crack" ,'*'))+glob.glob(os.path.join("/content/drive/MyDrive/mvtec/bottle/tile/test/rough" ,'*'))+glob.glob(os.path.join("/content/drive/MyDrive/mvtec/bottle/tile/test/oil" ,'*'))

#異常テスト
bad_test=glob.glob(os.path.join("/content/drive/MyDrive/mvtec/tile/test/test" ,'*'))

print(f"good {len(true_train)} good_test {len(true_test)} bad {len(bad_test)}")

batch_size = 32
num_classes = 2
epochs = 500
saveDir = "/opt/files/python/transfer/mvtec_cae/"
if not os.path.isdir(saveDir):
    os.makedirs(saveDir)

size=128

#学習画像の読み込み
x_train=[]
y_train=[]
for i in range(len(true_train)):
    img=cv2.imread(os.path.join(true_train[i]))
    img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
    img=np.asarray(img)
    img_resize=cv2.resize(img,(size,size))
    
    x_train.append(img_resize)
    y_train.append([0])

for k in range(len(bad_test)):
    timg=cv2.imread(os.path.join(bad_test[k]))
    timg=cv2.cvtColor(timg,cv2.COLOR_BGR2RGB)
    timg=np.asarray(timg)
    timg_resize=cv2.resize(timg,(size,size))
    x_train.append(timg_resize)
    y_train.append([1])


#テスト画像の読み込み
x_test=[]
y_test=[]
for j in range(len(true_test)):
    timg=cv2.imread(os.path.join(true_test[j]))
    timg=cv2.cvtColor(timg,cv2.COLOR_BGR2RGB)
    timg=np.asarray(timg)
    timg_resize=cv2.resize(img,(size,size))
    x_test.append(img_resize)
    y_test.append([0])
for k in range(len(bad_test)):
    timg=cv2.imread(os.path.join(bad_test[k]))
    timg=cv2.cvtColor(timg,cv2.COLOR_BGR2RGB)
    timg=np.asarray(timg)
    timg_resize=cv2.resize(timg,(size,size))
    x_test.append(timg_resize)
    y_test.append([1])

x_train=np.float32(x_train)
x_test=np.float32(x_test)
x_train/=255
x_test/=255

y_train=keras.utils.to_categorical(y_train, num_classes=2, dtype='float32')
y_test=keras.utils.to_categorical(y_test, num_classes=2, dtype='float32')

input_img = Input(shape=(size, size, 3))
#エンコーダー
#x = Conv2D(走査カーネル数, (カーネルサイズ, カーネルサイズ), padding='same')

x = Conv2D(16, (3, 3), padding='same')(input_img)
x = BatchNormalization()(x)
x = Activation('relu')(x)

x = MaxPooling2D((2, 2), padding='same')(x)

x = Conv2D(32, (3, 3), padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)

x = MaxPooling2D((2, 2), padding='same')(x)

x = Conv2D(64, (3, 3), padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)

x = MaxPooling2D((2, 2), padding='same')(x)

x = Conv2D(128, (3, 3), padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)

x = MaxPooling2D((2, 2), padding='same')(x)

x = Conv2D(256, (3, 3), padding='same')(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)


x = Flatten()(x)
x = Dense(1024)(x)
x = Activation('relu')(x)
x = Dropout(0.3)(x) 
x = Dense(2,activation="softmax")(x)

model = Model(input_img, x)
model.compile(optimizer='adam', loss='binary_crossentropy')
#mean_squared_error
#binary_crossentropy
model.summary()

es_cb = EarlyStopping(monitor='val_loss', patience=500, verbose=1, mode='auto')
chkpt = saveDir + 'AutoEncoder_mvtec_weights.{epoch:02d}-{loss:.2f}-{val_loss:.2f}.hdf5'
cp_cb = ModelCheckpoint(filepath = chkpt, monitor='val_loss', verbose=1, save_best_only=True, mode='auto')

model.optimizer.lr=0.01
history = model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, y_test),
                    callbacks=[es_cb, cp_cb],
                    )

```
上を実行して
```
ctest = model.predict(x_test)
cval = model.predict(x_train)
```
ctestには学習に使用していないテストデータ
cvalには学習に使用したデータが入っている


```
for i in range(len(c10val)):
  #print(np.argmax(c10val[i]))
　print(c10val[i])
  print("正解＝",y_train[i])
```

## 結果
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3079535/73da0d4f-e868-0d93-3dfd-438f9c95573a.png)
ロスが明らかに変なので次はグレイスケール画像で検証する。