2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Grad-CAMの損失関数

Posted at

前回の書いたGrad-CAMの記事では損失関数を結局どう定義してたかについては深く突っ込まなかったので、もう少し具体的に損失関数がどうなっているのか確認してみます。

前回コード

def grad_cam(input_model, image, cls, layer_name):
    y_c = input_model.output[0, cls]
    conv_output = input_model.get_layer(layer_name).output
    grads = K.gradients(y_c, conv_output)[0]

結局、このコードのy_c = input_model.output[0, cls]の一行部分が損失関数になっているわけですが、損失関数をどう定義して計算しているんでしょうか。
これはOne_hotベクトル同士の内積、つまり

def grad_cam(input_model, image, cls, layer_name):

    y_pred = input_model.output[0]

    y_true = np.zeros(1000)
    y_true[cls] = 1.0
    y_true = tf.convert_to_tensor(y_true.astype(np.float32))

    loss = K.sum(y_pred * y_true)

    conv_output = input_model.get_layer(layer_name).output
    grads = K.gradients(loss, conv_output)[0]

という計算をしているにすぎません。
それにしてもK.sum(y_pred * y_true)の損失関数の名称はなんていうんでしょうか。ヒンジ関数あたりが近い形ですかね?

ほかの損失関数を使った場合:

当然、ほかの損失関数を使っても良いです。

    #loss = - K.mean(K.square(y_pred - y_true), axis=-1)  # mean_squared_error
    loss = - K.categorical_crossentropy(y_true, y_pred)  # categorical_crossentropy

ただ、どのGrad-Camの結果は損失関数を使っても、あまり違いがないみたいです。
gradcam_hinge.jpg gradcam_mse.jpg gradcam_cate.jpg
:左(y_pred * y_true)、中央(mean_squared_error)、右(categorical_crossentropy)

まとめ:

Grad-CAMの損失関数はK.sum(y_pred * y_true)である。ほかの損失関数を使っても結果はあまり変わらない。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?