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

encode_jpeg generates noise when processing 4k image #8587

Open
Lily-Git-hub opened this issue Aug 13, 2024 · 6 comments
Open

encode_jpeg generates noise when processing 4k image #8587

Lily-Git-hub opened this issue Aug 13, 2024 · 6 comments

Comments

@Lily-Git-hub
Copy link

Lily-Git-hub commented Aug 13, 2024

Hi I tried the latest torchvision 19.0 with pytorch2.4. I found that the encode_jpeg func had a problem when processing 4k image. For example, I have a 4K image tensor, which size is (3, 2160, 3840), then I use torchvision.io.encode_jpeg() in a loop, in the first loop, it can encode the tensor correctly. But in the following loops, it will generate jpeg image of noise only. Could you help with this please? Thanks!

@NicolasHug
Copy link
Member

Hi @Lily-Git-hub can you please provide a minimal reproducing example? Thank you

@Lily-Git-hub
Copy link
Author

Lily-Git-hub commented Aug 19, 2024

Hi Nicolas,

Please try this example:

import torch 
import torchvision
import torch.nn.functional as F   

for i in range(2):
    image_data = torch.load('image_data.pt')
    resized_image_tensor = F.interpolate(image_data.unsqueeze(0), size=(2160, 3820), mode='bilinear', align_corners=False)
    image_data_resized = resized_image_tensor[0]
    image_data_encoded = torchvision.io.encode_jpeg( (image_data_resized).to(torch.uint8) )
    data = image_data_encoded.cpu().numpy().tobytes()
    with open(f'1.jpg', 'wb') as f:
        f.write(data)

    del data, image_data_encoded, resized_image_tensor, image_data_resized, image_data`
    
without the last line of code, which deleted the used variables, the saved image would be noise only. Please unzip the 'image_data.zip' to get image_data.pt.
[image_data.zip](https://github.com/user-attachments/files/16656971/image_data.zip)

@NicolasHug
Copy link
Member

Sorry @Lily-Git-hub , I cannot reproduce your issue.

@Lily-Git-hub
Copy link
Author

del data, image_data_encoded, resized_image_tensor, image_data_resized, image_data`

Hi Nicolas,

Did you remove the above line of code? The error occurs when not deleting used variables. Thanks!

@NicolasHug
Copy link
Member

Yes, I deleted these lines. Can you please provide a more minimal reproducing example, without a for loop, wihtout resizing, and from a normal image rather than from a pt file (which I won't load on my machine for security reaosns)

@glazhh
Copy link

glazhh commented Sep 20, 2024

I encountered a similar issue. I resolved it by adding torch.cuda.synchronize() before using encode_jpeg. It seems there might be some synchronization problems between F.interpolate and torchvision.io.encode_jpeg.

   resized_image_tensor = F.interpolate(image_data.unsqueeze(0), size=(2160, 3820), mode='bilinear', align_corners=False)
   image_data_resized = resized_image_tensor[0].to(torch.uint8)
   # add synchronize after modified image and before encode jpeg
   torch.cuda.synchronize()
   image_data_encoded = torchvision.io.encode_jpeg(image_data_resized)

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

3 participants