簡體   English   中英

Keras指標產生意外值

[英]Keras metric produces unexpected values

在上一個問題的幫助下,我設計了以下IoU實現:

def iou(y_pred_batch, y_true_batch):
    intersection = tf.zeros(())
    union = tf.zeros(())
    y_pred_batch = np.argmax(y_pred_batch, axis=-1)
    y_true_batch = np.argmax(y_true_batch, axis=-1)
    for i in range(num_classes):
        iTensor = tf.to_int64(tf.fill(y_pred_batch.shape, i))
        intersection = tf.add(intersection, tf.to_float(tf.count_nonzero(tf.logical_and(K.equal(y_true_batch, y_pred_batch), K.equal(y_true_batch, iTensor)))))
        union = tf.add(union, tf.to_float(tf.count_nonzero(tf.logical_or(K.equal(y_true_batch, iTensor), K.equal(y_pred_batch, iTensor)))))
    return intersection/union

我使用以下幾行代碼進行測試:

sess = tf.InteractiveSession()

y_true_batch = np.asarray([np.random.rand(imRows, imCols, num_classes) for i in range(2)])
y_pred_batch = np.asarray([np.random.rand(imRows, imCols, num_classes) for i in range(2)])

print (iou(y_true_batch, y_pred_batch).eval())
sess.close()

這將產生〜0.02的值,這是隨機初始化值所期望的。 但是,當我在keras模型中使用此度量標准時,該度量標准從第1個時期起返回1.0000,這顯然是錯誤的。 我不知道為什么會這樣,任何幫助將不勝感激。

剛剛改變了

np.argmax()

from keras import backend as K
K.argmax()

原因是當您使用np.argmax()計算時,不會創建張量,該代碼應使用張量的語言。 您需要按照keras中的張量操作執行所有操作。

用於測試keras。

y_true = np.asarray([np.random.rand(4,4, 4) for i in range(2)])
y_pred = np.asarray([np.random.rand(4, 4, 4) for i in range(2)])


iou_value = iou(
    K.variable(y_true),
    K.variable(y_pred),
).eval(session=K.get_session())
print('iou', iou)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM