#前提・実現したいこと
Variational Autoencoderを使った画像の異常検知 後編 (塩尻MLもくもく会#7)
上記URLのサイト様のコードを参考にVAEとオリジナルデータセットを用いて異常検知を行いたいと考えております。
#エラーメッセージ
Python
1------------------------------------------------------------------------- 2InvalidArgumentError Traceback (most recent call last) 3<ipython-input-40-8e19e3bc72e7> in <module> 4 62 vae.fit(x_train_b, 5 63 epochs=epochs, 6---> 64 batch_size=batch_size) 7 65 #validation_data=(x_test, None)) 8 66 vae.save_weights('vae_mlp_mnist.h5') 9 10~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs) 11 1237 steps_per_epoch=steps_per_epoch, 12 1238 validation_steps=validation_steps, 13-> 1239 validation_freq=validation_freq) 14 1240 15 1241 def evaluate(self, 16 17~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/keras/engine/training_arrays.py in fit_loop(model, fit_function, fit_inputs, out_labels, batch_size, epochs, verbose, callbacks, val_function, val_inputs, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq) 18 194 ins_batch[i] = ins_batch[i].toarray() 19 195 20--> 196 outs = fit_function(ins_batch) 21 197 outs = to_list(outs) 22 198 for l, o in zip(out_labels, outs): 23 24~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/tensorflow/python/keras/backend.py in __call__(self, inputs) 25 3790 value = math_ops.cast(value, tensor.dtype) 26 3791 converted_inputs.append(value) 27-> 3792 outputs = self._graph_fn(*converted_inputs) 28 3793 29 3794 # EagerTensor.numpy() will often make a copy to ensure memory safety. 30 31~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/tensorflow/python/eager/function.py in __call__(self, *args, **kwargs) 32 1603 TypeError: For invalid positional/keyword argument combinations. 33 1604 """ 34-> 1605 return self._call_impl(args, kwargs) 35 1606 36 1607 def _call_impl(self, args, kwargs, cancellation_manager=None): 37 38~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/tensorflow/python/eager/function.py in _call_impl(self, args, kwargs, cancellation_manager) 39 1643 raise TypeError("Keyword arguments {} unknown. Expected {}.".format( 40 1644 list(kwargs.keys()), list(self._arg_keywords))) 41-> 1645 return self._call_flat(args, self.captured_inputs, cancellation_manager) 42 1646 43 1647 def _filtered_call(self, args, kwargs): 44 45~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/tensorflow/python/eager/function.py in _call_flat(self, args, captured_inputs, cancellation_manager) 46 1744 # No tape is watching; skip to running the function. 47 1745 return self._build_call_outputs(self._inference_function.call( 48-> 1746 ctx, args, cancellation_manager=cancellation_manager)) 49 1747 forward_backward = self._select_forward_and_backward_functions( 50 1748 args, 51 52~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/tensorflow/python/eager/function.py in call(self, ctx, args, cancellation_manager) 53 596 inputs=args, 54 597 attrs=attrs, 55--> 598 ctx=ctx) 56 599 else: 57 600 outputs = execute.execute_with_cancellation( 58 59~/.pyenv/versions/3.6.5/lib/python3.6/site-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name) 60 58 ctx.ensure_initialized() 61 59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name, 62---> 60 inputs, attrs, num_outputs) 63 61 except core._NotOkStatusException as e: 64 62 if name is not None: 65 66InvalidArgumentError: Incompatible shapes: [960000] vs. [3200] 67 [[node gradients_5/Sub_511_grad/BroadcastGradientArgs (defined at /Users/user_name/.pyenv/versions/3.6.5/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_54577] 68 69Function call stack: 70keras_scratch_graph
#コード
Python
1from __future__ import absolute_import 2from __future__ import division 3from __future__ import print_function 4 5from keras.layers import Lambda, Input, Dense, Reshape 6from keras.models import Model 7from keras.losses import mse 8from keras.utils import plot_model 9from keras import backend as K 10from keras.layers import BatchNormalization, Activation, Flatten 11from keras.layers.convolutional import Conv2DTranspose, Conv2D 12 13import numpy as np 14import matplotlib.pyplot as plt 15import matplotlib.colors as colors 16import os 17from sklearn import metrics 18 19#〜文字数制限のため省略〜 20 21# reparameterization trick 22# instead of sampling from Q(z|X), sample eps = N(0,I) 23# z = z_mean + sqrt(var)*eps 24def sampling(args): 25 z_mean, z_log_var = args 26 batch = K.shape(z_mean)[0] 27 dim = K.int_shape(z_mean)[1] 28 # by default, random_normal has mean=0 and std=1.0 29 epsilon = K.random_normal(shape=(batch, dim)) 30 return z_mean + K.exp(0.5 * z_log_var) * epsilon 31 32# dataset 33from bcn_dataset import BCN_Dataset2 34(x_train, y_train), (x_test, y_test) = BCN_Dataset2.create_bcn() 35 36x_train = x_train.reshape(x_train.shape[0], 224, 224, 3) 37x_test = x_test.reshape(x_test.shape[0], 224, 224, 3) 38 39x_train = x_train.astype('float32') / 255 40x_test = x_test.astype('float32') / 255 41 42x_train_b = [] 43x_train_shape = x_train.shape 44 45for i in range(len(x_train)): 46 if y_train[i] == 1:#スニーカーは7 47 temp = x_train[i,:,:,:] 48 x_train_b.append(temp.reshape((x_train_shape[1],x_train_shape[2],x_train_shape[3]))) 49 50x_train_b = np.array(x_train_b) 51x_train_b = cut_img(x_train_b, 50) 52print("train data:",len(x_train_b)) 53 54x_test_b = [] 55x_test_n = [] 56for i in range(len(x_test)): 57 if y_test[i] == 1:#スニーカーは7 58 temp = x_test[i,:,:,:] 59 x_test_b.append(temp.reshape((x_train_shape[1],x_train_shape[2],x_train_shape[3]))) 60 61 if y_test[i] == 2: 62 temp = x_test[i,:,:,:] 63 x_test_n.append(temp.reshape((x_train_shape[1],x_train_shape[2],x_train_shape[3]))) 64 65x_test_b = np.array(x_test_b) 66x_test_n = np.array(x_test_n) 67 68# network parameters 69input_shape=(80, 80, 3) 70batch_size = 128 71latent_dim = 2 72epochs = 10 73Nc = 16 74 75# build encoder model 76inputs = Input(shape=input_shape, name='encoder_input') 77x = Conv2D(Nc, kernel_size=2, strides=2)(inputs) 78x = BatchNormalization()(x) 79x = Activation('relu')(x) 80x = Conv2D(2*Nc, kernel_size=2, strides=2)(x) 81x = BatchNormalization()(x) 82x = Activation('relu')(x) 83x = Flatten()(x) 84 85z_mean = Dense(latent_dim, name='z_mean')(x) 86z_log_var = Dense(latent_dim, name='z_log_var')(x) 87z = Lambda(sampling, output_shape=(latent_dim,), name='z')([z_mean, z_log_var]) 88 89encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder') 90#encoder.summary() 91 92# build decoder model 93latent_inputs = Input(shape=(latent_dim,), name='z_sampling') 94x = Dense(2*2)(latent_inputs) 95x = BatchNormalization()(x) 96x = Activation('relu')(x) 97x = Reshape((2,2,1))(x) 98x = Conv2DTranspose(2*Nc, kernel_size=2, strides=2, padding='same')(x) 99x = BatchNormalization()(x) 100x = Activation('relu')(x) 101x = Conv2DTranspose(Nc, kernel_size=2, strides=2, padding='same')(x) 102x = BatchNormalization()(x) 103x = Activation('relu')(x) 104 105x1 = Conv2DTranspose(1, kernel_size=4, padding='same')(x) 106x1 = BatchNormalization()(x1) 107out1 = Activation('sigmoid')(x1)#out.shape=(n,8,8,1) 108 109x2 = Conv2DTranspose(1, kernel_size=4, padding='same')(x) 110x2 = BatchNormalization()(x2) 111out2 = Activation('sigmoid')(x2)#out.shape=(n,8,8,1) 112 113decoder = Model(latent_inputs, [out1, out2], name='decoder') 114#decoder.summary() 115 116# build VAE model 117outputs_mu, outputs_sigma_2 = decoder(encoder(inputs)[2]) 118vae = Model(inputs, [outputs_mu, outputs_sigma_2], name='vae_mlp') 119 120# VAE loss 121m_vae_loss = (K.flatten(inputs) - K.flatten(outputs_mu))**2 / K.flatten(outputs_sigma_2) 122m_vae_loss = 0.5 * K.sum(m_vae_loss) 123 124a_vae_loss = K.log(2 * 3.14 * K.flatten(outputs_sigma_2)) 125a_vae_loss = 0.5 * K.sum(a_vae_loss) 126 127kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var) 128kl_loss = K.sum(kl_loss, axis=-1) 129kl_loss *= -0.5 130 131vae_loss = K.mean(kl_loss + m_vae_loss + a_vae_loss) 132vae.add_loss(vae_loss) 133vae.compile(optimizer='adam') 134 135# train the autoencoder 136vae.fit(x_train_b, 137 epochs=epochs, 138 batch_size=batch_size) 139 #validation_data=(x_test, None)) 140vae.save_weights('vae_mlp_mnist.h5')
#試していること
サイト様のコードでは、サイズが(28,28)の白黒のmnistデータセットを使用しているため、(8,8,1)となっておりました。しかし私のデータセットはカラー、かつ画像のサイズが(224,224)であるため値を(80,80,3)に変更してコードの実行を行っておりましたところ、上記のようなエラーが発生してしまいました。コードの下のほうにある# build decoder model
より下の#out.shape=(n,8,8,1)
に解決策があるのではないか、もしかするとsigmoid関数ではうまくいかないのか?などの考察をしておりますが、なかなか解決せず頭を抱えております。何かご助言をいただけたら幸いです。
#補足
PCはmacOS Catalina バージョン10.15.5
Pythonのバージョンは3.6.5です
Jupiter notebookを使用しています
文字数制限の関係上、コードの関係ないと思われる関数を一部、省略させていただいております。
あなたの回答
tips
プレビュー