MultiLabelImageDataModule¶
Lightning data module for multi-label image classification from CSV files.
Overview¶
MultiLabelImageDataModule loads multi-label datasets where each image can belong to multiple classes. It reads CSV files with binary label columns and pairs with ImageClassifier(multi_label=True).
Also provides MultiLabelImageDataset for custom data loading.
API Reference¶
autotimm.MultiLabelImageDataModule ¶
Bases: LightningDataModule
Lightning data module for multi-label image classification.
Reads CSV files where each row contains an image path and binary label
columns. See :class:~autotimm.data.dataset.MultiLabelImageDataset
for the expected CSV format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
train_csv
|
str | Path
|
Path to training CSV file. |
required |
image_dir
|
str | Path
|
Root directory for image paths. |
'.'
|
val_csv
|
str | Path | None
|
Optional path to validation CSV. If |
None
|
test_csv
|
str | Path | None
|
Optional path to test CSV. |
None
|
label_columns
|
list[str] | None
|
List of label column names. If |
None
|
image_column
|
str | None
|
Column name for image paths. Default: first column. |
None
|
image_size
|
int
|
Target image size (square). |
224
|
batch_size
|
int
|
Batch size for all dataloaders. |
32
|
num_workers
|
int
|
Number of data-loading workers. Defaults to |
min(cpu_count() or 4, 4)
|
val_split
|
float
|
Fraction for validation split when |
0.1
|
train_transforms
|
Callable | None
|
Custom training transforms. |
None
|
eval_transforms
|
Callable | None
|
Custom eval transforms. |
None
|
augmentation_preset
|
str | None
|
Built-in preset name. |
None
|
transform_backend
|
str
|
|
'torchvision'
|
transform_config
|
TransformConfig | None
|
Optional :class: |
None
|
backbone
|
str | Module | None
|
Backbone name or module for timm transform resolution. |
None
|
pin_memory
|
bool
|
Pin memory for GPU transfer. |
True
|
persistent_workers
|
bool
|
Keep workers alive between epochs. |
False
|
prefetch_factor
|
int | None
|
Number of batches prefetched per worker. |
None
|
Source code in src/autotimm/data/multilabel_datamodule.py
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 | |
__init__ ¶
__init__(train_csv: str | Path, image_dir: str | Path = '.', val_csv: str | Path | None = None, test_csv: str | Path | None = None, label_columns: list[str] | None = None, image_column: str | None = None, image_size: int = 224, batch_size: int = 32, num_workers: int = min(os.cpu_count() or 4, 4), val_split: float = 0.1, train_transforms: Callable | None = None, eval_transforms: Callable | None = None, augmentation_preset: str | None = None, transform_backend: str = 'torchvision', transform_config: TransformConfig | None = None, backbone: str | Module | None = None, pin_memory: bool = True, persistent_workers: bool = False, prefetch_factor: int | None = None)
Source code in src/autotimm/data/multilabel_datamodule.py
setup ¶
Source code in src/autotimm/data/multilabel_datamodule.py
train_dataloader ¶
val_dataloader ¶
test_dataloader ¶
Source code in src/autotimm/data/multilabel_datamodule.py
Usage Examples¶
Basic Usage¶
from autotimm import MultiLabelImageDataModule
data = MultiLabelImageDataModule(
train_csv="train.csv",
image_dir="./images",
val_csv="val.csv",
image_size=224,
batch_size=32,
)
data.setup("fit")
print(f"Labels: {data.num_labels}")
print(f"Label names: {data.label_names}")
With Auto Validation Split¶
With Albumentations¶
data = MultiLabelImageDataModule(
train_csv="train.csv",
image_dir="./images",
transform_backend="albumentations",
augmentation_preset="strong",
)
With Explicit Label Columns¶
data = MultiLabelImageDataModule(
train_csv="train.csv",
image_dir="./images",
label_columns=["cat", "dog"],
image_column="filepath",
)
Full Training Pipeline¶
from autotimm import AutoTrainer, ImageClassifier, MetricConfig
data = MultiLabelImageDataModule(
train_csv="train.csv",
image_dir="./images",
val_csv="val.csv",
image_size=224,
batch_size=32,
)
data.setup("fit")
model = ImageClassifier(
backbone="resnet50",
num_classes=data.num_labels,
multi_label=True,
metrics=[
MetricConfig(
name="accuracy",
backend="torchmetrics",
metric_class="MultilabelAccuracy",
params={"num_labels": data.num_labels},
stages=["train", "val"],
prog_bar=True,
),
],
)
trainer = AutoTrainer(max_epochs=10)
trainer.fit(model, datamodule=data)
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
train_csv |
str \| Path |
Required | Path to training CSV |
image_dir |
str \| Path |
"." |
Root directory for images |
val_csv |
str \| Path \| None |
None |
Validation CSV |
test_csv |
str \| Path \| None |
None |
Test CSV |
label_columns |
list[str] \| None |
None |
Label columns (auto-detected) |
image_column |
str \| None |
None |
Image column (first column) |
image_size |
int |
224 |
Target image size |
batch_size |
int |
32 |
Batch size |
num_workers |
int |
4 |
Data loading workers |
val_split |
float |
0.1 |
Validation split fraction |
train_transforms |
Callable \| None |
None |
Custom train transforms |
eval_transforms |
Callable \| None |
None |
Custom eval transforms |
augmentation_preset |
str \| None |
None |
Preset name |
transform_backend |
str |
"torchvision" |
"torchvision" or "albumentations" |
transform_config |
TransformConfig \| None |
None |
Transform configuration |
backbone |
str \| nn.Module \| None |
None |
Backbone for model normalization |
pin_memory |
bool |
True |
Pin memory for GPU |
persistent_workers |
bool |
False |
Keep workers alive |
prefetch_factor |
int \| None |
None |
Prefetch batches |
Attributes¶
| Attribute | Type | Description |
|---|---|---|
num_labels |
int \| None |
Number of labels (after setup) |
label_names |
list[str] \| None |
Label column names (after setup) |
train_dataset |
Dataset \| None |
Training dataset (after setup) |
val_dataset |
Dataset \| None |
Validation dataset (after setup) |
test_dataset |
Dataset \| None |
Test dataset (after setup) |
CSV Format¶
See Also¶
- CSV Data Loading API - Direct dataset API for multi-label CSV data
- ImageDataModule - Single-label classification data module
- CSV Data Loading Guide - Complete guide with examples
- TransformConfig - Unified transform configuration