This example will demonstrate how to integrate to the Tensorleap system. The architecture we use for this example is the model, trained on the CelebA full dataset on 1 class (faces).
The starting point for this example is having a trained model PyTorch weights (.pt) that was trained using the YoloV7 repository.
Dataset Script
To use the CelebA dataset for object detection, we set up the CelebA dataset into an images and labels folders, and created the corresponding .txt files according to the YOLOv7 specs.
In the following entries we provide an in-depth description of the main components of our dataset, following by the complete dataset script
Key components
Before going to each component in depth, two things are important to note:
The GT function converts a YOLO-format: [class,X,Y,W,H] and outputs [X,Y,W,H,class]
The input function return a channels-last image [H,W,3]
YoloV7 utils
YOLOv7 requires a decoder (so we can view the images), a custom loss (that is composed of an object & class & IOU losses), and a specific grid definition to be able to map the predictions to the priors. A complete description of these elements and their configuration could be found in the helperssection.
Here, we set up the YOLO utils with a YOLO-tiny config. This includes the loss config (overlap threshold, maximum matches, weights), the decoder config (NMS & confidence threshold, top_k and max bb to plot) and the Grid config (heads size, strides, and image size).
This method reads the YOLO-format labels files and returns a [X,Y,W,X,class_idx] encoded ground truth, with a MAX_BB_PER_IMAGE GT instances per image
def get_bb(idx: int, data: PreprocessResponse) -> NDArray[np.double]:
"""
returns an array shaped (MAX_BB_PER_IMAGE, 5) where the channel idx is [X,Y,W,H] normalized to [0,1]
"""
data = data.data
filepath = data['label_path'][idx]
fpath = _download(filepath)
with open(fpath, 'r') as f:
gt_list = [x.split() for x in f.read().strip().splitlines()]
bboxes = np.zeros([MAX_BB_PER_IMAGE, 5])
max_anns = min(MAX_BB_PER_IMAGE, len(gt_list))
for i, gt_entry in enumerate(gt_list):
ann = gt_entry
bboxes[i, :4] = np.array(ann[1:]).astype(float)
bboxes[i, 4] = np.array(ann[0]).astype(float)
bboxes[max_anns:, 4] = BACKGROUND_LABEL
return bboxes
Complete Dataset Script
Exporting an ONNX Model
After the PyTorch training is finished, an ONNX model should be exported using the YOLOv7 export script. YOLOv7 has multiple export options, but the one that would allow the easiest integration with the TensorLeap system is exporting the model without NMS, but with the decoder.
To export the PyTorch model to ONNX you should execute the following command: