Upload new.py
Browse files
new.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datasets import load_dataset
|
| 2 |
+
|
| 3 |
+
# Load your dataset (downloads files as needed)
|
| 4 |
+
ds = load_dataset("Devenarya/Microsoft100", split="train")
|
| 5 |
+
|
| 6 |
+
def image_and_segmentation_generator(dataset):
|
| 7 |
+
"""
|
| 8 |
+
Generator that yields {'image': PIL.Image, 'segmentation': PIL.Image}
|
| 9 |
+
Assumes order: image, mask, image, mask, ...
|
| 10 |
+
"""
|
| 11 |
+
images = [item['image'] for item in dataset]
|
| 12 |
+
for i in range(0, len(images), 2):
|
| 13 |
+
img = images[i].convert('RGB')
|
| 14 |
+
seg = images[i+1].convert('L')
|
| 15 |
+
yield {'image': img, 'segmentation': seg}
|
| 16 |
+
|
| 17 |
+
# Example usage:
|
| 18 |
+
gen = image_and_segmentation_generator(ds)
|
| 19 |
+
for i, sample in enumerate(gen):
|
| 20 |
+
print(f"Sample {i}: image size={sample['image'].size}, segmentation size={sample['segmentation'].size}")
|