Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to convert the generated npy file into an image #110

Closed
woqiaow opened this issue Nov 9, 2022 · 4 comments
Closed

How to convert the generated npy file into an image #110

woqiaow opened this issue Nov 9, 2022 · 4 comments

Comments

@woqiaow
Copy link

woqiaow commented Nov 9, 2022

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

@kazuto1011
Copy link
Owner

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

@woqiaow
Copy link
Author

woqiaow commented Nov 9, 2022

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!

@kazuto1011
Copy link
Owner

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

@woqiaow
Copy link
Author

woqiaow commented Nov 9, 2022

thank you!

@woqiaow woqiaow closed this as completed Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants