Saeed Alahmari
Saeed Alahmari

Reputation: 893

IoU keras (backend tensorflow) feed dict for placeholder error (tensor.eval()

I have error when I tried to create custom metric for keras (Intersection over union). I want to find intersection over union of two images (tensors)

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)

The error I get:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'activation_1_target' with dtype float and 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="/job:localhost/replica:0/task:0/cpu:0"]]

Upvotes: 2

Views: 555

Answers (1)

eps = 1.
def iou(y_true, y_pred):
   y_true_f = K.flatten(y_true)
   y_pred_f = K.flatten(y_pred)
   intersection = K.sum(y_true_f*y_pred_f)
   union = K.sum(y_true_f)+K.sum(y_pred_f)-intersection+eps
   return intersection/union

Upvotes: 0

Related Questions