SICE / create_data_loader.py
okhater's picture
Update create_data_loader.py
1a65a70 verified
Raw History Blame Contribute Delete
6.53 kB
import random
import os
from typing import Tuple, List, Dict
from datasets import load_dataset
from torch.utils.data import DataLoader, Dataset
from torchvision import transforms
from PIL import Image
import torch
class CustomDataset(Dataset):
"""
Custom Dataset that groups samples by their "label" field (acting as a sample identifier)
and separates images into low-quality images and a ground truth image.
The ground truth image is identified by inspecting the PIL image's filename for keywords
such as "label" or "gt". The remaining images are considered low-quality.
At retrieval time, if num_low is set to a positive integer, that many low images are randomly
selected from the available ones. If num_low is -1, then all available low images are selected.
"""
def __init__(
self,
hf_dataset,
num_low: int = 2,
seed: int = 42,
target_size: Tuple[int, int] = (256, 256)
):
"""
Parameters:
hf_dataset (datasets.Dataset): Hugging Face dataset object.
Each record is expected to be a dict with keys:
- "image": a PIL.Image (or an array convertible to PIL.Image) with a .filename attribute.
- "label": a string used as the sample identifier (e.g., "sample_1").
num_low (int): Number of low-quality images to randomly select per sample.
If set to -1, all available low images are selected.
seed (int): Random seed for reproducibility.
target_size (Tuple[int, int]): Size to which images will be resized.
"""
self.hf_dataset = hf_dataset
self.num_low = num_low
self.rng = random.Random(seed)
self.transform = transforms.Compose([
transforms.Resize(target_size),
transforms.ToTensor(),
])
self.samples = self._prepare_samples()
def _prepare_samples(self) -> List[Dict]:
groups = self._group_by_sample()
samples = []
for sample_id, items in groups.items():
low_imgs, label_img = self._separate_sample(items)
if label_img is not None and low_imgs:
samples.append({
"low_images": low_imgs,
"label": label_img,
})
return samples
def _group_by_sample(self) -> Dict[str, List[Dict]]:
groups = {}
for item in self.hf_dataset:
sample_id = item["label"] # Using the "label" field as the sample identifier.
groups.setdefault(sample_id, []).append(item)
return groups
def _separate_sample(self, items: List[Dict]) -> Tuple[List[Image.Image], Image.Image]:
low_images = []
label_image = None
for record in items:
image_obj = record["image"]
if not isinstance(image_obj, Image.Image):
image_obj = Image.fromarray(image_obj)
filename = getattr(image_obj, "filename", None)
if filename:
basename = os.path.basename(filename).lower()
if "label" in basename or "gt" in basename:
label_image = image_obj
else:
low_images.append(image_obj)
else:
low_images.append(image_obj)
return low_images, label_image
def __len__(self) -> int:
return len(self.samples)
def __getitem__(self, idx: int) -> Tuple[List[torch.Tensor], torch.Tensor]:
sample = self.samples[idx]
low_images = sample["low_images"]
label_image = sample["label"]
if self.num_low == -1:
selected_low = low_images
elif len(low_images) > self.num_low:
selected_low = self.rng.sample(low_images, self.num_low)
else:
selected_low = low_images
transformed_low = [self.transform(img) for img in selected_low]
transformed_label = self.transform(label_image)
return transformed_low, transformed_label
def custom_collate_fn(batch):
"""
Custom collate function to handle variable numbers of low images per sample.
Pads each sample's low images list to the maximum number found in the batch.
Returns:
Tuple of:
- Tensor of shape (batch_size, max_n, C, H, W) for low images.
- Tensor of shape (batch_size, C, H, W) for label images.
"""
low_imgs_list, labels = zip(*batch)
labels = torch.stack(labels, dim=0)
max_n = max(len(low_list) for low_list in low_imgs_list)
padded_low = []
for low_list in low_imgs_list:
if len(low_list) < max_n:
pad_tensor = torch.zeros_like(low_list[0])
padded = low_list + [pad_tensor] * (max_n - len(low_list))
else:
padded = low_list
padded_low.append(torch.stack(padded, dim=0))
low_imgs_batch = torch.stack(padded_low, dim=0)
return low_imgs_batch, labels
#%% Usage Example
if __name__ == "__main__":
dataset = load_dataset("okhater/SICE", name="default")
train_ds = dataset["train"]
test_ds = dataset["test"]
target_size = (512, 512)
# Example with fixed num_low = 2 (all samples return exactly 2 low images)
fixed_dataset = CustomDataset(train_ds, num_low=2, seed=42, target_size=target_size)
fixed_loader = DataLoader(fixed_dataset, batch_size=32, shuffle=True)
# Example with num_low = -1 (all available low images per sample are returned)
# Since the number of low images may vary, we use a custom collate function to pad the lists.
variable_dataset = CustomDataset(test_ds, num_low=-1, seed=42, target_size=target_size)
variable_loader = DataLoader(variable_dataset, batch_size=32, shuffle=False, collate_fn=custom_collate_fn)
print("Fixed Loader Batch:")
for low_imgs, label in fixed_loader:
# Here, low_imgs is a list of length batch_size, each element is a tensor with shape (num_low, C, H, W)
print("Batch contains", len(low_imgs), "samples.")
print("Each sample low images shapes:", [img.shape for img in low_imgs])
print("Label batch shape:", label.shape)
break
print("\nVariable Loader Batch:")
for low_imgs, label in variable_loader:
# Here, low_imgs is a tensor of shape (batch_size, max_n, C, H, W)
print("Low images batch shape:", low_imgs.shape)
print("Label batch shape:", label.shape)
break