-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Add support for image_read to take some bytes as an input #8020
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
Comments
Hi @tchaton , I think |
Hey @NicolasHug. I looked into Would import numpy as np
import torch
from torchvision.io import decode_image
from torchvision.transforms import ToTensor
from time import time
from io import BytesIO
from PIL import Image
t0 = time()
with open("n02110063_14457.JPEG", "rb") as f:
data = f.read()
array = torch.frombuffer(data, dtype=torch.uint8)
array_torvision = decode_image(array)
print(time() - t0)
t0 = time()
array = np.fromfile("n02110063_14457.JPEG", dtype=np.uint8)
array_torvision = decode_image(torch.from_numpy(array))
print(time() - t0)
t0 = time()
with open("n02110063_14457.JPEG", "rb") as f:
data = f.read()
inp = BytesIO(data)
array_jpeg = ToTensor()(Image.open(inp))
array_jpeg = (array_jpeg * 255.).to(torch.uint8)
print(time() - t0)
assert torch.equal(array_torvision, array_jpeg)
|
Hey @NicolasHug, I tried using GPU decoding and this leads to a segmentation fault. t0 = time()
with open("n02110063_14457.JPEG", "rb") as f:
data = f.read()
array = torch.frombuffer(data, dtype=torch.uint8)
array_torvision = decode_jpeg(array.to("cuda"))
print(time() - t0) This is odd because I can see cuda path in decode_jpeg: if device.type == "cuda":
output = torch.ops.image.decode_jpeg_cuda(input, mode.value, device)
else:
output = torch.ops.image.decode_jpeg(input, mode.value) Ok, it seems this worked this way:
But took |
yeaahh... The gpu decoder is still Beta and has a few rough edge-cases (see also #4378 where it seems to be leaking depending on the CUDA version). Perhaps your best bet is to avoid it for now, or maybe look at https://github.com/itsliupeng/torchnvjpeg |
Uh oh!
There was an error while loading. Please reload this page.
🚀 The feature
Hey there,
For optimisation reasons, I am reading the bytes for a given JPEG image.
I would like to be able to deserialize it using torchvision by providing the bytes directly.
Best,
T.C
Motivation, pitch
I am encoding several images into a single shard for streaming purposes. Therefore, I can't provide a path to image_read.
Alternatives
I am using PIL right now and was hoping for speed gains using torchvision
Additional context
No response
The text was updated successfully, but these errors were encountered: