-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.py
More file actions
216 lines (178 loc) · 7.92 KB
/
loader.py
File metadata and controls
216 lines (178 loc) · 7.92 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
import torch
from torch import Tensor
from torch.utils.data import Dataset
from torchvision import transforms
from PIL import Image
import xml.etree.ElementTree as ET
from typing import Any, Dict, List, Optional, Tuple, Union, cast
from pathlib import Path
import warnings
import logging
# Get the logger from our main app
logger = logging.getLogger("loader")
def collate_fn(
batch: List[Tuple[torch.Tensor, torch.Tensor]],
) -> Union[Tuple[torch.Tensor, torch.Tensor], None]:
"""Combines individual data samples into a single batch for training.
Images sometimes fail to load and return None. This function removes
failed samples and stacks successful ones into proper batch tensors
that PyTorch can work with.
Args:
batch: Collection of (image, mask) pairs. Failed samples will be None.
Returns:
Batch tensors ready for model training, or None if all samples failed.
"""
# Remove any samples that failed during loading
batch: List[Tuple[torch.Tensor, torch.Tensor]] = [item for item in batch if item is not None]
# Check if we lost everything
if not batch:
warnings.warn(message="Complete batch failure - no samples loaded successfully")
logger.warning("All samples in batch returned None")
return None
# Split images and masks for separate stacking
images: Tuple[torch.Tensor, ...]
masks: Tuple[torch.Tensor, ...]
images, masks = zip(*batch)
# Combine into batch tensors
return torch.stack(tensors=images), torch.stack(tensors=masks)
class DatasetLoader(Dataset):
"""
Loads images and their bounding box annotations for object detection.
This class handles the boring stuff of reading image files and matching them
up with their XML annotation files. It figures out what classes you have,
loads everything into the right format, and handles errors when files are
corrupted or missing.
The annotations need to be in Pascal VOC XML format (the standard format
that most annotation tools export to).
Expected directory structure:
dataset/
├── images/
│ ├── image001.jpg
│ ├── image002.png
│ └── ...
└── annotations/
├── image001.xml
├── image002.xml
└── ...
"""
def __init__(
self, dirpath: Path, transform: Optional[transforms.Compose] = None
) -> None:
"""
Set up the dataset by finding all matching image and annotation files.
Args:
dirpath: Path to your dataset folder (should contain 'images' and 'annotations')
transform: Any image transformations you want to apply (resize, normalize, etc.)
"""
# Remember where everything is
self.images: Path = Path(dirpath, "images")
self.annotations: Path = Path(dirpath, "annotations")
self.transform: Optional[transforms.Compose] = transform
# Make sure the folders actually exist
if not self.images.exists():
warnings.warn(message=f"Can't find images folder at {self.images}")
logger.warning(f"Images folder missing: {self.images}")
return
if not self.annotations.exists():
warnings.warn(
message=f"Can't find annotations folder at {self.annotations}"
)
logger.warning(f"Annotations folder missing: {self.annotations}")
return
# Find pairs of images and annotations that have matching names
# This looks for .jpg, .jpeg, .png, and .tiff files
self.files: List[Tuple[Path, Path]] = [
(image, annotation)
for annotation in self.annotations.glob(pattern="*.xml")
for pattern in [
"*.jpg",
"*.jpeg",
"*.png",
"*.tiff",
]
for image in self.images.glob(pattern=pattern)
if image.stem == annotation.stem # Same filename, different extensions
]
# Build a dictionary that maps class names to numbers
# This goes through all the XML files and finds every unique class name
self.classes: Dict[str, int] = {
name: label
for label, name in enumerate(
dict.fromkeys( # Removes duplicates while keeping order
obj.find(path="name").text
for _, annotation in self.files
for obj in ET.parse(source=annotation)
.getroot()
.findall(path="object")
)
)
}
# Warn if we didn't find anything useful
if not self.files:
warnings.warn(
message="Couldn't find any matching image-annotation pairs. Check your file names!"
)
logger.warning("No valid pairs found - check that image and annotation filenames match")
return
logger.info(f"Found {len(self.files)} image-annotation pairs")
logger.info(f"Detected {len(self.classes)} different classes: {list(self.classes.keys())}")
def __len__(self) -> int:
"""
How many items are in this dataset?
Returns:
Number of image-annotation pairs we found
"""
return len(self.files)
def __getitem__(self, index: int) -> Optional[Tuple[torch.Tensor, Dict[str, Any]]]:
"""
Get a single item from the dataset.
This loads the image, applies any transforms, reads the XML file,
and packages everything up in the format the model expects.
Args:
index: Which item you want (0 to len(dataset)-1)
Returns:
A tuple with:
- The image as a tensor
- A dictionary with 'boxes', 'labels', and 'scores'
Returns None if something went wrong loading this item
"""
# Get the file paths for this index
image: Path
annotation: Path
image, annotation = self.files[index]
try:
# Load and transform the image
image: Image.Image = Image.open(fp=image)
if self.transform:
image: Union[Image.Image, torch.Tensor] = self.transform(img=image)
# Parse the XML annotation
tree: ET.ElementTree = ET.parse(source=annotation)
root: ET.Element = tree.getroot()
# Get all the objects in this image
objects: List[ET.Element] = root.findall(path="object")
# Extract bounding boxes (x1, y1, x2, y2 format)
boxes: List[List[int]] = [
[
int(cast(ET.Element, object.find(path="bndbox/xmin")).text),
int(cast(ET.Element, object.find(path="bndbox/ymin")).text),
int(cast(ET.Element, object.find(path="bndbox/xmax")).text),
int(cast(ET.Element, object.find(path="bndbox/ymax")).text),
]
for object in objects
]
# Convert class names to numbers using our class dictionary
labels: List[int] = [
self.classes[cast(ET.Element, object.find(path="name")).text]
for object in objects
]
# Package everything up for the model
target: Dict[str, torch.Tensor] = {
"boxes": torch.tensor(data=boxes),
"labels": torch.tensor(data=labels),
}
return image, target
except Exception as error:
# Something went wrong - log it and move on
logger.error(f"Failed to load item {index} ({image}): {str(error)}")
warnings.warn(f"Couldn't load item {index}: {str(error)}")
return None