TypeError:不允许将“ tf.Tensor”用作Python“ bool”。在keras中编写自定义指标函数时

时间:2019-07-03 17:03:58

标签: python tensorflow keras

Keras版本:2.2.4

Tensorflow版本:1.14.0

TypeError:不允许将tf.Tensor用作Python bool。使用if t is not None:代替if t:来测试是否定义了张量,并使用TensorFlow ops(例如tf.cond)执行以张量的值为条件的子图。

我试图在Keras中编写一个自定义指标函数,由于上述错误而无法通过。请找到我正在使用的以下代码块。

def IOU(y_true, y_pred):
    intersections = 0
    unions = 0
    gt = y_true
    pred = y_pred

    # Compute interection of predicted (pred) and ground truth (gt) bounding boxes
    diff_width = np.minimum(gt[:,0] + gt[:,2], pred[:,0] + pred[:,2]) - np.maximum(gt[:,0], pred[:,0])
    diff_height = np.minimum(gt[:,1] + gt[:,3], pred[:,1] + pred[:,3]) - np.maximum(gt[:,1], pred[:,1])
    intersection = diff_width * diff_height

    # Compute union
    area_gt = gt[:,2] * gt[:,3]
    area_pred = pred[:,2] * pred[:,3]
    union = area_gt + area_pred - intersection

    # Compute intersection and union over multiple boxes
    for j, _ in enumerate(union):
      if union[j] > 0 and intersection[j] > 0 and union[j] >= intersection[j]:
        intersections += intersection[j]
        unions += union[j]

    # Compute IOU. Use epsilon to prevent division by zero
    iou = np.round(intersections / (unions + epsilon()), 4)
    return iou

model = create_model() 

model.compile(loss="mean_squared_error", optimizer="adam", metrics=[IOU])

model.fit(X_train,y_train, 
          validation_data=(X_val, y_val),
          epochs=EPOCHS,
          batch_size=32,
          verbose=1)

请访问y_truey_pred,以帮助我在keras中编写自定义指标函数。预先感谢。

1 个答案:

答案 0 :(得分:0)

使用tf.py_func为我解决了这个问题。下面给出的是对问题中上述代码块进行必要更改的代码块。

def IOU(y_true, y_pred):
        intersections = 0
        unions = 0

        gt = y_true
        pred = y_pred
        # Compute interection of predicted (pred) and ground truth (gt) bounding boxes
        diff_width = np.minimum(gt[:,0] + gt[:,2], pred[:,0] + pred[:,2]) - np.maximum(gt[:,0], pred[:,0])
        diff_height = np.minimum(gt[:,1] + gt[:,3], pred[:,1] + pred[:,3]) - np.maximum(gt[:,1], pred[:,1])
        intersection = diff_width * diff_height

        # Compute union
        area_gt = gt[:,2] * gt[:,3]
        area_pred = pred[:,2] * pred[:,3]
        union = area_gt + area_pred - intersection

        # Compute intersection and union over multiple boxes
        for j, _ in enumerate(union):
          if union[j] > 0 and intersection[j] > 0 and union[j] >= intersection[j]:
            intersections += intersection[j]
            unions += union[j]

        # Compute IOU. Use epsilon to prevent division by zero
        iou = np.round(intersections / (unions + epsilon()), 4)
        # This must match the type used in py_func
        iou = iou.astype(np.float32)
        return iou

def IoU(y_true, y_pred):
    iou = tf.py_func(IOU, [y_true, y_pred], tf.float32)
    return iou

model = create_model()
model.compile(loss="mean_squared_error", optimizer="adam", metrics=[IoU])
model.fit(X_train,y_train, validation_data=(X_val, y_val), epochs=EPOCHS, batch_size=32,
          verbose=1)