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_true
和y_pred
,帮助我在keras中编写自定义度量函数。提前谢谢。你知道吗
使用
tf.py_func
为我解决了这个问题。下面给出的是对问题中上述代码块进行必要更改的代码块。你知道吗相关问题 更多 >
编程相关推荐