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11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
"""
Data Loader Utilities

This module provides data loading utilities for different domains
(satellite, fashion, robotics) with support for few-shot and zero-shot learning.
"""

import torch
import torch.nn as nn
import numpy as np
from PIL import Image
import os
import json
from typing import List, Dict, Tuple, Optional
import random
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transforms
from torchvision.transforms import functional as F
import cv2


class BaseDataLoader:
    """Base class for domain-specific data loaders."""
    
    def __init__(self, data_dir: str, image_size: Tuple[int, int] = (512, 512)):
        self.data_dir = data_dir
        self.image_size = image_size
        
        # Standard transforms
        self.transform = transforms.Compose([
            transforms.Resize(image_size),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])
        
        self.mask_transform = transforms.Compose([
            transforms.Resize(image_size, interpolation=transforms.InterpolationMode.NEAREST),
            transforms.ToTensor()
        ])
    
    def load_image(self, image_path: str) -> torch.Tensor:
        """Load and preprocess image."""
        image = Image.open(image_path).convert('RGB')
        return self.transform(image)
    
    def load_mask(self, mask_path: str) -> torch.Tensor:
        """Load and preprocess mask."""
        mask = Image.open(mask_path).convert('L')
        return self.mask_transform(mask)
    
    def get_random_sample(self) -> Tuple[torch.Tensor, Dict[str, torch.Tensor]]:
        """Get a random sample from the dataset."""
        raise NotImplementedError
    
    def get_class_examples(self, class_name: str, num_examples: int) -> List[Tuple[torch.Tensor, torch.Tensor]]:
        """Get examples for a specific class."""
        raise NotImplementedError


class SatelliteDataLoader(BaseDataLoader):
    """Data loader for satellite imagery segmentation."""
    
    def __init__(self, data_dir: str, image_size: Tuple[int, int] = (512, 512)):
        super().__init__(data_dir, image_size)
        
        # Satellite-specific classes
        self.classes = ["building", "road", "vegetation", "water"]
        self.class_to_id = {cls: i for i, cls in enumerate(self.classes)}
        
        # Load dataset structure
        self.load_dataset_structure()
    
    def load_dataset_structure(self):
        """Load dataset structure and file paths."""
        self.images = []
        self.masks = []
        self.class_samples = {cls: [] for cls in self.classes}
        
        # Assuming structure: data_dir/images/ and data_dir/masks/
        images_dir = os.path.join(self.data_dir, "images")
        masks_dir = os.path.join(self.data_dir, "masks")
        
        if not os.path.exists(images_dir) or not os.path.exists(masks_dir):
            # Create dummy data for demonstration
            self.create_dummy_data()
            return
        
        # Load real data
        for filename in os.listdir(images_dir):
            if filename.endswith(('.jpg', '.png', '.tif')):
                image_path = os.path.join(images_dir, filename)
                mask_path = os.path.join(masks_dir, filename.replace('.jpg', '_mask.png'))
                
                if os.path.exists(mask_path):
                    self.images.append(image_path)
                    self.masks.append(mask_path)
                    
                    # Categorize by class (simplified)
                    self.categorize_sample(image_path, mask_path)
    
    def create_dummy_data(self):
        """Create dummy satellite data for demonstration."""
        print("Creating dummy satellite data...")
        
        # Create dummy directory structure
        os.makedirs(os.path.join(self.data_dir, "images"), exist_ok=True)
        os.makedirs(os.path.join(self.data_dir, "masks"), exist_ok=True)
        
        # Generate dummy images and masks
        for i in range(100):
            # Create dummy image (satellite-like)
            image = np.random.randint(50, 200, (512, 512, 3), dtype=np.uint8)
            
            # Add some structure to make it look like satellite imagery
            # Buildings (rectangular shapes)
            for _ in range(5):
                x, y = np.random.randint(0, 400), np.random.randint(0, 400)
                w, h = np.random.randint(20, 80), np.random.randint(20, 80)
                image[y:y+h, x:x+w] = np.random.randint(100, 150, 3)
            
            # Roads (linear structures)
            for _ in range(3):
                x, y = np.random.randint(0, 512), np.random.randint(0, 512)
                length = np.random.randint(50, 150)
                angle = np.random.uniform(0, 2*np.pi)
                for j in range(length):
                    px = int(x + j * np.cos(angle))
                    py = int(y + j * np.sin(angle))
                    if 0 <= px < 512 and 0 <= py < 512:
                        image[py, px] = [80, 80, 80]
            
            # Save image
            image_path = os.path.join(self.data_dir, "images", f"satellite_{i:03d}.jpg")
            Image.fromarray(image).save(image_path)
            
            # Create corresponding mask
            mask = np.zeros((512, 512), dtype=np.uint8)
            
            # Add building masks
            for _ in range(3):
                x, y = np.random.randint(0, 400), np.random.randint(0, 400)
                w, h = np.random.randint(20, 80), np.random.randint(20, 80)
                mask[y:y+h, x:x+w] = 1  # Building class
            
            # Add road masks
            for _ in range(2):
                x, y = np.random.randint(0, 512), np.random.randint(0, 512)
                length = np.random.randint(50, 150)
                angle = np.random.uniform(0, 2*np.pi)
                for j in range(length):
                    px = int(x + j * np.cos(angle))
                    py = int(y + j * np.sin(angle))
                    if 0 <= px < 512 and 0 <= py < 512:
                        mask[py, px] = 2  # Road class
            
            # Save mask
            mask_path = os.path.join(self.data_dir, "masks", f"satellite_{i:03d}_mask.png")
            Image.fromarray(mask * 85).save(mask_path)  # Scale for visibility
            
            # Add to lists
            self.images.append(image_path)
            self.masks.append(mask_path)
            
            # Categorize
            self.categorize_sample(image_path, mask_path)
    
    def categorize_sample(self, image_path: str, mask_path: str):
        """Categorize sample by dominant class."""
        mask = np.array(Image.open(mask_path))
        
        # Count pixels for each class
        class_counts = {}
        for i, class_name in enumerate(self.classes):
            class_counts[class_name] = np.sum(mask == i)
        
        # Find dominant class
        dominant_class = max(class_counts.items(), key=lambda x: x[1])[0]
        self.class_samples[dominant_class].append((image_path, mask_path))
    
    def get_random_query(self, class_name: str) -> Tuple[torch.Tensor, torch.Tensor]:
        """Get a random query image and mask for a specific class."""
        if class_name not in self.class_samples or not self.class_samples[class_name]:
            # Fallback to any available sample
            idx = random.randint(0, len(self.images) - 1)
            image = self.load_image(self.images[idx])
            mask = self.load_mask(self.masks[idx])
            return image, mask
        
        # Get random sample from specified class
        image_path, mask_path = random.choice(self.class_samples[class_name])
        image = self.load_image(image_path)
        mask = self.load_mask(mask_path)
        
        return image, mask
    
    def get_class_examples(self, class_name: str, num_examples: int) -> List[Tuple[torch.Tensor, torch.Tensor]]:
        """Get examples for a specific class."""
        examples = []
        
        if class_name in self.class_samples:
            available_samples = self.class_samples[class_name]
            selected_samples = random.sample(available_samples, min(num_examples, len(available_samples)))
            
            for image_path, mask_path in selected_samples:
                image = self.load_image(image_path)
                mask = self.load_mask(mask_path)
                examples.append((image, mask))
        
        return examples


class FashionDataLoader(BaseDataLoader):
    """Data loader for fashion segmentation."""
    
    def __init__(self, data_dir: str, image_size: Tuple[int, int] = (512, 512)):
        super().__init__(data_dir, image_size)
        
        # Fashion-specific classes
        self.classes = ["shirt", "pants", "dress", "shoes"]
        self.class_to_id = {cls: i for i, cls in enumerate(self.classes)}
        
        # Load dataset structure
        self.load_dataset_structure()
    
    def load_dataset_structure(self):
        """Load dataset structure and file paths."""
        self.images = []
        self.masks = []
        self.class_samples = {cls: [] for cls in self.classes}
        
        # Assuming structure: data_dir/images/ and data_dir/masks/
        images_dir = os.path.join(self.data_dir, "images")
        masks_dir = os.path.join(self.data_dir, "masks")
        
        if not os.path.exists(images_dir) or not os.path.exists(masks_dir):
            # Create dummy data for demonstration
            self.create_dummy_data()
            return
        
        # Load real data
        for filename in os.listdir(images_dir):
            if filename.endswith(('.jpg', '.png')):
                image_path = os.path.join(images_dir, filename)
                mask_path = os.path.join(masks_dir, filename.replace('.jpg', '_mask.png'))
                
                if os.path.exists(mask_path):
                    self.images.append(image_path)
                    self.masks.append(mask_path)
                    
                    # Categorize by class
                    self.categorize_sample(image_path, mask_path)
    
    def create_dummy_data(self):
        """Create dummy fashion data for demonstration."""
        print("Creating dummy fashion data...")
        
        # Create dummy directory structure
        os.makedirs(os.path.join(self.data_dir, "images"), exist_ok=True)
        os.makedirs(os.path.join(self.data_dir, "masks"), exist_ok=True)
        
        # Generate dummy images and masks
        for i in range(100):
            # Create dummy image (fashion-like)
            image = np.random.randint(200, 255, (512, 512, 3), dtype=np.uint8)
            
            # Add fashion items
            class_id = i % len(self.classes)
            
            if class_id == 0:  # Shirt
                # Create shirt-like shape
                center_x, center_y = 256, 256
                width, height = 150, 200
                image[center_y-height//2:center_y+height//2, center_x-width//2:center_x+width//2] = [100, 150, 200]
            
            elif class_id == 1:  # Pants
                # Create pants-like shape
                center_x, center_y = 256, 300
                width, height = 120, 180
                image[center_y-height//2:center_y+height//2, center_x-width//2:center_x+width//2] = [50, 100, 150]
            
            elif class_id == 2:  # Dress
                # Create dress-like shape
                center_x, center_y = 256, 250
                width, height = 140, 220
                image[center_y-height//2:center_y+height//2, center_x-width//2:center_x+width//2] = [200, 100, 150]
            
            else:  # Shoes
                # Create shoes-like shape
                center_x, center_y = 256, 400
                width, height = 100, 60
                image[center_y-height//2:center_y+height//2, center_x-width//2:center_x+width//2] = [80, 80, 80]
            
            # Save image
            image_path = os.path.join(self.data_dir, "images", f"fashion_{i:03d}.jpg")
            Image.fromarray(image).save(image_path)
            
            # Create corresponding mask
            mask = np.zeros((512, 512), dtype=np.uint8)
            
            # Add mask for the fashion item
            if class_id == 0:  # Shirt
                center_x, center_y = 256, 256
                width, height = 150, 200
                mask[center_y-height//2:center_y+height//2, center_x-width//2:center_x+width//2] = 1
            
            elif class_id == 1:  # Pants
                center_x, center_y = 256, 300
                width, height = 120, 180
                mask[center_y-height//2:center_y+height//2, center_x-width//2:center_x+width//2] = 2
            
            elif class_id == 2:  # Dress
                center_x, center_y = 256, 250
                width, height = 140, 220
                mask[center_y-height//2:center_y+height//2, center_x-width//2:center_x+width//2] = 3
            
            else:  # Shoes
                center_x, center_y = 256, 400
                width, height = 100, 60
                mask[center_y-height//2:center_y+height//2, center_x-width//2:center_x+width//2] = 4
            
            # Save mask
            mask_path = os.path.join(self.data_dir, "masks", f"fashion_{i:03d}_mask.png")
            Image.fromarray(mask * 51).save(mask_path)  # Scale for visibility
            
            # Add to lists
            self.images.append(image_path)
            self.masks.append(mask_path)
            
            # Categorize
            self.categorize_sample(image_path, mask_path)
    
    def categorize_sample(self, image_path: str, mask_path: str):
        """Categorize sample by dominant class."""
        mask = np.array(Image.open(mask_path))
        
        # Count pixels for each class
        class_counts = {}
        for i, class_name in enumerate(self.classes):
            class_counts[class_name] = np.sum(mask == (i + 1))  # +1 because 0 is background
        
        # Find dominant class
        dominant_class = max(class_counts.items(), key=lambda x: x[1])[0]
        self.class_samples[dominant_class].append((image_path, mask_path))
    
    def get_test_sample(self) -> Tuple[torch.Tensor, Dict[str, torch.Tensor]]:
        """Get a random test sample with ground truth masks."""
        idx = random.randint(0, len(self.images) - 1)
        image = self.load_image(self.images[idx])
        mask = self.load_mask(self.masks[idx])
        
        # Convert single mask to multi-class dictionary
        ground_truth = {}
        for i, class_name in enumerate(self.classes):
            class_mask = (mask == (i + 1)).float()  # +1 because 0 is background
            ground_truth[class_name] = class_mask
        
        return image, ground_truth


class RoboticsDataLoader(BaseDataLoader):
    """Data loader for robotics segmentation."""
    
    def __init__(self, data_dir: str, image_size: Tuple[int, int] = (512, 512)):
        super().__init__(data_dir, image_size)
        
        # Robotics-specific classes
        self.classes = ["robot", "tool", "safety"]
        self.class_to_id = {cls: i for i, cls in enumerate(self.classes)}
        
        # Load dataset structure
        self.load_dataset_structure()
    
    def load_dataset_structure(self):
        """Load dataset structure and file paths."""
        self.images = []
        self.masks = []
        self.class_samples = {cls: [] for cls in self.classes}
        
        # Assuming structure: data_dir/images/ and data_dir/masks/
        images_dir = os.path.join(self.data_dir, "images")
        masks_dir = os.path.join(self.data_dir, "masks")
        
        if not os.path.exists(images_dir) or not os.path.exists(masks_dir):
            # Create dummy data for demonstration
            self.create_dummy_data()
            return
        
        # Load real data
        for filename in os.listdir(images_dir):
            if filename.endswith(('.jpg', '.png')):
                image_path = os.path.join(images_dir, filename)
                mask_path = os.path.join(masks_dir, filename.replace('.jpg', '_mask.png'))
                
                if os.path.exists(mask_path):
                    self.images.append(image_path)
                    self.masks.append(mask_path)
                    
                    # Categorize by class
                    self.categorize_sample(image_path, mask_path)
    
    def create_dummy_data(self):
        """Create dummy robotics data for demonstration."""
        print("Creating dummy robotics data...")
        
        # Create dummy directory structure
        os.makedirs(os.path.join(self.data_dir, "images"), exist_ok=True)
        os.makedirs(os.path.join(self.data_dir, "masks"), exist_ok=True)
        
        # Generate dummy images and masks
        for i in range(100):
            # Create dummy image (robotics-like)
            image = np.random.randint(50, 150, (512, 512, 3), dtype=np.uint8)
            
            # Add robotics elements
            class_id = i % len(self.classes)
            
            if class_id == 0:  # Robot
                # Create robot-like shape
                center_x, center_y = 256, 256
                width, height = 120, 160
                image[center_y-height//2:center_y+height//2, center_x-width//2:center_x+width//2] = [100, 100, 100]
            
            elif class_id == 1:  # Tool
                # Create tool-like shape
                center_x, center_y = 256, 256
                width, height = 80, 120
                image[center_y-height//2:center_y+height//2, center_x-width//2:center_x+width//2] = [150, 100, 50]
            
            else:  # Safety equipment
                # Create safety equipment-like shape
                center_x, center_y = 256, 256
                width, height = 100, 100
                image[center_y-height//2:center_y+height//2, center_x-width//2:center_x+width//2] = [200, 200, 50]
            
            # Save image
            image_path = os.path.join(self.data_dir, "images", f"robotics_{i:03d}.jpg")
            Image.fromarray(image).save(image_path)
            
            # Create corresponding mask
            mask = np.zeros((512, 512), dtype=np.uint8)
            
            # Add mask for the robotics element
            if class_id == 0:  # Robot
                center_x, center_y = 256, 256
                width, height = 120, 160
                mask[center_y-height//2:center_y+height//2, center_x-width//2:center_x+width//2] = 1
            
            elif class_id == 1:  # Tool
                center_x, center_y = 256, 256
                width, height = 80, 120
                mask[center_y-height//2:center_y+height//2, center_x-width//2:center_x+width//2] = 2
            
            else:  # Safety equipment
                center_x, center_y = 256, 256
                width, height = 100, 100
                mask[center_y-height//2:center_y+height//2, center_x-width//2:center_x+width//2] = 3
            
            # Save mask
            mask_path = os.path.join(self.data_dir, "masks", f"robotics_{i:03d}_mask.png")
            Image.fromarray(mask * 85).save(mask_path)  # Scale for visibility
            
            # Add to lists
            self.images.append(image_path)
            self.masks.append(mask_path)
            
            # Categorize
            self.categorize_sample(image_path, mask_path)
    
    def categorize_sample(self, image_path: str, mask_path: str):
        """Categorize sample by dominant class."""
        mask = np.array(Image.open(mask_path))
        
        # Count pixels for each class
        class_counts = {}
        for i, class_name in enumerate(self.classes):
            class_counts[class_name] = np.sum(mask == (i + 1))  # +1 because 0 is background
        
        # Find dominant class
        dominant_class = max(class_counts.items(), key=lambda x: x[1])[0]
        self.class_samples[dominant_class].append((image_path, mask_path))
    
    def get_test_sample(self) -> Tuple[torch.Tensor, Dict[str, torch.Tensor]]:
        """Get a random test sample with ground truth masks."""
        idx = random.randint(0, len(self.images) - 1)
        image = self.load_image(self.images[idx])
        mask = self.load_mask(self.masks[idx])
        
        # Convert single mask to multi-class dictionary
        ground_truth = {}
        for i, class_name in enumerate(self.classes):
            class_mask = (mask == (i + 1)).float()  # +1 because 0 is background
            ground_truth[class_name] = class_mask
        
        return image, ground_truth