Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #勾配を特徴マップごとに平均化します。 | |
| weights = np.mean(grads_val, axis=(0, 1)) | |
| #平均化したものと中間出力の内積をとります。(特徴マップごとにweightを乗算し、すべて足します) | |
| grad_cam = np.dot(out_val, weights) | |
| #ここで、grad_cam大きさは14*14です。もとの画像に重ね合わせるためリサイズします。 | |
| from scipy.ndimage.interpolation import zoom | |
| grad_cam = zoom(grad_cam, images.shape[0]/grad_cam.shape[0]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #準備するkerasのtensor | |
| target_data = 勾配を求めたいデータ | |
| loss = 定義した損失 | |
| #要の勾配を定義する式。下記式は「target_dataのどこを微少変化させるとlossの値に影響が出るか」を微分した値で表現したデータを得るためのもの。得られるgradientsのshapeはtarget_dataと同じ | |
| gradients = K.gradients(loss,target_data)[0] | |
| #関数として定義。input_dataとK.leraning_phase()をいれてgradientを返す関数。 | |
| backprop_fn = K.function([input_data, K.learning_phase()], [gradients]) | |