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

POC: add tiffile serializer #425

Merged
merged 12 commits into from
Nov 28, 2024
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ filelock
numpy
boto3
requests
tifffile
27 changes: 26 additions & 1 deletion src/litdata/streaming/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, Union

import numpy as np
import tifffile
import torch
from lightning_utilities.core.imports import RequirementCache

Expand Down Expand Up @@ -387,13 +388,37 @@ def can_serialize(self, data: float) -> bool:
return isinstance(data, float)


class TIFFSerializer(Serializer):
tchaton marked this conversation as resolved.
Show resolved Hide resolved
"""Serializer for TIFF files using tifffile."""

def serialize(self, item: Any) -> Tuple[bytes, Optional[str]]:
if not isinstance(item, str) or not os.path.isfile(item):
raise ValueError(f"The item to serialize must be a valid file path. Received: {item}")

# Read the TIFF file as bytes
with open(item, "rb") as f:
data = f.read()

# Store metadata if needed
_, ext = os.path.splitext(item)
metadata = f"tifffile:{ext.lower()}"
return data, metadata
robmarkcole marked this conversation as resolved.
Show resolved Hide resolved

def deserialize(self, data: bytes) -> Any:
image_array = tifffile.imread(io.BytesIO(data))
return image_array # This is a NumPy array

def can_serialize(self, item: Any) -> bool:
return isinstance(item, str) and os.path.isfile(item) and item.lower().endswith((".tif", ".tiff"))


_SERIALIZERS = OrderedDict(
**{
"str": StringSerializer(),
"int": IntegerSerializer(),
"float": FloatSerializer(),
"video": VideoSerializer(),
"tif": FileSerializer(),
"tifffile": TIFFSerializer(),
"file": FileSerializer(),
"pil": PILSerializer(),
"jpeg": JPEGSerializer(),
Expand Down
Loading