kaixin96 / PANet

Code for our ICCV 2019 paper PANet: Few-Shot Image Semantic Segmentation with Prototype Alignment
315 stars 64 forks source link

visualization of the segmentation results #23

Closed qianqianCDQ closed 4 years ago

qianqianCDQ commented 4 years ago

I am very interested in the PANet you made, but I want to know how to visualize the segmentation results. In the code, I did not find the visualization code of the segmentation results, thank you

kaixin96 commented 4 years ago

Hi @qianqianCDQ , visualizing the segmentation is similar to plotting an image in matplotlib. Say if you have a segmentation result of shape H x W where each entry stores the predicted class index, you can visualize it with plt.inshow (with an appropriate discrete colormap). For example:

import imageio

import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt

mask = imageio.imread('2007_000032.png', pilmode='P')
print(np.unique(mask))

mask[mask == 255] = 2  # depend on your choice of cmap
plt.imshow(mask, cmap=cm.tab10_r)
plt.axis('off')
plt.show()

image

Thank you.

qianqianCDQ commented 4 years ago

what i mean is that how to leverage checkpoint to save or plot the prediction result of a single query image or a group of query images.Because There are only some metrics calculated in test.py, such as mIoU, the forecast result is not drawn,thank you!

------------------ 原始邮件 ------------------ 发件人: "Kaixin WANG"<notifications@github.com>; 发送时间: 2020年6月21日(星期天) 晚上10:43 收件人: "kaixin96/PANet"<PANet@noreply.github.com>; 抄送: "yIcFOT价部峡艘史茬抠"<1906711512@qq.com>;"Mention"<mention@noreply.github.com>; 主题: Re: [kaixin96/PANet] visualization of the segmentation results (#23)

Hi @qianqianCDQ , visualizing the segmentation is similar to plotting an image in matplotlib. Say if you have a segmentation result of shape H x W where each entry stores the predicted class index, you can visualize it with plt.inshow (with an appropriate discrete colormap). For example: import imageio import numpy as np import matplotlib.cm as cm import matplotlib.pyplot as plt mask = imageio.imread('2007_000032.png', pilmode='P') print(np.unique(mask)) mask[mask == 255] = 2 # depend on your choice of cmap plt.imshow(mask, cmap=cm.tab10_r) plt.axis('off') plt.show()

Thank you.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

EdiblEovo commented 4 years ago

what i mean is that how to leverage checkpoint to save or plot the prediction result of a single query image or a group of query images.Because There are only some metrics calculated in test.py, such as mIoU, the forecast result is not drawn,thank you!

Hi @qianqianCDQ, I happened to work on the visualization too. What i do is simply use matplotlib to plot the prediction mask, label and image of a single query image. I just add the code below and it works for me. https://github.com/kaixin96/PANet/blob/e516dc3a4571be199b5821da39efd967fa169e35/test.py#L125-L127

import matplotlib.cm as cm
import matplotlib.pyplot as plt

plt.figure()
plt.subplot(2,2,1)
plt.imshow(np.array(query_pred.argmax(dim=1)[0].cpu()), cmap=cm.tab10_r)
plt.subplot(2,2,2)
plt.imshow(np.array(query_labels[0].cpu()), cmap=cm.tab10_r)
plt.subplot(2,2,3)
plt.imshow(np.array(np.squeeze(query_images[0].cpu())).transpose(1,2,0))
plt.axis('off')
plt.show()
os.system("pause")

Hope this helps.

kaixin96 commented 4 years ago

I see, in that case you might need to hack test.py, say adding a breakpoint after https://github.com/kaixin96/PANet/blob/e516dc3a4571be199b5821da39efd967fa169e35/test.py#L122-L123 query_pred is the prediction with shape (N x B) x (1 + Wa) x H x W. By applying argmax you can get a segmentation mask of shape (N x B) x H x W. Thank you.

kaixin96 commented 4 years ago

I’m closing this issue because it has been inactive for a while. Feel free to reopen if you have questions. Thank you.