keep pil data in loading? #406
-
Hi, I'm quite new to litdata and am looking into the imagenet demo here, just wondering if it is possible to return the original pil image here instead of pt tensor? Not sure what's the correct way to past code from studio here, but I'm following the demo here.
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
Hi @rxqy, sure you can. Just be sure that when you optimized your dataset ( For details on optimize fn, refer readme
|
Beta Was this translation helpful? Give feedback.
-
Hi @deependujha, in the imagenet demo we are returning the pil img right? But I'm still getting pt tensor in my streaming dataset. It seems that litdata is automatically doing the convertion for me. I made some small modification to the optimize_fn so I can double check the result, should make no difference here.
While in my streaming dataset:
|
Beta Was this translation helpful? Give feedback.
-
when you call optimize function, data is serialized before writing to the chunks. In your case, def deserialize(self, data: bytes) -> Union["JpegImageFile", torch.Tensor]:
if _TORCH_VISION_AVAILABLE:
from torchvision.io import decode_jpeg
from torchvision.transforms.functional import pil_to_tensor
array = torch.frombuffer(data, dtype=torch.uint8)
# Note: Some datasets like Imagenet contains some PNG images with JPEG extension, so we fallback to PIL
with suppress(RuntimeError):
return decode_jpeg(array)
img = PILSerializer.deserialize(data)
if _TORCH_VISION_AVAILABLE:
img = pil_to_tensor(img)
return img If torchvision is available, it tries to use it as it is much faster. btw, you can convert back from torchvision to pil image using: to_pil_image |
Beta Was this translation helpful? Give feedback.
-
@deependujha I see. Many thanks for the help and detailed explain! |
Beta Was this translation helpful? Give feedback.
-
Oh I found the PILSerializer here. A quick fix would be return the img in PIL.Image.Image format (not PIL.JpegImagePlugin.JpegImageFile). Many thanks!
|
Beta Was this translation helpful? Give feedback.
when you call optimize function, data is serialized before writing to the chunks.
src/litdata/streaming/writer.py
file checks which serializer can serialize the data (serializers.py
file)In your case,
JPEGSerializer
might be the best fit, and if you look at its deserialize code