Kerasを使用したGrad CAMによるヒートマップ表示で未初期化値があるというエラーが起きる
Kerasで画像分類のCNNを作成し、自作のモデルを学習させています。
下記サイトを参考にGrad-CAMによるヒートマップを表示させてみようと思いましたが、エラーが発生します。解決方法についてご教授いただきたいです。
https://qiita.com/yakisobamilk/items/8f094590e5f45a24b59c
環境情報
Kerasバージョン 2.1.5
Google Colaboratory上で実行
モデルの定義部分
# これより前は省略
input_shape = x_train[0].shape
kernel_size = (3, 3)
conv_strides = (1, 1)
pool_size = (2, 2)
pool_strides = (2, 2)
class_length = len(y_train[0])
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=kernel_size,
strides=conv_strides, padding='same', activation='relu',
input_shape=input_shape))
model.add(MaxPooling2D(pool_size=pool_size, strides=pool_strides, padding='same'))
model.add(Dropout(0.2))
model.add(Conv2D(filters=64, kernel_size=kernel_size, strides=conv_strides,
padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=pool_size, strides=pool_strides, padding='same'))
model.add(Dropout(0.2))
model.add(Conv2D(filters=64, kernel_size=kernel_size, strides=conv_strides,
padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=pool_size, strides=pool_strides, padding='same'))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dense(class_length, activation='softmax'))
loss = categorical_crossentropy
optimizer = SGD(lr=0.01)
model.compile(loss=loss,
optimizer=optimizer,
metrics=['accuracy'])
summaryで出力した結果
_________________________________________________________________
Layer (type) Output Shape Param #
_________________________________________________________________
conv2d_1 (Conv2D) (None, 250, 250, 32) 896
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 125, 125, 32) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 125, 125, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 125, 125, 64) 18496
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 63, 63, 64) 0
_________________________________________________________________
dropout_2 (Dropout) (None, 63, 63, 64) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 63, 63, 64) 36928
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 32, 32, 64) 0
_________________________________________________________________
dropout_3 (Dropout) (None, 32, 32, 64) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 65536) 0
_________________________________________________________________
dense_1 (Dense) (None, 1024) 67109888
_________________________________________________________________
dense_2 (Dense) (None, 10) 10250
_________________________________________________________________
Total params: 67,176,458
Trainable params: 67,176,458
Non-trainable params: 0
_________________________________________________________________
Grad-CAMでヒートマップを表示させようとしている部分(最終行の時点でエラーとなっています)
import numpy as np
import keras
from keras.models import model_from_json
from keras.preprocessing.image import img_to_array, load_img
from tensorflow.python.keras import backend as K
image_path = 'cat1.jpg'
img = img_to_array(load_img(image_path, target_size=(250, 250)))
img = img_to_array(img) / 255.0
img = img[None, ...]
json_string = open('model.json').read()
model = model_from_json(json_string)
model.load_weights('model.h5')
output = model.output[:, 0:10]
last_conv = model.get_layer('conv2d_3')
grads = K.gradients(output, last_conv.output)[0]
pooled_grads = K.mean(grads, axis=(0, 1, 2))
iterate = K.function([model.input], [pooled_grads, last_conv.output[0]])
pooled_grads_val, conv_output_val = iterate([img])
発生するエラー
3つ目の畳み込み層のバイアス(/biasの直前の数字は実行するたびに何故か増えています…)が初期化されていないという意味のエラーとなっていることは分かりますが、以下の点について詰まっています。
- どうやってバイアス値を与えればいいか
- そもそものモデル定義時に明示的になにか設定しているわけではないためどういった具体的な値をあたえればいいか
---------------------------------------------------------------------------
FailedPreconditionError Traceback (most recent call last)
<ipython-input-12-039830cde68a> in <module>()
20 pooled_grads = K.mean(grads, axis=(0, 1, 2))
21 iterate = K.function([model.input], [pooled_grads, last_conv.output[0]])
---> 22 pooled_grads_val, conv_output_val = iterate([img])
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/backend.py in __call__(self, inputs)
3074
3075 fetched = self._callable_fn(*array_vals,
-> 3076 run_metadata=self.run_metadata)
3077 self._call_fetch_callbacks(fetched[-len(self._fetches):])
3078 return nest.pack_sequence_as(self._outputs_structure,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1437 ret = tf_session.TF_SessionRunCallable(
1438 self._session._session, self._handle, args, status,
-> 1439 run_metadata_ptr)
1440 if run_metadata:
1441 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
526 None, None,
527 compat.as_text(c_api.TF_Message(self.status.status)),
--> 528 c_api.TF_GetCode(self.status.status))
529 # Delete the underlying status object from memory otherwise it stays alive
530 # as there is a reference to status from this from the traceback due to
FailedPreconditionError: Attempting to use uninitialized value conv2d_3_9/bias
[[{{node conv2d_3_9/bias/read}}]]
[[{{node Mean_9}}]]
以上、よろしくお願い致します。