Data generator
DataGenerator
¤
Bases: Sequence
Infinite Data Generator which automatically creates batches from a list of samples.
The created batches are model ready. This generator can be supplied directly to a NeuralNetwork train() & predict() function (also compatible to tensorflow.keras.model fit() & predict() function).
The DataGenerator is the second of the three pillars of AUCMEDI.
Pillars of AUCMEDI
The DataGenerator can be used for training, validation as well as for prediction.
Example
# Import
from aucmedi import *
# Initialize model
model = NeuralNetwork(
n_labels=8,
channels=3,
architecture="2D.ResNet50"
)
# Do some training
datagen_train = DataGenerator(
samples=samples[:100],
path_imagedir="images_dir/",
image_format=image_format,
labels=class_ohe[:100],
resize=model.meta_input,
standardize_mode=model.meta_standardize
)
model.train(datagen_train, epochs=50)
# Do some predictions
datagen_test = DataGenerator(
samples=samples[100:150],
path_imagedir="images_dir/",
image_format=image_format,
labels=None,
resize=model.meta_input,
standardize_mode=model.meta_standardize
)
preds = model.predict(datagen_test)
It supports real-time batch generation as well as beforehand preprocessing of images, which are then temporarily stored on disk (requires enough disk space!).
The resulting batches are created based the following pipeline:
- Image Loading
- Application of Subfunctions
- Resize image
- Application of Data Augmentation
- Standardize image
- Stacking processed images to a batch
Warning
When instantiating a DataGenerator
, it is highly recommended, to pass the image_format
parameter provided
by the input_interface()
and the resize
& standardize_mode
parameters provided by the
NeuralNetwork
class attributes meta_input
& meta_standardize
.
It assures, that the samples contain the expected file extension, input shape and standardization.
Build on top of the library
Tensorflow.Keras Iterator: https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/Iterator
Example: How to integrate metadata in AUCMEDI?
from aucmedi import *
import numpy as np
my_metadata = np.random.rand(len(samples), 10)
my_model = NeuralNetwork(n_labels=8, channels=3, architecture="2D.DenseNet121",
meta_variables=10)
my_dg = DataGenerator(samples, "images_dir/",
labels=None, metadata=my_metadata,
resize=my_model.meta_input, # (224,224)
standardize_mode=my_model.meta_standardize) # "torch"
Source code in aucmedi/data_processing/data_generator.py
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 |
|
__init__(samples, path_imagedir, labels=None, metadata=None, image_format=None, subfunctions=[], batch_size=32, resize=(224, 224), standardize_mode='z-score', data_aug=None, shuffle=False, grayscale=False, sample_weights=None, workers=1, prepare_images=False, loader=image_loader, seed=None, **kwargs)
¤
Initialization function of the DataGenerator which acts as a configuration hub.
If using for prediction, the 'labels' parameter has to be None
.
For more information on Subfunctions, read here: aucmedi.data_processing.subfunctions.
Data augmentation is applied even for prediction if a DataAugmentation object is provided!
Warning
Augmentation should only be applied to a training DataGenerator!
For test-time augmentation, aucmedi.ensemble.augmenting should be used.
Applying None
to resize
will result into no image resizing. Default (224, 224)
IO_loader Functions
Interface | Description |
---|---|
image_loader() | Image Loader for image loading via Pillow. |
sitk_loader() | SimpleITK Loader for loading NIfTI (nii) or Metafile (mha) formats. |
numpy_loader() | NumPy Loader for image loading of .npy files. |
cache_loader() | Cache Loader for passing already loaded images. |
More information on IO_loader functions can be found here: aucmedi.data_processing.io_loader.
Parameters defined in **kwargs
are passed down to IO_loader functions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
samples |
list of str
|
List of sample/index encoded as Strings. Provided by input_interface. |
required |
path_imagedir |
str
|
Path to the directory containing the images. |
required |
labels |
numpy.ndarray
|
Classification list with One-Hot Encoding. Provided by input_interface. |
None
|
metadata |
numpy.ndarray
|
NumPy Array with additional metadata. Have to be shape (n_samples, meta_variables). |
None
|
image_format |
str
|
Image format to add at the end of the sample index for image loading. Provided by input_interface. |
None
|
subfunctions |
List of Subfunctions
|
List of Subfunctions class instances which will be SEQUENTIALLY executed on the data set. |
[]
|
batch_size |
int
|
Number of samples inside a single batch. |
32
|
resize |
tuple of int
|
Resizing shape consisting of a X and Y size. (optional Z size for Volumes) |
(224, 224)
|
standardize_mode |
str
|
Standardization modus in which image intensity values are scaled. Calls the Standardize Subfunction. |
'z-score'
|
data_aug |
Augmentation Interface
|
Data Augmentation class instance which performs diverse augmentation techniques.
If |
None
|
shuffle |
bool
|
Boolean, whether dataset should be shuffled. |
False
|
grayscale |
bool
|
Boolean, whether images are grayscale or RGB. |
False
|
sample_weights |
list of float
|
List of weights for samples. Can be computed via compute_sample_weights(). |
None
|
workers |
int
|
Number of workers. If n_workers > 1 = use multi-threading for image preprocessing. |
1
|
prepare_images |
bool
|
Boolean, whether all images should be prepared and backup to disk before training. Recommended for large images or volumes to reduce CPU computing time. |
False
|
loader |
io_loader function
|
Function for loading samples/images from disk. |
image_loader
|
seed |
int
|
Seed to ensure reproducibility for random function. |
None
|
**kwargs |
dict
|
Additional parameters for the sample loader. |
{}
|
Source code in aucmedi/data_processing/data_generator.py
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 |
|
preprocess_image(index, prepared_image=False, run_aug=True, run_standardize=True, dump_pickle=False)
¤
Internal preprocessing function for applying Subfunctions, augmentation, resizing and standardization on an image given its index.
Can be utilized for debugging purposes.
Activating the prepared_image option also allows loading a beforehand preprocessed image from disk.
Deactivating the run_aug & run_standardize option to output image without augmentation and standardization.
Activating dump_pickle will store the preprocessed image as pickle on disk instead of returning.
Source code in aucmedi/data_processing/data_generator.py
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 |
|