This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TDA4VM: Yolov5/8 nano model training/inference

Part Number: TDA4VM

Tool/software:

Hi ,

I have a TDA4VM board with am68pa architecture and i want to train, compile and run a yolov5n or yolov8n model on the board, what is the procedure and steps to follow to do the same? I am successfully able to do it for yolov5s but cannot seem to do it for yolov5n

  • I want to be finetuning the model, not training from scratch

  • Hi Vendanth,

    What TIDL version are you using?  You cannot train or compile on the board.  You can compile and train on the host and run inference on the board.  Please include the model (preferred) or where you got it from.  There may be operators in the model that are not supported.

    Regards,

    Chris

  • Hi we are using tidl version 11_00_06_00 , i am aware that we cannot train and compile on the board.

    • i was doing it on the host itself.
    • Is there any documentation on the process for yolov5n and yolov8n
    • I am looking to finetune the model downloaded from ultralytics , we know we have to do model surgery, looking on how to do it on a pretrained model or a model trained by us using pytorch
  • HI Vedanth,

    You will probably need to convert the models to a lite format using model optimize.  I have include a script for this but before doing this you will probably need to  clone edgeai-tensorlab and install some modules.

    git clone github.com/.../edgeai-tensorlab.git
    cd edgeai-tensorlab/
    cd edgeai-modeloptimization/
    pip install .

    pip install torchvision

    pip install tqdm

    pip install netron

    The main call you will need to update your model to a TIDL friendly format is:

    edgeai_torchmodelopt.xmodelopt.surgery.v1.convert_to_lite_model()

    # surgery
    if args.model_surgery ==xmodelopt.surgery.SyrgeryVersion.SURGERY_V1:
        model = xmodelopt.surgery.v1.convert_to_lite_model(model)
        name_suffix = "_lite.onnx"
    elif args.model_surgery ==xmodelopt.surgery.SyrgeryVersion.SURGERY_FX:
       model = xmodelopt.surgery.v2.convert_to_lite_fx(model)
        name_suffix = "_lite.onnx"
    else:
        raise RuntimeError('unsupported surgery type')
    #

    Then save your model and try the import again.

    These are the yolo5 models supported by TIDL.

    EdgeAI-YOLOv5 Models

    YOLOv5 models have very good accuracy and are good candidates for low complexity inference. We have optimized YOLOv5 models to be friendly to TIDL. Take a look at the above link for more information, especially under the section "Models trained by TI". Pre-trained model checkpoints and pre-compiled modelartifacts are also published there.

    Dataset Model Name Input Size GigaMACS AP[0.5:0.95]% AP50% Available Notes
    COCO Yolov5s6_ti_lite_640 640x640 8.74 37.4 56.0 Y
    COCO Yolov5s6_ti_lite_576 576x576 7.08 36.6 55.7 (Train@ 640, val@576)
    COCO Yolov5s6_ti_lite_512 512x512 5.59 35.3 54.3 (Train@ 640, val@512)
    COCO Yolov5s6_ti_lite_448 448x448 4.28 34.0 52.3 (Train@ 640, val@448)
    COCO Yolov5s6_ti_lite_384 384x384 3.15 32.8 51.2 Y (Train@ 384, val@384)
    COCO Yolov5s6_ti_lite_320 320x320 2.19 30.3 47.6 (Train@ 384, val@320)
    COCO Yolov5m6_ti_lite_640 640x640 26.25 44.1 62.9 Y
    COCO Yolov5m6_ti_lite_576 576x576 21.26 43.0 61.9 (Train@ 640, val@576)
    COCO Yolov5m6_ti_lite_512 512x512 16.08 42.0 60.5 (Train@ 640, val@512)
    COCO Yolov5l6_ti_lite_640 640x640 58.92 47.1 65.6 Y

    This model is finetuned from the official ckpt for 100 epochs

    There are some variants of yolov8 supported but are not available on our site due to licensing restrictions. 

    #################################################################################
    # Copyright (c) 2018-2023, Texas Instruments Incorporated - http://www.ti.com
    # All Rights Reserved.
    #
    # Redistribution and use in source and binary forms, with or without
    # modification, are permitted provided that the following conditions are met:
    #
    # * Redistributions of source code must retain the above copyright notice, this
    #   list of conditions and the following disclaimer.
    #
    # * Redistributions in binary form must reproduce the above copyright notice,
    #   this list of conditions and the following disclaimer in the documentation
    #   and/or other materials provided with the distribution.
    #
    # * Neither the name of the copyright holder nor the names of its
    #   contributors may be used to endorse or promote products derived from
    #   this software without specific prior written permission.
    #
    # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    #
    #################################################################################
    
    
    import os
    import argparse
    import datetime
    import torch
    try:
        import torchvision
        has_tv = True
    except:
        has_tv = False
    import onnx
    import onnxsim
    from edgeai_torchmodelopt import xmodelopt
    
    def main(args):
        if not has_tv:
            print('This script is dependent on torchvision and as it is not installed, the script will close')
            return
        os.makedirs(args.output_path, exist_ok=True)
        all_models = ["mobilenet_v2"]
        # all_models = ["mobilenet_v2", "convnext_large"]
        # all_models = torchvision.models.list_models() # for all models
        # all_models = torchvision.models.list_models(module=torchvision.models) # for classification models
    
        print(all_models)
        input_tensor = torch.rand((1,3,224,224))
        for name in all_models:
            print(f"Exporting {name}")
            model = torchvision.models.get_model(name)
            output_path = os.path.join(args.output_path, name+".onnx")
            torch.onnx.export(model, input_tensor, output_path, opset_version=18)
            onnx.shape_inference.infer_shapes_path(output_path, output_path)
    
            # surgery
            if args.model_surgery ==xmodelopt.surgery.SyrgeryVersion.SURGERY_V1:
                model = xmodelopt.surgery.v1.convert_to_lite_model(model)
                name_suffix = "_lite.onnx"
            elif args.model_surgery ==xmodelopt.surgery.SyrgeryVersion.SURGERY_FX:
                model = xmodelopt.surgery.v2.convert_to_lite_fx(model)
                name_suffix = "_lite.onnx"
            else:
                raise RuntimeError('unsupported surgery type')
            #
    
            # quantization
            # Note: change total_epochs  to the epochs in your training script.
            total_epochs = 1
    
            if args.quantization:
                if args.quantization == xmodelopt.quantization.QuantizationVersion.QUANTIZATION_LEGACY:
                    model = xmodelopt.quantization.v1.QuantTrainModule(model, dummy_input=input_tensor, total_epochs=total_epochs)
                    name_suffix = "_quant" + name_suffix
                elif args.quantization == xmodelopt.quantization.QuantizationVersion.QUANTIZATION_FX:
                    model = xmodelopt.quantization.v2.QATFxModule(model, qconfig_type=args.quantization_type, total_epochs=total_epochs)
                    name_suffix = "_quant" + name_suffix
                else:
                    raise RuntimeError('unsupported surgery type')
                #
    
                # a forward pass is requied for quantization / convert to work
                model(input_tensor)
                model_converted = model.convert() if hasattr(model, 'convert') else model
            else:
                model_converted = model
    
            output_path = os.path.join(args.output_path, name+name_suffix)
            torch.onnx.export(model_converted, input_tensor, output_path, opset_version=18)
            # onnx_model = onnx.load(output_path)
            # onnx_model, _ = onnxsim.simplify(onnx_model)
            # onnx.save(onnx_model, output_path)
            onnx.shape_inference.infer_shapes_path(output_path, output_path)
    
    
    if __name__ == "__main__":
        date = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
        parser = argparse.ArgumentParser(description="PyTorch Model Surgery Examples")
        parser.add_argument("--model-surgery", default=xmodelopt.surgery.SyrgeryVersion.SURGERY_FX, type=str, help="model surgery type")
        parser.add_argument("--quantization", "--quantize", dest="quantization", default=0, type=int, choices=xmodelopt.quantization.QuantizationVersion.get_choices(), help="Quaantization Aware Training (QAT)")
        parser.add_argument("--quantization-type", default=None, help="Actual Quaantization Flavour - applies only if quantization is enabled")
        parser.add_argument("--output-path", default=f"./data/checkpoints/{date}_export", type=str, help="dataset path")
        args = parser.parse_args()
        main(args)
    

    Regards,

  • Hi i have tried a few things as suggested and these are few things i have questions about

    • Does TIDL support yolov5n model as well or only s variant onwards?
    • I tried performing model surgery on a pretrained yolov5s model from ultralytics, with the help of the script you provided. The compilation is happening although i am not getting any bounding boxes in the output image generated by edgeai-tidl-tools.
    • I have used edgeai-yolov5 repo to finetune train a yolov5s model and it seems to be working fine, is there something similar possible for yolov5n? Because yolov5n has a different backbone and was introduced in v6.0 
    • Does TIDL support yolov5n at all?
  • Hi Vendanth,

    • Does TIDL support yolov5n model as well or only s variant onwards?
      • TIDL supports the S variant with model optimizer and surgery, you may get the yolo5n to work
    • I tried performing model surgery on a pretrained yolov5s model from ultralytics, with the help of the script you provided. The compilation is happening although i am not getting any bounding boxes in the output image generated by edgeai-tidl-tools
      • Good to hear that helped.  Look for a prototex file associated with the model for the bounding boxes and object ID
    • I have used edgeai-yolov5 repo to finetune train a yolov5s model and it seems to be working fine, is there something similar possible for yolov5n? Because yolov5n has a different backbone and was introduced in v6.0 
      • Training should work the same
    • Does TIDL support yolov5n at all?
      • See first bullet

    I recommend starting with models already in the model zoo as they are supported out of the box.  Going with a random model from the wild will take you longer.

    Regards,

    Chris

  • I am able to train yolov5n from scratch if i change yolov5n.yaml to match the backbone from v5.0, but i want to be fine tuning it, which i am unable to do from edgeai-yolov5 repository because the yolov5n was started in v6.0 of ultralytics with a different backbone layers and different layers, for example SPPF , which is not present in the older versions and hence i get an error saying these layers do no exist when i try finetuning it from edgeai-yolov5, is there a pretrained yolov5n model available?

  • Hi Vendanth,

    There is only a yolo5s available in the model zoo.

    Regards,

    Chris