| from datasets import load_dataset |
| from torch.utils.data import Dataset |
| from PIL import Image |
|
|
| from src.config import ORIGINAL_ID_TO_NEW_ID |
|
|
| |
| class RVLCDIPSubset(Dataset): |
| def __init__(self, split="train", transform=None, max_per_class=300): |
| |
| self.dataset = load_dataset("dvgodoy/rvl_cdip_mini", split=split) |
| self.transform = transform |
| self.samples = [] |
|
|
| class_counts = {} |
|
|
| for i, item in enumerate(self.dataset): |
| label = item["label"] |
|
|
| |
| if label not in ORIGINAL_ID_TO_NEW_ID: |
| continue |
|
|
| new_label = ORIGINAL_ID_TO_NEW_ID[label] |
| class_counts[new_label] = class_counts.get(new_label, 0) |
|
|
| |
| if class_counts[new_label] >= max_per_class: |
| continue |
|
|
| self.samples.append(i) |
| class_counts[new_label] += 1 |
|
|
| if len(class_counts) == len(ORIGINAL_ID_TO_NEW_ID): |
| if all(count >= max_per_class for count in class_counts.values()): |
| break |
|
|
| def __len__(self): |
| return len(self.samples) |
|
|
| def __getitem__(self, idx): |
| item = self.dataset[self.samples[idx]] |
| |
| image = item["image"].convert("RGB") |
| label = ORIGINAL_ID_TO_NEW_ID[item["label"]] |
|
|
| |
| if self.transform: |
| image = self.transform(image) |
|
|
| |
| return image, label |