画像で異常検知をしようとしたのですが学習をした後、評価画像との比較画像の出し方がわかりません
解決したいこと
プログラミング初心者です。
https://qiita.com/michelle0915/items/28bc5b844bd0d7ab597b
上記の投稿を参考に独自の画像を用いて異常検知を行おうとしているのですが元画像との差分を計算したあとに画像を表示するコードがわかりません。どのように構築すればよろしいでしょうか?
該当するソースコード
# 学習データの読み込み&前処理
train_images = glob.glob("//*.png")
train = []
for i in train_images:
image = cv2.imread(i)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
train.append(image)
train = np.array(train)
train = train.astype('float32') / 255
# 学習用ハイパーパラメータ
LEARNING_RATE = 0.0005
BATCH_SIZE = 8
Z_DIM = 50
EPOCHS = 3
# エンコーダ
encoder_input = Input(shape=(128,128,3), name='encoder_input')
x = encoder_input
x = Conv2D(filters=32, kernel_size=3, strides=1, padding='same', name='encoder_conv_0')(x)
x = LeakyReLU()(x)
x = Conv2D(filters=32, kernel_size=3, strides=1, padding='same', name='encoder_conv_0_1')(x)
x = LeakyReLU()(x)
x = Conv2D(filters=64, kernel_size=3, strides=2, padding='same', name='encoder_conv_1')(x)
x = LeakyReLU()(x)
x = Conv2D(filters=64, kernel_size=3, strides=2, padding='same', name='encoder_conv_2')(x)
x = LeakyReLU()(x)
x = Conv2D(filters=64, kernel_size=3, strides=1, padding='same', name='encoder_conv_3')(x)
x = LeakyReLU()(x)
shape_before_flattening = K.int_shape(x)[1:]
x = Flatten()(x)
encoder_output = Dense(Z_DIM, name='encoder_output')(x)
encoder = Model(encoder_input, encoder_output)
# デコーダ
decoder_input = Input(shape=(Z_DIM,), name='decoder_input')
x = Dense(np.prod(shape_before_flattening))(decoder_input)
x = Reshape(shape_before_flattening)(x)
x = Conv2DTranspose(filters=64, kernel_size=3, strides=1, padding='same', name='decoder_conv_t_0')(x)
x = LeakyReLU()(x)
x = Conv2DTranspose(filters=64, kernel_size=3, strides=2, padding='same', name='decoder_conv_t_1')(x)
x = LeakyReLU()(x)
x = Conv2DTranspose(filters=32, kernel_size=3, strides=2, padding='same', name='decoder_conv_t_2')(x)
x = LeakyReLU()(x)
x = Conv2DTranspose(filters=32, kernel_size=3, strides=1, padding='same', name='decoder_conv_t_2_5')(x)
x = LeakyReLU()(x)
x = Conv2DTranspose(filters=3, kernel_size=3, strides=1, padding='same', name='decoder_conv_t_3')(x)
x = Activation('sigmoid')(x)
decoder_output = x
decoder = Model(decoder_input, decoder_output)
# エンコーダ/デコーダ連結
model_input = encoder_input
model_output = decoder(encoder_output)
model = Model(model_input, model_output)
# 学習用設定設定(最適化関数、損失関数)
optimizer = Adam(learning_rate=LEARNING_RATE)
def r_loss(y_true, y_pred):
return K.mean(K.square(y_true - y_pred), axis=[1,2,3])
model.compile(optimizer=optimizer, loss=r_loss)
# 学習実行
model.fit(
train,
train,
batch_size=BATCH_SIZE,
epochs=EPOCHS
)
#テスト用画像の読み込み
original_images = glob.glob("//*.png")
test = []
for n in original_images:
img = cv2.imread(n)
img= cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
test.append(img)
test = np.array(test)
test = test.astype('float32') / 255
# 画像の復元
z_points = encoder.predict(test)
reconst_images = decoder.predict(z_points)
# 元画像との差分を計算
diff_images = np.absolute(reconst_images - test)
この後どうすれば画像を表示させることができますでしょうか?
0 likes