TypeError:使用`tf.张量`作为Python,不允许使用“bool”。在keras中编写自定义度量函数时

2024-06-28 20:08:18 发布

您现在位置:Python中文网/ 问答频道 /正文

Keras版本:2.2.4

Tensorflow版本:1.14.0

TypeError:不允许将tf.Tensor用作Python bool。使用if t is not None:而不是if t:来测试是否定义了张量,并使用TensorFlow操作,例如tf.条件执行以张量值为条件的子图。你知道吗

我试图在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中编写自定义度量函数。提前谢谢。你知道吗


Tags: andgttruemodelifnpdiffarea
1条回答
网友
1楼 · 发布于 2024-06-28 20:08:18

使用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)

相关问题 更多 >