Skip to content

Instantly share code, notes, and snippets.

@totti0223
Last active May 22, 2019 01:20
Show Gist options
  • Select an option

  • Save totti0223/4e57084013bef2fc5f72883fd474c898 to your computer and use it in GitHub Desktop.

Select an option

Save totti0223/4e57084013bef2fc5f72883fd474c898 to your computer and use it in GitHub Desktop.
生物学者がディープラーニング可視化技術を実装しながら理解したい #1 ref: https://qiita.com/totti0223/items/122b4be9bbaafa40d9e9
#準備する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])
calculated_gradient = backprop_fn([image, 0]][0]
#準備
model = Kerasのmodel。 Modelでも、Sequentialでも
#勾配を取りたいtargetは入力画像。入力画像を入れるkeras tensor
target_data = model.input
#勾配元keras tensor。画像が分類されたクラスに該当するindexの値。なお、という表現は適切でないが、勾配の入出力の関係をわかりやすくするためにこのままにしておく。
loss = model.output[0,class_index_of_input_image]
#lossに対するtarget_dataの勾配。「target_dataのどこを微少変化させるとlossの値(推論結果)に影響が出るか」を微分した値で表現。この値をもとの画像に足してiterateすると狭義のdeepdream?
gradients = K.gradients(loss,target_data)[0]
#画像を入力して画像の勾配が帰ってくる関数。
backprop_fn = K.function([target_data, K.learning_phase()], [gradients])
#実際の使用
grads_val = backprop_fn([image, 0])
for k in range(1024):
_grads_val = backprop_fn([image, 0])[0]
_grads_val = deprocess_image(_grads_val)
image += _grads_val
image = deprocess_image(image)
#96はtoucanのindex
#元の画像
print(model.predict(x[np.newaxis])[0][96])
24.952557
#gradient ascent後の画像
print(model.predict(images[np.newaxis])[0][96])
559.31946
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment