shiyinzhang / Inside-Outside-Guidance

Interactive Object Segmentation with Inside-Outside Guidance
217 stars 34 forks source link

How to measure the IOU accuracy of pytorch model? #12

Open dhwndud407 opened 3 years ago

dhwndud407 commented 3 years ago

Hi I really appreciate your work. Before using your pytorch model uploaded on github, I want to know the IOU accuracy of the model. I try to measure IOU accuracy using PASCAL Val dataset. But the IOU accuracy I tested is quire different compared with IOG accuracy of your paper. So, I wander How to measure the IOU accuracy of IOG model? I attached my code modified eval.py to measure IOU accuracy.
Please let me know if there is anything wrong about my code.

def jaccard(gt_image, pre_image, void_pixels=None):
    segmentation = pre_image > 0.5
    annotation = gt_image == 1.0
    assert (annotation.shape == segmentation.shape)
    if void_pixels is None:
        void_pixels = np.zeros_like(annotation)
    assert (void_pixels.shape == annotation.shape)

    annotation = annotation.astype(np.bool)
    segmentation = segmentation.astype(np.bool)
    void_pixels = void_pixels.astype(np.bool)
    if np.isclose(np.sum(annotation & np.logical_not(void_pixels)), 0) and np.isclose(
            np.sum(segmentation & np.logical_not(void_pixels)), 0):
        return 1
    else:
        return np.sum(((annotation & segmentation) & np.logical_not(void_pixels))) / \
               np.sum(((annotation | segmentation) & np.logical_not(void_pixels)), dtype=np.float32)
 coarse_outs1,coarse_outs2,coarse_outs3,coarse_outs4,fine_out = net.forward(inputs)
 outputs = fine_out.to(torch.device('cpu'))
 pred = np.transpose(outputs.data.numpy()[0, :, :, :], (1, 2, 0))
 pred = 1 / (1 + np.exp(-pred))
 pred = np.squeeze(pred)
 gt = tens2image(sample_batched['crop_gt'][0, :, :, :])
 jaccard(gt,pred)