ModelWrapper
ModelWrapper
is an object similar to keras.Model
that trains, test and predict on datasets.
Using our wrapper makes it easier to do Monte-Carlo sampling with the iterations
parameters.
Another optimization that we do is that instead of using a for-loop to perform MC sampling, we stack examples.
Example
from baal.modelwrapper import ModelWrapper
from baal.active.dataset import ActiveLearningDataset
from torch.utils.data import Dataset
# You define ModelWrapper with a Pytorch model and a criterion.
wrapper = ModelWrapper(model=your_model, criterion=your_criterion)
# Assuming you have your ActiveLearningDataset ready,
al_dataset: ActiveLearningDataset = ...
test_dataset: Dataset = ...
train_history = wrapper.train_on_dataset(al_dataset, optimizer=your_optimizer, batch_size=32, epoch=10, use_cuda=True)
# We can also use BMA during test time using `average_predictions`.
test_values = wrapper.test_on_dataset(test_dataset, average_predictions=20, **kwargs)
# We use Monte-Carlo sampling using the `iterations` arguments.
predictions = wrapper.predict_on_dataset(al_dataset.pool, iterations=20, **kwargs)
predictions.shape
# > [len(al_dataset.pool), num_outputs, 20]
API
baal.ModelWrapper
Bases: MetricMixin
Wrapper created to ease the training/testing/loading.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
nn.Module
|
The model to optimize. |
required |
criterion |
Callable
|
A loss function. |
required |
replicate_in_memory |
bool
|
Replicate in memory optional. |
True
|
Source code in baal/modelwrapper.py
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 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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
|
eval()
get_params()
load_state_dict(state_dict, strict=True)
predict_on_batch(data, iterations=1, cuda=False)
Get the model's prediction on a batch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Tensor
|
The model input. |
required |
iterations |
int
|
Number of prediction to perform. |
1
|
cuda |
bool
|
Use CUDA or not. |
False
|
Returns:
Type | Description |
---|---|
Tensor, the loss computed from the criterion. shape = {batch_size, nclass, n_iteration}. |
Source code in baal/modelwrapper.py
predict_on_dataset(dataset, batch_size, iterations, use_cuda, workers=4, collate_fn=None, half=False, verbose=True)
Use the model to predict on a dataset iterations
time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
Dataset to predict on. |
required |
batch_size |
int
|
Batch size to use during prediction. |
required |
iterations |
int
|
Number of iterations per sample. |
required |
use_cuda |
bool
|
Use CUDA or not. |
required |
workers |
int
|
Number of workers to use. |
4
|
collate_fn |
Optional[Callable]
|
The collate function to use. |
None
|
half |
bool
|
If True use half precision. |
False
|
verbose |
bool
|
If True use tqdm to show progress. |
True
|
Notes
The "batch" is made of batch_size
* iterations
samples.
Returns:
Type | Description |
---|---|
Array [n_samples, n_outputs, ..., n_iterations]. |
Source code in baal/modelwrapper.py
predict_on_dataset_generator(dataset, batch_size, iterations, use_cuda, workers=4, collate_fn=None, half=False, verbose=True)
Use the model to predict on a dataset iterations
time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
Dataset to predict on. |
required |
batch_size |
int
|
Batch size to use during prediction. |
required |
iterations |
int
|
Number of iterations per sample. |
required |
use_cuda |
bool
|
Use CUDA or not. |
required |
workers |
int
|
Number of workers to use. |
4
|
collate_fn |
Optional[Callable]
|
The collate function to use. |
None
|
half |
bool
|
If True use half precision. |
False
|
verbose |
bool
|
If True use tqdm to display progress |
True
|
Notes
The "batch" is made of batch_size
* iterations
samples.
Returns:
Type | Description |
---|---|
Generators [batch_size, n_classes, ..., n_iterations]. |
Source code in baal/modelwrapper.py
reset_all()
reset_fcs()
set_dataset_size(dataset_size)
Set state for dataset size. Useful for tracking.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset_size |
int
|
Dataset state |
required |
state_dict()
test_on_batch(data, target, cuda=False, average_predictions=1)
Test the current model on a batch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Tensor
|
The model input. |
required |
target |
Tensor
|
The ground truth. |
required |
cuda |
bool
|
Use CUDA or not. |
False
|
average_predictions |
int
|
The number of predictions to average to compute the test loss. |
1
|
Returns:
Type | Description |
---|---|
Tensor, the loss computed from the criterion. |
Source code in baal/modelwrapper.py
test_on_dataset(dataset, batch_size, use_cuda, workers=4, collate_fn=None, average_predictions=1)
Test the model on a Dataset dataset
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
Dataset to evaluate on. |
required |
batch_size |
int
|
Batch size used for evaluation. |
required |
use_cuda |
bool
|
Use Cuda or not. |
required |
workers |
int
|
Number of workers to use. |
4
|
collate_fn |
Optional[Callable]
|
The collate function to use. |
None
|
average_predictions |
int
|
The number of predictions to average to compute the test loss. |
1
|
Returns:
Type | Description |
---|---|
Average loss value over the dataset. |
Source code in baal/modelwrapper.py
train()
train_and_test_on_datasets(train_dataset, test_dataset, optimizer, batch_size, epoch, use_cuda, workers=4, collate_fn=None, regularizer=None, return_best_weights=False, patience=None, min_epoch_for_es=0)
Train and test the model on both Dataset train_dataset
, test_dataset
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
train_dataset |
Dataset
|
Dataset to train on. |
required |
test_dataset |
Dataset
|
Dataset to evaluate on. |
required |
optimizer |
Optimizer
|
Optimizer to use during training. |
required |
batch_size |
int
|
Batch size used. |
required |
epoch |
int
|
Number of epoch to train on. |
required |
use_cuda |
bool
|
Use Cuda or not. |
required |
workers |
int
|
Number of workers to use. |
4
|
collate_fn |
Optional[Callable]
|
The collate function to use. |
None
|
regularizer |
Optional[Callable]
|
The loss regularization for training. |
None
|
return_best_weights |
bool
|
If True, will keep the best weights and return them. |
False
|
patience |
Optional[int]
|
If provided, will use early stopping to stop after
|
None
|
min_epoch_for_es |
int
|
Epoch at which the early stopping starts. |
0
|
Returns:
Type | Description |
---|---|
History and best weights if required. |
Source code in baal/modelwrapper.py
train_on_batch(data, target, optimizer, cuda=False, regularizer=None)
Train the current model on a batch using optimizer
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Tensor
|
The model input. |
required |
target |
Tensor
|
The ground truth. |
required |
optimizer |
optim.Optimizer
|
An optimizer. |
required |
cuda |
bool
|
Use CUDA or not. |
False
|
regularizer |
Optional[Callable]
|
The loss regularization for training. |
None
|
Returns:
Type | Description |
---|---|
Tensor, the loss computed from the criterion. |
Source code in baal/modelwrapper.py
train_on_dataset(dataset, optimizer, batch_size, epoch, use_cuda, workers=4, collate_fn=None, regularizer=None)
Train for epoch
epochs on a Dataset `dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
Dataset
|
Pytorch Dataset to be trained on. |
required |
optimizer |
optim.Optimizer
|
Optimizer to use. |
required |
batch_size |
int
|
The batch size used in the DataLoader. |
required |
epoch |
int
|
Number of epoch to train for. |
required |
use_cuda |
bool
|
Use cuda or not. |
required |
workers |
int
|
Number of workers for the multiprocessing. |
4
|
collate_fn |
Optional[Callable]
|
The collate function to use. |
None
|
regularizer |
Optional[Callable]
|
The loss regularization for training. |
None
|
Returns:
Type | Description |
---|---|
The training history. |