kazuto1011 / deeplab-pytorch

PyTorch re-implementation of DeepLab v2 on COCO-Stuff / PASCAL VOC datasets

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to convert the generated npy file into an image

woqiaow opened this issue · comments

Thanks you for your work.
How to convert the generated npy file into an image?
I look forward to your reply. Thank you.

For example:

from torchvision.utils import save_image
import torch
import matplotlib.pyplot as plt
import numpy as np
import math

# load npy
logit = np.load(npy_filename)
C, H, W = logit.shape

# normalize
logit = torch.from_numpy(logit)[None]  # (1,C,H,W) in [-inf,+inf]
prob = logit.softmax(dim=1)[0]         # (C,H,W) in [0,1]

# save as images
for c in range(C):
    save_image(prob[c], f"class_{c}.png")

# or visualize w/ matplotlib
n = math.ceil(C**0.5)
fig, ax = plt.subplots(nrows=n, ncols=n, constrained_layout=True)
ax = ax.flatten()
[a.axis("off") for a in ax]
for c in range(C):
    ax[c].set_title(f"class {c}")
    ax[c].imshow(prob[c])
plt.show()

output

Thank you for your help. Actually, I want to ask how the images of dataset test to be submitted to PASCAL VOC's official website for testing were converted from npy to images. I have been puzzled for a long time and look forward to your reply. Thank you very much!

PR #108 reported in issue #77 may help you.

thank you!