###前提・実現したいこと
Python(Keras)でCNNを用い2種類の画像を判別するプログラムを作っております。
その時に学習したモデルを活用して判定時にどこに注目したのかを
grad-camにより、表現したいと考えております。
grad-camについては
https://github.com/jacobgil/keras-grad-cam/blob/master/grad-cam.py
こちらのコードを参考にしており自分のモデルに合わせて対象箇所を変えたつもりです。
モデルはこちらになります。
Layer (type) Output Shape Param #
conv2d_1 (Conv2D) (None, 62, 62, 32) 896
max_pooling2d_1 (MaxPooling2 (None, 31, 31, 32) 0
dropout_1 (Dropout) (None, 31, 31, 32) 0
zero_padding2d_1 (ZeroPaddin (None, 33, 33, 32) 0
conv2d_2 (Conv2D) (None, 31, 31, 96) 27744
max_pooling2d_2 (MaxPooling2 (None, 15, 15, 96) 0
dropout_2 (Dropout) (None, 15, 15, 96) 0
zero_padding2d_2 (ZeroPaddin (None, 17, 17, 96) 0
conv2d_3 (Conv2D) (None, 15, 15, 96) 83040
max_pooling2d_3 (MaxPooling2 (None, 7, 7, 96) 0
flatten_1 (Flatten) (None, 4704) 0
dense_1 (Dense) (None, 1024) 4817920
dropout_3 (Dropout) (None, 1024) 0
dense_2 (Dense) (None, 2) 2050
Total params: 4,931,650
Trainable params: 4,931,650
Non-trainable params: 0
###発生している問題・エラーメッセージ
IndexError Traceback (most recent call last) <ipython-input-3-2fe13b62daac> in <module>() 128 129 predicted_class = np.argmax(predictions) --> 130 cam, heatmap = grad_cam(model, preprocessed_input, predicted_class, "conv2d_3") 131 cv2.imwrite("gradcam.jpg", cam) 132 <ipython-input-3-2fe13b62daac> in grad_cam(input_model, image, category_index, layer_name) 92 93 loss = K.sum(model.layers[-1].output) ---> 94 conv_output = [l for l in model.layers[0].layers if l.name is layer_name][0].output 95 grads = normalize(K.gradients(loss, conv_output)[0]) 96 gradient_function = K.function([model.layers[0].input], [conv_output, grads]) IndexError: list index out of range
###該当のソースコード
python
1from keras.preprocessing import image 2from keras.layers.core import Lambda 3from keras.models import Sequential ,load_model 4from tensorflow.python.framework import ops 5import keras.backend as K 6import tensorflow as tf 7import numpy as np 8import keras 9import sys 10import cv2 11 12def target_category_loss(x, category_index, nb_classes): 13 return tf.multiply(x, K.one_hot([category_index], nb_classes)) 14 15def target_category_loss_output_shape(input_shape): 16 return input_shape 17 18def normalize(x): 19 # utility function to normalize a tensor by its L2 norm 20 return x / (K.sqrt(K.mean(K.square(x))) + 1e-5) 21 22def load_image(path): 23 img_path = path 24 img = image.load_img(img_path, target_size=(64, 64)) 25 x = image.img_to_array(img) 26 x = np.expand_dims(x, axis=0) 27 return x 28 29def register_gradient(): 30 if "GuidedBackProp" not in ops._gradient_registry._registry: 31 @ops.RegisterGradient("GuidedBackProp") 32 def _GuidedBackProp(op, grad): 33 dtype = op.inputs[0].dtype 34 return grad * tf.cast(grad > 0., dtype) * \ 35 tf.cast(op.inputs[0] > 0., dtype) 36 37def compile_saliency_function(model, activation_layer='conv2d_3'): 38 input_img = model.input 39 layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]]) 40 layer_output = layer_dict[activation_layer].output 41 max_output = K.max(layer_output, axis=3) 42 saliency = K.gradients(K.sum(max_output), input_img)[0] 43 return K.function([input_img, K.learning_phase()], [saliency]) 44 45def modify_backprop(model, name): 46 g = tf.get_default_graph() 47 with g.gradient_override_map({'Relu': name}): 48 49 # get layers that have an activation 50 layer_dict = [layer for layer in model.layers[1:] 51 if hasattr(layer, 'activation')] 52 53 # replace relu activation 54 for layer in layer_dict: 55 if layer.activation == keras.activations.relu: 56 layer.activation = tf.nn.relu 57 58 # re-instanciate a new model 59 new_model = model 60 return new_model 61 62def deprocess_image(x): 63 ''' 64 Same normalization as in: 65 https://github.com/fchollet/keras/blob/master/examples/conv_filter_visualization.py 66 ''' 67 if np.ndim(x) > 3: 68 x = np.squeeze(x) 69 # normalize tensor: center on 0., ensure std is 0.1 70 x -= x.mean() 71 x /= (x.std() + 1e-5) 72 x *= 0.1 73 74 # clip to [0, 1] 75 x += 0.5 76 x = np.clip(x, 0, 1) 77 78 # convert to RGB array 79 x *= 255 80 if K.image_dim_ordering() == 'th': 81 x = x.transpose((1, 2, 0)) 82 x = np.clip(x, 0, 255).astype('uint8') 83 return x 84 85def grad_cam(input_model, image, category_index, layer_name): 86 model = Sequential() 87 model.add(input_model) 88 89 nb_classes = 2 90 target_layer = lambda x: target_category_loss(x, category_index, nb_classes) 91 model.add(Lambda(target_layer, 92 output_shape = target_category_loss_output_shape)) 93 94 loss = K.sum(model.layers[-1].output) 95 conv_output = [l for l in model.layers[0].layers if l.name is layer_name][0].output 96 grads = normalize(K.gradients(loss, conv_output)[0]) 97 gradient_function = K.function([model.layers[0].input], [conv_output, grads]) 98 99 output, grads_val = gradient_function([image]) 100 output, grads_val = output[0, :], grads_val[0, :, :, :] 101 102 weights = np.mean(grads_val, axis = (0, 1)) 103 cam = np.ones(output.shape[0 : 2], dtype = np.float32) 104 105 for i, w in enumerate(weights): 106 cam += w * output[:, :, i] 107 108 cam = cv2.resize(cam, (64, 64)) 109 cam = np.maximum(cam, 0) 110 heatmap = cam / np.max(cam) 111 112 #Return to BGR [0..255] from the preprocessed image 113 image = image[0, :] 114 image -= np.min(image) 115 image = np.minimum(image, 255) 116 117 cam = cv2.applyColorMap(np.uint8(255*heatmap), cv2.COLORMAP_JET) 118 cam = np.float32(cam) + np.float32(image) 119 cam = 255 * cam / np.max(cam) 120 return np.uint8(cam), heatmap 121 122# 判定画像の読み込み 123preprocessed_input = load_image('image.jpeg') 124 125model = load_model('mymodel.h5') 126model.summary() 127 128predictions = model.predict(preprocessed_input) 129 130predicted_class = np.argmax(predictions) 131cam, heatmap = grad_cam(model, preprocessed_input, predicted_class, "conv2d_3") 132cv2.imwrite("gradcam.jpg", cam) 133 134register_gradient() 135guided_model = modify_backprop(model, 'GuidedBackProp') 136saliency_fn = compile_saliency_function(guided_model) 137saliency = saliency_fn([preprocessed_input, 0]) 138gradcam = saliency[0] * heatmap[..., np.newaxis] 139cv2.imwrite("guided_gradcam.jpg", deprocess_image(gradcam))
###試したこと
1行1行入力してみたもののどこが問題かわからず、
もともと専門外で初心者なこともあり、お手上げ状態です…
###補足情報(言語/FW/ツール等のバージョンなど)
tensorflow (1.1.0)
Keras (2.0.4)
回答3件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。