utils package

Submodules

utils.collect module

A simple attribute dictionary used for representing configuration options.

class utils.collect.AttrDict(*args, **kwargs)

Bases: dict

IMMUTABLE = '__immutable__'
immutable(is_immutable)

Set immutability to is_immutable and recursively apply the setting to all nested AttrDicts.

is_immutable()

utils.config module

utils.config.cfg = {'OUTPUT_ROOT': '/tmp', 'PIXEL_MEANS': [0.579, 0.425, 0.626], 'PIXEL_STD': [0.234, 0.291, 0.154], 'SUMMARY': {'LOG_DIR': '../run'}, 'TEST': {'BATCH_SIZE': 1, 'ROOT': '../data/test'}, 'TRAIN': {'BASE_LR': 0.003, 'BATCH_SIZE': 96, 'BEST_MODEL_PATH': '/tmp/train/checkpoints/model_best.pkl', 'CHECKPOINT_PATH': '/tmp/train/checkpoints', 'EPOCHS': 50, 'GAMMA': 0.1, 'GPU_IDS': [0, 1, 2], 'OUTPUT_ROOT': '/tmp/train', 'ROOT': '../data/train', 'STEP_SIZE': 10, 'USE_CUDA': True, 'WARM_UP_EPOCHS': 5}}

utils.data_helper module

utils.data_helper.compute_mean_std(datasets)

return the mean and standard deviation value of the datasets

Parameters

datasets – a tuple of datasets(e.g. (train set, val set,))

Returns

the mean values and std values in (R, G, B) format

utils.data_helper.get_data_loader(dataset, batch_size=32, shuffle=True, sampler=None, to_tensor=True)

get the data loader for the training set or validation/test set

:param dataset :param batch_size :param shuffle :param sampler :return: dataloader

utils.data_helper.get_transform(mean=[0.5, 0.5, 0.5], std=[1, 1, 1], train=True, flip=True, rotation=False)

return the transform for the training set, validation/test set

Parameters
  • mean – mean values for normalization

  • std – std values for normalization

  • train – training set

  • flip – random flip augmentation(default: True)

  • rotation – rotation augmentation(default: False)

Returns

the transformation

utils.data_helper.preprocess(img, mean=[0.5, 0.5, 0.5], std=[1, 1, 1])

prepare the inputs for the network

Parameters
  • img – HWC, BGR format img

  • mean – mean values to normalize

  • std – std values to normalize

Returns

output tensor

utils.data_helper.split_dataset(dataset, frac)

split the dataset into two parts(e.g. full set into training set and validation set

Parameters
  • dataset – the dataset to be splitted

  • frac – the fraction of all subdatasets, type can either be a float number or list(must sum up to 1)

Returns

(setA, setB,…)

utils.focal_loss module

class utils.focal_loss.FocalLoss(gamma=2, alpha=None, reduction='mean')

Bases: torch.nn.modules.module.Module

forward(inputs, targets)

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

utils.infer module

utils.infer.infer(model, filepath, classes, mean=[0.5, 0.5, 0.5], std=[1, 1, 1])

predict the label of an image

Parameters
  • model – model used

  • filepath – path of the image

  • classes – classes list

  • mean – mean used to normalize

  • std – std used to normalize

Returns

the predicted class name of the image

utils.label_smooth_loss module

class utils.label_smooth_loss.LSRLoss(lsr_param=0.05, reduction='mean')

Bases: torch.nn.modules.module.Module

forward(inputs, labels)

we must convert the labels into one-hot format

utils.lr module

class utils.lr.ExponentialLR(optimizer, max_lr, num_iters, last_epoch=-1)

Bases: torch.optim.lr_scheduler._LRScheduler

get_lr(epoch=None)
class utils.lr.FindLR(model, optimizer, criterion, use_gpu=True)

Bases: object

plot(skip_start=15, skip_end=2, log_lr=True)
range_test(train_loader, val_loader=None, step_mode='exp', max_lr=10, num_iters=200, smooth_value=0.05, stop_thresh=5)
class utils.lr.LinearLR(optimizer, max_lr, num_iters, last_epoch=-1)

Bases: torch.optim.lr_scheduler._LRScheduler

get_lr(epoch=None)
class utils.lr.WarmUpLR(optimizer, total_iters, scale_factor=0.3333333333333333, step_mode='linear', last_epoch=-1)

Bases: torch.optim.lr_scheduler._LRScheduler

get_lr()

utils.model_builder module

utils.model_builder.get_network(model_name, num_classes=1000, pretrained=False, use_gpu=True, gpu_id=[0])

wrapper to get the network

Parameters

model_name – name of the model(e.g. vgg19, resnet18…)

Returns

the model

utils.test module

utils.test.test(model, test_loader)

get the accuracy on test set

Parameters
  • model – model to be tested

  • test_loader – data loader for the test dataset

Returns

accuracy

utils.train module

utils.train.train(model, train_loader, val_loader, criterion, optimizer, lr_scheduler, num_epochs=50, warmup_scheduler=None, warmup_epochs=0, summary_writer=None, use_cuda=True, log_interval=1)

utils.vis module

utils.vis.imshow_grid(imgs, sqrtimg=32)

visualize the images

Parameters
  • imgs – a list of RGB images

  • sqrtimg – scale of the resized grids

Returns

visualization result

Module contents