depth_anything_cpp icon indicating copy to clipboard operation
depth_anything_cpp copied to clipboard

onnx2trt

Open dapao-0808 opened this issue 1 year ago • 5 comments

Hi! Thank you very much for your work! I am getting the following error while converting Onnx model to TensorRT engine:

[03/25/2025-15:14:29] [W] [TRT] onnx2trt_utils.cpp:377: Your ONNX model has been generated with INT64 weights, while TensorRT does not natively support INT64. Attempting to cast down to INT32. [03/25/2025-15:14:29] [I] [TRT] No importer registered for op: LayerNormalization. Attempting to import as plugin. [03/25/2025-15:14:29] [I] [TRT] Searching for plugin: LayerNormalization, plugin_version: 1, plugin_namespace: [03/25/2025-15:14:29] [E] [TRT] ModelImporter.cpp:726: While parsing node number 23 [LayerNormalization -> "/blocks.0/norm1/LayerNormalization_output_0"]: [03/25/2025-15:14:29] [E] [TRT] ModelImporter.cpp:727: --- Begin node --- [03/25/2025-15:14:29] [E] [TRT] ModelImporter.cpp:728: input: "/Add_output_0" input: "pretrained.blocks.0.norm1.weight" input: "pretrained.blocks.0.norm1.bias" output: "/blocks.0/norm1/LayerNormalization_output_0" name: "/blocks.0/norm1/LayerNormalization" op_type: "LayerNormalization" attribute { name: "axis" i: -1 type: INT } attribute { name: "epsilon" f: 1e-06 type: FLOAT }

[03/25/2025-15:14:29] [E] [TRT] ModelImporter.cpp:729: --- End node --- [03/25/2025-15:14:29] [E] [TRT] ModelImporter.cpp:732: ERROR: builtin_op_importers.cpp:5428 In function importFallbackPluginImporter: [8] Assertion failed: creator && "Plugin not found, are the plugin name, version, and namespace correct?" [03/25/2025-15:14:29] [E] Failed to parse onnx file [03/25/2025-15:14:29] [I] Finished parsing network model. Parse time: 0.162889 [03/25/2025-15:14:29] [E] Parsing model failed [03/25/2025-15:14:29] [E] Failed to create engine from model or file. [03/25/2025-15:14:29] [E] Engine set up failed

I wonder if you have had similar problems? How should I go about solving it? My TensorRT version is 8.6.1.6.

dapao-0808 avatar Mar 25 '25 07:03 dapao-0808

Hi! Thank you for your interest in this project! Did you use trtexec tool to convert the model?

zz990099 avatar Mar 25 '25 07:03 zz990099

Hi! Thank you for your interest in this project! Did you use tool to convert the model?trtexec

yes

dapao-0808 avatar Mar 25 '25 07:03 dapao-0808

Sorry. We did not have similar problems.

Just wanted to confirm if this issue was caused by the environment and scripts we provided?

zz990099 avatar Mar 25 '25 08:03 zz990099

Sorry. We did not have similar problems.

Just wanted to confirm if this issue was caused by the environment and scripts we provided?

I have tried to use the onnx model provided by others, and it throws a different error, I don't know if it could be a problem when exporting the onnx model.

dapao-0808 avatar Mar 25 '25 08:03 dapao-0808

This this our onnx model export script for your reference.

import argparse
import cv2
import glob
import matplotlib
import numpy as np
import os
import torch

from depth_anything_v2.dpt import DepthAnythingV2


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Depth Anything V2')
    
    parser.add_argument('--img-path', type=str)
    parser.add_argument('--input-size', type=int, default=518)
    parser.add_argument('--outdir', type=str, default='./vis_depth')
    
    parser.add_argument('--encoder', type=str, default='vits', choices=['vits', 'vitb', 'vitl', 'vitg'])
    
    parser.add_argument('--pred-only', dest='pred_only', action='store_true', help='only display the prediction')
    parser.add_argument('--grayscale', dest='grayscale', action='store_true', help='do not apply colorful palette')
    
    args = parser.parse_args()
    
    DEVICE = 'cpu'
    
    model_configs = {
        'vits': {'encoder': 'vits', 'features': 64, 'out_channels': [48, 96, 192, 384]},
        'vitb': {'encoder': 'vitb', 'features': 128, 'out_channels': [96, 192, 384, 768]},
        'vitl': {'encoder': 'vitl', 'features': 256, 'out_channels': [256, 512, 1024, 1024]},
        'vitg': {'encoder': 'vitg', 'features': 384, 'out_channels': [1536, 1536, 1536, 1536]}
    }
    
    depth_anything = DepthAnythingV2(**model_configs[args.encoder])
    depth_anything.load_state_dict(torch.load(f'checkpoints/depth_anything_v2_{args.encoder}.pth', map_location='cpu'))
    depth_anything = depth_anything.to(DEVICE).eval()
    
    input = {"images": torch.zeros(1, 3, 518, 518).to(DEVICE)}

    torch.onnx.export(
        depth_anything,
        input,
        "depth_anything_v2_vits.onnx",
        opset_version=17,
        do_constant_folding=True,
        input_names=["images"],
        output_names=["depth"]
    )
    

zz990099 avatar Mar 25 '25 08:03 zz990099