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. |
Replies: 5 comments
|
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: |
|
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 imgIf 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 |
|
@deependujha I see. Many thanks for the help and detailed explain! |
|
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! |
when you call optimize function, data is serialized before writing to the chunks.
src/litdata/streaming/writer.pyfile checks which serializer can serialize the data (serializers.pyfile)In your case,
JPEGSerializermight be the best fit, and if you look at its deserialize code