-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataset.py
More file actions
281 lines (239 loc) · 11.6 KB
/
Copy pathdataset.py
File metadata and controls
281 lines (239 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
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
import os
import cv2
import pandas as pd
import numpy as np
import torch
import glob
import random
import json
from torch.utils.data import Dataset, Sampler
from tqdm import tqdm
from typing import Optional, Iterator, List, Dict
import albumentations as A
class MultiTaskDataset(Dataset):
def __init__(
self,
data_root: str,
transforms: Optional[A.Compose] = None,
task_transforms: Optional[Dict[str, A.Compose]] = None,
regression_heatmap_size: int = 64,
regression_heatmap_sigma: float = 4,
split: Optional[str] = None,
allowed_task_names: Optional[List[str]] = None,
):
super().__init__()
self.data_root = data_root
self.transforms = transforms
self.task_transforms = task_transforms or {}
self.regression_heatmap_size = regression_heatmap_size
self.regression_heatmap_sigma = regression_heatmap_sigma
self.split = split.lower() if split else None
self.allowed_task_names = [name.lower() for name in allowed_task_names] if allowed_task_names else None
if self.split and self.split not in {'train', 'val', 'validation'}:
raise ValueError("Split must be 'train', 'val', or 'validation'.")
self.csv_path = os.path.join(self.data_root, 'csv_files')
if not os.path.isdir(self.csv_path):
raise FileNotFoundError(f"CSV path not found: {self.csv_path}")
all_csv_files = glob.glob(os.path.join(self.csv_path, '*.csv'))
if not all_csv_files:
raise FileNotFoundError(f"No CSV files found in {self.csv_path}")
df_list = [pd.read_csv(csv_file) for csv_file in all_csv_files]
combined_df = pd.concat(df_list, ignore_index=True).reset_index(drop=True)
if self.split:
if 'train' not in combined_df.columns:
raise ValueError(
"CSV files must contain a 'train' column (1=train, 0=val) when using split filtering."
)
normalized_split = 'train' if self.split == 'train' else 'val'
train_flags = pd.to_numeric(combined_df['train'], errors='coerce')
if normalized_split == 'train':
mask = train_flags == 1
else:
mask = train_flags == 0
mask = mask.fillna(False)
filtered_df = combined_df[mask]
if filtered_df.empty:
raise ValueError(
f"No samples found for split '{self.split}' in {self.csv_path}."
)
self.dataframe = filtered_df.reset_index(drop=True)
split_name = 'Training' if normalized_split == 'train' else 'Validation'
print(f"Data loaded. {split_name} split samples: {len(self.dataframe)}")
else:
self.dataframe = combined_df
print(f"Data loaded. Total samples: {len(self.dataframe)}")
if self.allowed_task_names:
filtered_df = self.dataframe[self.dataframe['task_name'].str.lower().isin(self.allowed_task_names)]
if filtered_df.empty:
raise ValueError(
f"No samples found for allowed task names: {allowed_task_names}."
)
self.dataframe = filtered_df.reset_index(drop=True)
print(
f"Task filter applied ({allowed_task_names}). Remaining samples: {len(self.dataframe)}"
)
def __len__(self) -> int:
return len(self.dataframe)
def __getitem__(self, idx: int) -> dict:
record = self.dataframe.iloc[idx]
task_id = record['task_id']
task_name = record['task_name']
# Load image
image_abs_path = os.path.normpath(os.path.join(self.csv_path, record['image_path']))
image = cv2.imread(image_abs_path)
# Robustness check: retry next index if image load fails
if image is None:
return self.__getitem__((idx + 1) % len(self))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Save original image size BEFORE any transforms (for Regression coordinate normalization)
original_height, original_width = image.shape[:2]
# Load raw labels based on task
label = None
mask = None
bboxes = []
class_labels = []
reg_coords_tensor = None
regression_keypoints = None
if task_name == 'segmentation':
if pd.notna(record.get('mask_path')):
mask_path = os.path.normpath(os.path.join(self.csv_path, record['mask_path']))
mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
elif task_name == 'classification':
label = int(record['mask'])
elif task_name == 'Regression':
num_points = int(record['num_classes'])
coords = []
regression_keypoints = []
for i in range(1, num_points + 1):
col = f'point_{i}_xy'
if col in record and pd.notna(record[col]):
pt = json.loads(record[col])
else:
pt = [0, 0]
coords.extend(pt)
regression_keypoints.append((float(pt[0]), float(pt[1])))
label = np.array(coords, dtype=np.float32)
elif task_name == 'detection':
cols = ['x_min', 'y_min', 'x_max', 'y_max']
if all(c in record and pd.notna(record[c]) for c in cols):
box_coords = [float(record[c]) for c in cols]
bboxes = [box_coords + [0]]
class_labels = [0]
# Apply augmentations
transform = self.task_transforms.get(task_name, self.transforms)
if transform:
if task_name == 'segmentation':
mask_to_use = mask if mask is not None else np.zeros_like(image[:, :, 0])
augmented = transform(image=image, mask=mask_to_use)
image = augmented['image']
label = augmented.get('mask')
elif task_name == 'detection':
augmented = transform(image=image, bboxes=bboxes, class_labels=class_labels)
image = augmented['image']
if augmented['bboxes']:
label = np.array(augmented['bboxes'][0][:4], dtype=np.float32)
else:
label = np.array([-1.0, -1.0, -1.0, -1.0], dtype=np.float32)
elif task_name == 'Regression':
regression_keypoints = regression_keypoints or []
augmented = transform(image=image, keypoints=regression_keypoints)
image = augmented['image']
transformed_keypoints = augmented.get('keypoints', regression_keypoints)
if not transformed_keypoints:
transformed_keypoints = regression_keypoints
flat_coords = []
for kp in transformed_keypoints:
flat_coords.extend(kp)
label = np.array(flat_coords, dtype=np.float32)
else:
augmented = transform(image=image)
image = augmented['image']
# Format conversion & normalization
final_label = None
h, w = image.shape[1], image.shape[2]
# Ensure label is numpy for processing
if isinstance(label, torch.Tensor):
label = label.cpu().numpy()
if task_name == 'segmentation':
if label is None:
label = np.zeros((h, w), dtype=np.int64)
final_label = torch.from_numpy(label).long()
elif task_name == 'classification':
final_label = torch.tensor(label).long()
elif task_name in ['Regression', 'detection']:
if not isinstance(label, np.ndarray):
label = np.array([-1.0, -1.0, -1.0, -1.0], dtype=np.float32)
# Normalize coordinates to [0, 1]
if task_name == 'detection' and np.all(label >= 0):
label[[0, 2]] /= w
label[[1, 3]] /= h
elif task_name == 'Regression':
label[0::2] /= max(w, 1e-6)
label[1::2] /= max(h, 1e-6)
heatmaps = self._build_regression_heatmaps(label)
final_label = torch.from_numpy(heatmaps).float()
reg_coords_tensor = torch.from_numpy(label).float()
if final_label is None:
final_label = torch.from_numpy(label).float()
sample = {'image': image, 'label': final_label, 'task_id': task_id}
if reg_coords_tensor is not None:
sample['reg_coords'] = reg_coords_tensor
return sample
def _build_regression_heatmaps(self, normalized_coords: np.ndarray) -> np.ndarray:
num_points = normalized_coords.shape[0] // 2
heatmaps = np.zeros((num_points, self.regression_heatmap_size, self.regression_heatmap_size), dtype=np.float32)
for idx in range(num_points):
x_norm = np.clip(normalized_coords[2 * idx], 0.0, 1.0)
y_norm = np.clip(normalized_coords[2 * idx + 1], 0.0, 1.0)
center_x = x_norm * (self.regression_heatmap_size - 1)
center_y = y_norm * (self.regression_heatmap_size - 1)
heatmaps[idx] = self._generate_gaussian_heatmap(center_x, center_y)
return heatmaps
def _generate_gaussian_heatmap(self, center_x: float, center_y: float) -> np.ndarray:
size = self.regression_heatmap_size
xs = np.arange(0, size, 1, dtype=np.float32)
ys = np.arange(0, size, 1, dtype=np.float32)[:, None]
heatmap = np.exp(-((xs - center_x) ** 2 + (ys - center_y) ** 2) / (2 * self.regression_heatmap_sigma ** 2))
return heatmap
class MultiTaskUniformSampler(Sampler[List[int]]):
def __init__(self, dataset: MultiTaskDataset, batch_size: int, steps_per_epoch: Optional[int] = None):
self.dataset = dataset
self.batch_size = batch_size
self.indices_by_task = {}
# Group indices by task_id
print("\n--- Initializing Sampler ---")
for idx, task_id in enumerate(tqdm(dataset.dataframe['task_id'], desc="Grouping indices")):
if task_id not in self.indices_by_task:
self.indices_by_task[task_id] = []
self.indices_by_task[task_id].append(idx)
self.task_ids = list(self.indices_by_task.keys())
# Initial shuffle
for task_id in self.task_ids:
random.shuffle(self.indices_by_task[task_id])
# Determine epoch length
if steps_per_epoch is None:
self.steps_per_epoch = len(self.dataset) // self.batch_size
else:
self.steps_per_epoch = steps_per_epoch
def __iter__(self) -> Iterator[List[int]]:
task_cursors = {task_id: 0 for task_id in self.task_ids}
for _ in range(self.steps_per_epoch):
# Randomly select a task
task_id = random.choice(self.task_ids)
indices = self.indices_by_task[task_id]
cursor = task_cursors[task_id]
start_idx = cursor
end_idx = start_idx + self.batch_size
if end_idx > len(indices):
# Wrap around
batch_indices = indices[start_idx:]
random.shuffle(indices)
remaining = self.batch_size - len(batch_indices)
batch_indices.extend(indices[:remaining])
task_cursors[task_id] = remaining
else:
batch_indices = indices[start_idx:end_idx]
task_cursors[task_id] = end_idx
yield batch_indices
def __len__(self) -> int:
return self.steps_per_epoch