CelebA Object Detection (YoloV7)
This example will demonstrate how to integrate YoloV7 to the Tensorleap system. The architecture we use for this example is the YoloV7-tiny 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 helpers section.
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).
Preprocess
The following method downloads our input text files from the public cloud, reads them, and parses the first NUM_SAMPLES entries from each file.
Input Images
this method downloads the images from our cloud, loads them, and then resizes them to a specific IMAGE_SIZE
Ground Truth
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
Complete Dataset Script
The complete Face Detection dataset script could be found here.
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:
python export.py --weights WEIGHTS_PATH --grid --simplify --img-size 640 640 --max-wh 640
Where WEIGHTS_PATH
is the .PT weights and 640 is the resolution of the input images.
Example ONNX model
Our YoloV7 exported model could be found here.
Model Integration
Following the import model guide we can now upload the ONNX model to the platform.
Removing last node
This model has a redundant node
added to it at upload time - it should be removed.
Setting up the model
To set up the model, we need to first move the dataset node from the left-most part of the model to the right.
We should then select the YOLO parsed dataset on the dataset node. and connect several nodes:
The GT visualizer (visualize GT BB)
Prediction visualizer (visualize prediction BB)
Image visualizer (visualize input)
Custom Loss
Don't forget to choose the loss within the dropdown menu after adding the loss node
Optimizer
Metrics
After connecting these nodes you should save the model (by overriding current version)
At this point both the dataset and the model is integrated into the platform. You can run evaluate and training
Last updated