CSV Data Loading API¶
Complete API reference for CSV-based data loading across all tasks.
Overview¶
AutoTimm provides CSV dataset classes for loading data from CSV files with custom annotations. Each task has a dedicated CSV dataset class:
| Dataset | Task | Description |
|---|---|---|
| CSVImageDataset | Classification | Single-label classification from CSV |
| MultiLabelImageDataset | Multi-Label | Multi-label classification from CSV |
| CSVDetectionDataset | Detection | Object detection with bounding boxes |
| CSVInstanceDataset | Instance Seg | Instance segmentation with masks |
Note: For DataModules that wrap these datasets, see:
- ImageDataModule - supports train_csv, val_csv, test_csv for classification
- MultiLabelImageDataModule - CSV-only data module for multi-label
- DetectionDataModule - supports CSV via format parameter
- InstanceSegmentationDataModule - supports CSV format
CSVImageDataset¶
Dataset for single-label classification from CSV files.
API Reference¶
autotimm.CSVImageDataset ¶
Bases: Dataset
Dataset for single-label classification from CSV.
CSV format::
image_path,label
img001.jpg,cat
img002.jpg,dog
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csv_path
|
str | Path
|
Path to CSV file. |
required |
image_dir
|
str | Path
|
Root directory for resolving image paths. |
'.'
|
image_column
|
str | None
|
Name of the column containing image paths.
If |
None
|
label_column
|
str | None
|
Name of the column containing class labels.
If |
None
|
transform
|
Callable | None
|
Transform to apply to images. Supports both
torchvision transforms (PIL input) and albumentations
transforms (numpy input with |
None
|
use_albumentations
|
bool
|
If |
False
|
Source code in src/autotimm/data/dataset.py
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 | |
class_to_idx
instance-attribute
¶
__init__ ¶
__init__(csv_path: str | Path, image_dir: str | Path = '.', image_column: str | None = None, label_column: str | None = None, transform: Callable | None = None, use_albumentations: bool = False)
Source code in src/autotimm/data/dataset.py
__len__ ¶
__getitem__ ¶
Source code in src/autotimm/data/dataset.py
CSV Format¶
- Column 1 (default): relative image path
- Column 2 (default): class label (string)
Custom column names can be specified via image_column and label_column parameters.
Usage Examples¶
Basic Usage¶
from autotimm.data import CSVImageDataset
from torchvision import transforms
transform = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
dataset = CSVImageDataset(
csv_path="train.csv",
image_dir="./images",
transform=transform,
)
print(f"Classes: {dataset.classes}")
print(f"Num classes: {dataset.num_classes}")
With Albumentations¶
import albumentations as A
from albumentations.pytorch import ToTensorV2
from autotimm.data import CSVImageDataset
transform = A.Compose([
A.Resize(224, 224),
A.HorizontalFlip(p=0.5),
A.Normalize(),
ToTensorV2(),
])
dataset = CSVImageDataset(
csv_path="train.csv",
image_dir="./images",
transform=transform,
use_albumentations=True, # Load images with OpenCV
)
Custom Column Names¶
# CSV with custom headers:
# filepath,category,metadata
# data/img1.jpg,cat,outdoor
# data/img2.jpg,dog,indoor
dataset = CSVImageDataset(
csv_path="custom.csv",
image_dir="./",
image_column="filepath",
label_column="category",
)
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
csv_path |
str \| Path |
Required | Path to CSV file |
image_dir |
str \| Path |
"." |
Root directory for resolving image paths |
image_column |
str \| None |
None |
Name of image path column (first column if None) |
label_column |
str \| None |
None |
Name of label column (second column if None) |
transform |
Callable \| None |
None |
Image transforms |
use_albumentations |
bool |
False |
Load images with OpenCV for albumentations |
Attributes¶
| Attribute | Type | Description |
|---|---|---|
classes |
list[str] |
Sorted list of unique class names |
class_to_idx |
dict[str, int] |
Mapping from class name to index |
num_classes |
int |
Number of classes |
samples |
list[tuple[str, int]] |
List of (image_path, class_idx) tuples |
MultiLabelImageDataset¶
Dataset for multi-label classification from CSV files with binary label columns.
API Reference¶
autotimm.MultiLabelImageDataset ¶
Bases: Dataset
Dataset for multi-label classification from CSV.
CSV format::
image_path,label_0,label_1,...,label_N
img1.jpg,1,0,1,...,0
img2.jpg,0,1,0,...,1
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csv_path
|
str | Path
|
Path to CSV file. |
required |
image_dir
|
str | Path
|
Root directory for resolving image paths. |
'.'
|
label_columns
|
list[str] | None
|
List of column names to use as labels.
If |
None
|
image_column
|
str | None
|
Name of the column containing image paths.
If |
None
|
transform
|
Callable | None
|
Transform to apply to images. Supports both
torchvision transforms (PIL input) and albumentations
transforms (numpy input with |
None
|
use_albumentations
|
bool
|
If |
False
|
Source code in src/autotimm/data/dataset.py
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 | |
__init__ ¶
__init__(csv_path: str | Path, image_dir: str | Path = '.', label_columns: list[str] | None = None, image_column: str | None = None, transform: Callable | None = None, use_albumentations: bool = False)
Source code in src/autotimm/data/dataset.py
__len__ ¶
__getitem__ ¶
Source code in src/autotimm/data/dataset.py
CSV Format¶
- Column 1 (default): relative image path
- Remaining columns: binary label indicators (0 or 1)
Usage Examples¶
Basic Usage¶
from autotimm.data import MultiLabelImageDataset
from torchvision import transforms
transform = transforms.Compose([
transforms.Resize(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
dataset = MultiLabelImageDataset(
csv_path="train.csv",
image_dir="./images",
transform=transform,
)
print(f"Labels: {dataset.label_names}")
print(f"Num labels: {dataset.num_labels}")
With Explicit Label Columns¶
dataset = MultiLabelImageDataset(
csv_path="train.csv",
image_dir="./images",
label_columns=["cat", "dog", "bird"], # Only use these labels
image_column="filepath",
)
With Albumentations¶
import albumentations as A
from albumentations.pytorch import ToTensorV2
transform = A.Compose([
A.Resize(224, 224),
A.HorizontalFlip(p=0.5),
A.Normalize(),
ToTensorV2(),
])
dataset = MultiLabelImageDataset(
csv_path="train.csv",
image_dir="./images",
transform=transform,
use_albumentations=True,
)
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
csv_path |
str \| Path |
Required | Path to CSV file |
image_dir |
str \| Path |
"." |
Root directory for resolving image paths |
label_columns |
list[str] \| None |
None |
List of label column names (auto-detected if None) |
image_column |
str \| None |
None |
Name of image path column (first column if None) |
transform |
Callable \| None |
None |
Image transforms |
use_albumentations |
bool |
False |
Load images with OpenCV for albumentations |
Attributes¶
| Attribute | Type | Description |
|---|---|---|
label_names |
list[str] |
List of label column names |
num_labels |
int |
Number of labels |
samples |
list[tuple[str, list[int]]] |
List of (image_path, labels) tuples |
CSVDetectionDataset¶
Dataset for object detection from CSV files with bounding box annotations.
API Reference¶
autotimm.CSVDetectionDataset ¶
Bases: Dataset
Dataset for object detection from CSV.
CSV format (one row per bounding box)::
image_path,x_min,y_min,x_max,y_max,label
img001.jpg,10,20,100,200,cat
img001.jpg,50,60,150,250,dog
img002.jpg,30,40,120,220,cat
Multiple rows per image are grouped automatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csv_path
|
str | Path
|
Path to CSV file. |
required |
image_dir
|
str | Path
|
Directory containing image files. |
'.'
|
image_column
|
str
|
Column name for image paths. Default |
'image_path'
|
bbox_columns
|
list[str] | None
|
Column names for bounding box coordinates.
Default |
None
|
label_column
|
str
|
Column name for class labels. Default |
'label'
|
transform
|
Callable | None
|
Albumentations transform with bbox_params. |
None
|
min_bbox_area
|
float
|
Minimum bbox area to include. Default 0. |
0.0
|
Attributes:
| Name | Type | Description |
|---|---|---|
class_names |
list[str]
|
List of class names. |
num_classes |
int
|
Number of classes. |
Source code in src/autotimm/data/detection_dataset.py
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 | |
__init__ ¶
__init__(csv_path: str | Path, image_dir: str | Path = '.', image_column: str = 'image_path', bbox_columns: list[str] | None = None, label_column: str = 'label', transform: Callable | None = None, min_bbox_area: float = 0.0)
Source code in src/autotimm/data/detection_dataset.py
__len__ ¶
__getitem__ ¶
Source code in src/autotimm/data/detection_dataset.py
CSV Format¶
image_path,x1,y1,x2,y2,label
img1.jpg,10,20,100,150,car
img1.jpg,50,60,200,180,person
img2.jpg,30,40,120,200,car
- image_path: relative path to image (multiple rows per image allowed)
- x1, y1, x2, y2: bounding box coordinates in
xyxyformat - label: class name
Usage Examples¶
Basic Usage¶
from autotimm.data import CSVDetectionDataset
import albumentations as A
from albumentations.pytorch import ToTensorV2
transform = A.Compose([
A.Resize(640, 640),
A.HorizontalFlip(p=0.5),
A.Normalize(),
ToTensorV2(),
], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['labels']))
dataset = CSVDetectionDataset(
csv_path="annotations.csv",
images_dir="./images",
transform=transform,
)
print(f"Classes: {dataset.classes}")
print(f"Num images: {len(dataset)}")
# Sample output
sample = dataset[0]
print(f"Boxes: {sample['boxes'].shape}") # [N, 4]
print(f"Labels: {sample['labels'].shape}") # [N]
Custom Column Names¶
# CSV with custom headers:
# filepath,xmin,ymin,xmax,ymax,category
# data/img1.jpg,10,20,100,150,car
dataset = CSVDetectionDataset(
csv_path="annotations.csv",
images_dir="./",
image_column="filepath",
bbox_columns=["xmin", "ymin", "xmax", "ymax"],
label_column="category",
)
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
csv_path |
str \| Path |
Required | Path to CSV file with annotations |
images_dir |
str \| Path |
Required | Directory containing images |
image_column |
str |
"image_path" |
Name of image path column |
bbox_columns |
list[str] |
["x1", "y1", "x2", "y2"] |
Names of bbox coordinate columns (xyxy format) |
label_column |
str |
"label" |
Name of label column |
transform |
Callable \| None |
None |
Albumentations transform with bbox support |
Return Format¶
Returns a dictionary with:
| Key | Type | Description |
|---|---|---|
image |
Tensor [C, H, W] |
Transformed image |
boxes |
Tensor [N, 4] |
Bounding boxes in (x1, y1, x2, y2) format |
labels |
Tensor [N] |
Class indices |
image_id |
int |
Image index |
orig_size |
Tensor [2] |
Original image size (H, W) |
CSVInstanceDataset¶
Dataset for instance segmentation from CSV files with mask annotations.
API Reference¶
autotimm.CSVInstanceDataset ¶
Bases: Dataset
Dataset for instance segmentation from CSV.
CSV format (one row per instance)::
image_path,x_min,y_min,x_max,y_max,label,mask_path
img001.jpg,10,20,100,200,cat,masks/img001_0.png
img001.jpg,50,60,150,250,dog,masks/img001_1.png
Each mask_path is a binary mask PNG for that instance.
Does NOT require pycocotools.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csv_path
|
str | Path
|
Path to CSV file. |
required |
image_dir
|
str | Path
|
Directory containing images and masks. |
'.'
|
image_column
|
str
|
Column name for image paths. Default |
'image_path'
|
bbox_columns
|
list[str] | None
|
Column names for bbox coordinates.
Default |
None
|
label_column
|
str
|
Column name for class labels. Default |
'label'
|
mask_column
|
str
|
Column name for mask file paths. Default |
'mask_path'
|
transform
|
Any
|
Albumentations transforms to apply. |
None
|
Source code in src/autotimm/data/instance_dataset.py
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 | |
__init__ ¶
__init__(csv_path: str | Path, image_dir: str | Path = '.', image_column: str = 'image_path', bbox_columns: list[str] | None = None, label_column: str = 'label', mask_column: str = 'mask_path', transform: Any = None)
Source code in src/autotimm/data/instance_dataset.py
__len__ ¶
__getitem__ ¶
Source code in src/autotimm/data/instance_dataset.py
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 | |
CSV Format¶
image_path,mask_path,x1,y1,x2,y2,label
img1.jpg,masks/img1_inst1.png,10,20,100,150,car
img1.jpg,masks/img1_inst2.png,50,60,200,180,person
img2.jpg,masks/img2_inst1.png,30,40,120,200,car
- image_path: relative path to image
- mask_path: relative path to binary instance mask
- x1, y1, x2, y2: bounding box coordinates in
xyxyformat - label: class name
Usage Examples¶
Basic Usage¶
from autotimm.data import CSVInstanceDataset
import albumentations as A
from albumentations.pytorch import ToTensorV2
transform = A.Compose([
A.Resize(640, 640),
A.HorizontalFlip(p=0.5),
A.Normalize(),
ToTensorV2(),
], bbox_params=A.BboxParams(format='pascal_voc', label_fields=['labels']))
dataset = CSVInstanceDataset(
csv_path="annotations.csv",
images_dir="./images",
masks_dir="./masks",
transform=transform,
)
print(f"Classes: {dataset.classes}")
print(f"Num images: {len(dataset)}")
# Sample output
sample = dataset[0]
print(f"Boxes: {sample['boxes'].shape}") # [N, 4]
print(f"Labels: {sample['labels'].shape}") # [N]
print(f"Masks: {sample['masks'].shape}") # [N, H, W]
Custom Column Names¶
dataset = CSVInstanceDataset(
csv_path="annotations.csv",
images_dir="./data/images",
masks_dir="./data/masks",
image_column="filepath",
mask_column="mask_filepath",
bbox_columns=["xmin", "ymin", "xmax", "ymax"],
label_column="category",
)
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
csv_path |
str \| Path |
Required | Path to CSV file with annotations |
images_dir |
str \| Path |
Required | Directory containing images |
masks_dir |
str \| Path \| None |
None |
Directory containing masks (defaults to images_dir) |
image_column |
str |
"image_path" |
Name of image path column |
mask_column |
str |
"mask_path" |
Name of mask path column |
bbox_columns |
list[str] |
["x1", "y1", "x2", "y2"] |
Names of bbox coordinate columns |
label_column |
str |
"label" |
Name of label column |
transform |
Callable \| None |
None |
Albumentations transform with bbox and mask support |
Return Format¶
Returns a dictionary with:
| Key | Type | Description |
|---|---|---|
image |
Tensor [C, H, W] |
Transformed image |
boxes |
Tensor [N, 4] |
Bounding boxes in (x1, y1, x2, y2) format |
labels |
Tensor [N] |
Class indices |
masks |
Tensor [N, H, W] |
Binary instance masks |
image_id |
int |
Image index |
orig_size |
Tensor [2] |
Original image size (H, W) |
Best Practices¶
1. Image Paths¶
Use relative paths in CSV files:
# Good :material-check:
images/train/img001.jpg,cat
# Bad :material-close: (absolute paths break portability)
/home/user/data/images/train/img001.jpg,cat
2. CSV Validation¶
Validate your CSV before training:
from autotimm.data import CSVImageDataset
try:
dataset = CSVImageDataset(csv_path="train.csv", image_dir="./data")
print(f"Loaded {len(dataset)} samples")
print(f"Classes: {dataset.classes}")
except Exception as e:
print(f"CSV validation error: {e}")
3. Transform Consistency¶
Use the same transform backend (torchvision vs albumentations) for both dataset and data module:
# Consistent albumentations usage
from autotimm import ImageDataModule, TransformConfig
config = TransformConfig(preset="strong", backend="albumentations")
data = ImageDataModule(
train_csv="train.csv",
image_dir="./data",
transform_config=config,
)
4. Performance¶
For large CSV files:
- Use
persistent_workers=Truein DataModule - Increase
num_workersbased on available CPU cores - Use
pin_memory=Truewhen training on GPU
data = ImageDataModule(
train_csv="large_train.csv",
image_dir="./data",
num_workers=8,
persistent_workers=True,
pin_memory=True,
)
See Also¶
- CSV Data Loading Guide - Complete guide with examples
- CSV Data Loading Examples - Usage examples
- ImageDataModule - Classification data module
- DetectionDataModule - Detection data module
- MultiLabelImageDataModule - Multi-label data module