当我尝试为keras创建自定义指标时,我遇到了错误(Intersection over union) . 我想找到两个图像(张量)联合的交叉点
def IoU(y_true,y_pred):
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
#assert len(y_true_f) != len(y_pred_f)
y_true_f = y_true_f.eval(session = K.get_session())
y_pred_f = y_pred_f.eval(session = K.get_session())
union1 = [i for i,j in zip(y_true_f,y_pred_f) if i != j]
union2 = [j for i,j in zip(y_true_f,y_pred_f) if i != j]
intersection = [i for i,j in zip(y_true_f,y_pred_f) if i == j]
unionAll = union1 + union2 + intersection
return (np.sum(intersection) + smooth) / float(np.sum(unionAll)+ smooth)
error 我得到:
InvalidArgumentError(请参见上面的回溯):您必须为占位符张量'activation_1_target'提供一个值,其中dtype为float和shape [?,?,?] [[Node:activation_1_target = Placeholderdtype = DT_FLOAT,shape = [?,?,? ],_ device =“/ job:localhost / replica:0 / task:0 / gpu:0”]] [[Node:metrics / IoU / Reshape / _5 = _Recvclient_terminated = false,recv_device =“/ job:localhost / replica: 0 / task:0 / cpu:0“,send_device =”/ job:localhost / replica:0 / task:0 / gpu:0“,send_device_incarnation = 1,tensor_name =”edge_8_metrics / IoU / Reshape“,tensor_type = DT_FLOAT, _device = “/作业:本地主机/复制:0 /任务:0 / CPU:0”]]