mapreader.classify.classifier_context
Module Contents
Classes
- class mapreader.classify.classifier_context.ClassifierContextContainer(model, dataloaders, labels_map, device='default', input_size=(224, 224), is_inception=False, load_path=None, force_device=False, **kwargs)
Bases:
mapreader.classify.classifier.ClassifierContainer
- Parameters:
model (Union[str, torch.nn.Module, None]) –
dataloaders (Union[Dict[str, torch.utils.data.DataLoader], None]) –
labels_map (Union[Dict[int, str], None]) –
device (Optional[str]) –
input_size (Optional[int]) –
is_inception (Optional[bool]) –
load_path (Optional[str]) –
force_device (Optional[bool]) –
- train(phases=['train', 'val'], num_epochs=25, save_model_dir='models', verbosity_level=1, tensorboard_path=None, tmp_file_save_freq=2, remove_after_load=True, print_info_batch_freq=5)
Train the model on the specified phases for a given number of epochs. Wrapper function for
train_core
method to capture exceptions (with supported exceptions so far:KeyboardInterrupt
). Refer totrain_core
for more information.- Parameters:
phases (list of str, optional) – The phases to train the model on for each epoch. Default is
["train", "val"]
.num_epochs (int, optional) – The number of epochs to train the model for. Default is
25
.save_model_dir (str or None, optional) – The directory to save the model in. Default is
"models"
. If set toNone
, the model is not saved.verbosity_level (int, optional) –
The level of verbosity during training:
0
is silent,1
is progress bar and metrics,2
is detailed information.
Default is
1
.tensorboard_path (str or None, optional) – The path to the directory to save TensorBoard logs in. If set to
None
, no TensorBoard logs are saved. Default isNone
.tmp_file_save_freq (int, optional) – The frequency (in epochs) to save a temporary file of the model. Default is
2
. If set to0
orNone
, no temporary file is saved.remove_after_load (bool, optional) – Whether to remove the temporary file after loading it. Default is
True
.print_info_batch_freq (int, optional) – The frequency (in batches) to print training information. Default is
5
. If set to0
orNone
, no training information is printed.
- Returns:
The function saves the model to the
save_model_dir
directory, and optionally to a temporary file. If interrupted with aKeyboardInterrupt
, the function tries to load the temporary file. If no temporary file is found, it continues without loading.- Return type:
None
- train_core(phases=['train', 'val'], num_epochs=25, save_model_dir='models', verbosity_level=1, tensorboard_path=None, tmp_file_save_freq=2, print_info_batch_freq=5)
Trains/fine-tunes a classifier for the specified number of epochs on the given phases using the specified hyperparameters.
- Parameters:
phases (list of str, optional) – The phases to train the model on for each epoch. Default is
["train", "val"]
.num_epochs (int, optional) – The number of epochs to train the model for. Default is
25
.save_model_dir (str or None, optional) – The directory to save the model in. Default is
"models"
. If set toNone
, the model is not saved.verbosity_level (int, optional) –
The level of verbosity during training:
0
is silent,1
is progress bar and metrics,2
is detailed information.
Default is
1
.tensorboard_path (str or None, optional) – The path to the directory to save TensorBoard logs in. If set to
None
, no TensorBoard logs are saved. Default isNone
.tmp_file_save_freq (int, optional) – The frequency (in epochs) to save a temporary file of the model. Default is
2
. If set to0
orNone
, no temporary file is saved.print_info_batch_freq (int, optional) – The frequency (in batches) to print training information. Default is
5
. If set to0
orNone
, no training information is printed.
- Raises:
ValueError –
If the criterion is not set. Use the
add_criterion
method to set the criterion.If the optimizer is not set and the phase is “train”. Use the
initialize_optimizer
oradd_optimizer
method to set the optimizer.KeyError – If the specified phase cannot be found in the object’s dataloader with keys.
- Return type:
None
- show_sample(set_name='train', batch_number=1, print_batch_info=True, figsize=(15, 10))
Displays a sample of training or validation data in a grid format with their corresponding class labels.
- Parameters:
set_name (str, optional) – Name of the dataset (
train
/validation
) to display the sample from, by default"train"
.batch_number (int, optional) – Number of batches to display, by default
1
.print_batch_info (bool, optional) – Whether to print information about the batch size, by default
True
.figsize (tuple, optional) – Figure size (width, height) in inches, by default
(15, 10)
.
- Returns:
Displays the sample images with their corresponding class labels.
- Return type:
None
- Raises:
StopIteration – If the specified number of batches to display exceeds the total number of batches in the dataset.
Notes
This method uses the dataloader of the
ImageClassifierData
class and thetorchvision.utils.make_grid
function to display the sample data in a grid format. It also calls the_imshow
method of theImageClassifierData
class to show the sample data.
- generate_layerwise_lrs(min_lr, max_lr, spacing='linspace', sep_group_names=['features1', 'features2'])
Calculates layer-wise learning rates for a given set of model parameters.
- Parameters:
min_lr (float) – The minimum learning rate to be used.
max_lr (float) – The maximum learning rate to be used.
spacing (str, optional) – The type of sequence to use for spacing the specified interval learning rates. Can be either
"linspace"
or"geomspace"
, where “linspace” uses evenly spaced learning rates over a specified interval and “geomspace” uses learning rates spaced evenly on a log scale (a geometric progression). By default"linspace"
.sep_group_names (list, optional) – A list of strings containing the names of parameter groups. Layers belonging to each group will be assigned the same learning rate. Defaults to
["features1", "features2"]
.
- Returns:
A list of dictionaries containing the parameters and learning rates for each layer.
- Return type:
list of dicts
- show_inference_sample_results(label, num_samples=6, set_name='train', min_conf=None, max_conf=None, figsize=(15, 15))
Shows a sample of the results of the inference.
- Parameters:
label (str, optional) – The label for which to display results.
num_samples (int, optional) – The number of sample results to display. Defaults to
6
.set_name (str, optional) – The name of the dataset split to use for inference. Defaults to
"train"
.min_conf (float, optional) – The minimum confidence score for a sample result to be displayed. Samples with lower confidence scores will be skipped. Defaults to
None
.max_conf (float, optional) – The maximum confidence score for a sample result to be displayed. Samples with higher confidence scores will be skipped. Defaults to
None
.figsize (tuple[int, int], optional) – Figure size (width, height) in inches, displaying the sample results. Defaults to
(15, 15)
.
- Return type:
None