ultrayolo package¶
Submodules¶
ultrayolo.cli module¶
Console script for ultrayolo.
-
ultrayolo.cli.
load_datasets
(mode: str, image_shape: Tuple[int, int, int], max_objects: int, batch_size: int, base_grid_size: int, anchors: numpy.ndarray, train_path: str, val_path: str, augment: bool, pad_to_fixed_size: bool, **kwargs) → Tuple[ultrayolo.datasets.datasets.BaseDataset, ultrayolo.datasets.datasets.BaseDataset][source]¶ Load a dataset given the configurations
- Arguments:
mode {str} – a value in singlefile, multifile, coco. Repreents the format of the dataset image_shape {Image} – the shape of the image max_objects {int} – [description] batch_size {int} – the size of the batch base_grid_size {int} – the base size of the grid anchors {np.ndarray} – the anchors train_path {str} – the path to the training annotations val_path {str} – the path to the validation annotations augment {bool} – True of False to apply data augmentations pad_to_fixed_size {bool} – True to pad the images to fixed size without distorting the image
- Returns:
Tuple[datasets.BaseDataset, datasets.BaseDataset] – a valid instance of the training and validation dataset
-
ultrayolo.cli.
load_model
(masks: numpy.ndarray, dataset: ultrayolo.datasets.datasets.BaseDataset, iou: float, backbone: str, **kwargs) → ultrayolo.ultrayolo.BaseModel[source]¶ load the model for the training
- Arguments:
masks {np.ndarray} – the mask used dataset {datasets.BaseDataset} – the dataset used to train the model iou {float} – the value of the intersection over union backbone {str} – the backbone used for the model
- Returns:
[BaseModel] – an instance of the Yolo model
-
ultrayolo.cli.
load_or_compute_anchors
(mode: str, number: int, path: str, ds_mode: str, ds_train_path: str, image_shape: Tuple) → numpy.ndarray[source]¶ load or compute the anchors given a dataset
- Arguments:
mode {str} – siniglefile, multifile, coco number {int} – the number of anchors path {str} – the path to the anchors ds_mode {str} – the mode of the dataset ds_train_path {str} – the path to the dataset image_shape {tuple} – the shape of the image
- Returns:
np.ndarray – the anchors for the algorithm
-
ultrayolo.cli.
make_augmentations
(percentage: float = 0.2) → imgaug.augmenters.meta.Sequential[source]¶ apply data augmentations to the dataset
- Keyword Arguments:
percentage {float} – the percentage of the dataset to augment for each epoch (default: 0.2)
- Returns:
[iaa.Sequential] – a pipeline of transformations
ultrayolo.learningrates module¶
-
ultrayolo.learningrates.
cyclic_learning_rate
(learning_rate=0.01, max_lr=0.1, step_size=20.0, gamma=0.99994, mode='triangular')[source]¶ Applies cyclic learning rate (CLR). From the paper: Smith, Leslie N. “Cyclical learning rates for training neural networks.” 2017. [https://arxiv.org/pdf/1506.01186.pdf]
This method lets the learning rate cyclically
vary between reasonable boundary values achieving improved classification accuracy and often in fewer iterations.
This code varies the learning rate linearly between the
- minimum (learning_rate) and the maximum (max_lr).
- It returns the cyclic learning rate. It is computed as:
```python cycle = floor( 1 + global_step /
( 2 * step_size ) )
x = abs( global_step / step_size – 2 * cycle + 1 ) clr = learning_rate +
- Polices:
- ‘triangular’:
Default, linearly increasing then linearly decreasing the learning rate at each cycle.
- ‘triangular2’:
The same as the triangular policy except the learning rate difference is cut in half at the end of each cycle. This means the learning rate difference drops after each cycle.
- ‘exp_range’:
The learning rate varies between the minimum and maximum boundaries and each boundary value declines by an exponential factor of: gamma^global_step.
ultrayolo.losses module¶
-
class
ultrayolo.losses.
BaseLoss
(anchor_masks, ignore_iou_threshold, img_size, num_classes, name, num_batches)[source]¶ Bases:
object
-
class
ultrayolo.losses.
FocalLoss
(anchor_masks, ignore_iou_threshold, img_size, num_classes, name, num_batches)[source]¶ Bases:
ultrayolo.losses.BaseLoss
-
class
ultrayolo.losses.
YoloLoss
(anchor_masks, ignore_iou_threshold, img_size, num_classes, name, num_batches)[source]¶ Bases:
ultrayolo.losses.BaseLoss
-
ultrayolo.losses.
make_loss
(num_classes, anchors, masks, img_size, num_batches, ignore_iou_threshold=0.5, loss_name='yolo')[source]¶ helper to create the losses
- Arguments:
num_classes {int} – the number of classes anchors {np.ndarray} – the anchors used for the detection masks {np.ndarray} – the mask used to filter the anchors img_size {tuple} – the shape of the images num_batches {int} – the number of batches in the training
- Keyword Arguments:
ignore_iou_threshold {float} – the value used to ignore predictions (default: {0.7}) loss_name {str} – the loss function used (default: {yolo}) (values: {yolo, focal})
- Returns:
[type] – [description]
-
ultrayolo.losses.
process_predictions
[source]¶ process the predictions to transform from: - pred_xy, pred_wh, pred_obj, pred_class into - box_xyxy, pred_obj, pred_class, pred_xywh
- Arguments:
- y_pred {tf.tensor} – the predictions in the format
(NBATCH, x_center, y_center, width, heigth, obj, one_hot_classes)
num_classes {int} – the number of classes anchors_masks {tf.tensor} – the anchors masks
- Returns:
tuple – box_xyxy, pred_obj, pred_class, pred_xywh
ultrayolo.ultrayolo module¶
-
class
ultrayolo.ultrayolo.
BaseModel
(img_shape=(None, None, 3), max_objects=100, iou_threshold=0.5, score_threshold=0.5, anchors=None, num_classes=80, base_grid_size: int = 32, backbone='DarkNet', training=False)[source]¶ Bases:
object
-
compile
(optimizer, loss, run_eagerly, summary=True)[source]¶ compile the model
- Arguments:
optimizer {tf.keras.optimizers} – a valid tensorflow optimizer loss {ultrayolo.losses.Loss} – the loss function for yolo run_eagerly {bool} – if True is uses eager mode, that is you can see more explainable stack traces
- Keyword Arguments:
summary {bool} – if True print the summary of the model (default: {True})
-
fit
(train_dataset, val_dataset, epochs, initial_epoch=0, callbacks=None, workers=1, max_queue_size=64)[source]¶ train the model
- Arguments:
train_dataset {ultrayolo.datasets.Dataset} – an instance of the dataset val_dataset {ultrayolo.datasets.Dataset} – an instance of the dataset epochs {int} – the number of epochs
- Keyword Arguments:
initial_epoch {int} – the inital epoch (default: {0}) callbacks {[type]} – a list of callbacks for the model (default: {None}) workers {int} – the number of workers (default: {1}) max_queue_size {int} – the max size of the queue (default: {64})
- Returns:
[type] – [description]
-
get_loss_function
(num_batches, loss_name: str = 'yolo') → List[source]¶ utility to create the loss function
- Keyword Arguments:
- loss_fn {str} – a literal value for Yolo and Focal loss (default: {‘yolo’})
(values: {‘yolo’, ‘focal’})
- Returns:
List – [description]
-
get_optimizer
(optimizer_name, lrate)[source]¶ helper to create the optimizer using the class defined members
- Arguments:
optimizer_name {str} – the name of the optimizer to use: (values: adam, rmsprop, sgd) lrate {float} – a valid starting value for the learning rate
- Raises:
Exception: raise an exception if the optimizer is not supported
- Returns:
tensorflow.keras.optimizer – an instance of the selected optimizer
-
load_weights
(path)[source]¶ - load:
saved checkpoints in h5 format
saved weights in darknet format
- Arguments:
path {str} – the path where the weights are saved backbone {str} – the name of the backbone used
-
save
(path, save_format='h5')[source]¶ save the model to the given path
- Arguments:
path {str|pathlib.Path} – the path to save the checkpoint
-
set_mode_fine_tuning
(num_layers_to_train)[source]¶ unfreeze the backbone and freeze the first num_layers_to_train layers
- Arguments:
num_layers_to_train {[type]} – [description]
-
-
class
ultrayolo.ultrayolo.
YoloV3
(img_shape=(None, None, 3), max_objects=100, iou_threshold=0.5, score_threshold=0.5, anchors=None, num_classes=80, base_grid_size: int = 32, backbone='DarkNet', training=False)[source]¶ Bases:
ultrayolo.ultrayolo.BaseModel
-
default_anchors
= array([[ 10., 13.], [ 16., 30.], [ 33., 23.], [ 30., 61.], [ 62., 45.], [ 59., 119.], [116., 90.], [156., 198.], [373., 326.]], dtype=float32)¶
-
default_masks
= array([[6, 7, 8], [3, 4, 5], [0, 1, 2]])¶
-
-
class
ultrayolo.ultrayolo.
YoloV3Tiny
(img_shape=(None, None, 3), max_objects=100, iou_threshold=0.7, score_threshold=0.7, anchors=None, num_classes=80, base_grid_size: int = 32, backbone='DarknetTiny', training=False)[source]¶ Bases:
ultrayolo.ultrayolo.BaseModel
-
default_anchors
= array([[ 10., 14.], [ 23., 27.], [ 37., 58.], [ 81., 82.], [135., 169.], [344., 319.]], dtype=float32)¶
-
default_masks
= array([[3, 4, 5], [0, 1, 2]])¶
-
-
ultrayolo.ultrayolo.
non_max_suppression
[source]¶ an implementation of non max suppression
- Arguments:
outputs {tf.tensor} – the outputs of the yolo branches anchors {np.ndarray} – the anchors scaled in [0,1] masks {np.ndarray} – the list of the anchors to use classes {list} – the list of classes iou_threshold {float} – the minimum intersection over union threshold score_threshold {float} – the minimum confidence score to use max_boxes_per_image {int} – the number of maximum boxes to show img_size {int} – the size of the image
- Returns:
(boxes, scores, classes, valid_detections) – a tuple of the results
Module contents¶
Top-level package for ultrayolo.