Khadas VIM 3 NPU issues

Which Khadas SBC do you use?

VIM3 PRO

Which system do you use? Android, Ubuntu, OOWOW or others?

Ubuntu

Which version of system do you use? Khadas official images, self built images, or others?

Official Image

Please describe your issue below:

How do I use the NPU on the SBC.
I’m new to this and all documentation seems to be gone and repos no longer maintained
I followed this guide Instructions for KSNN conversion tool [Khadas Docs].
The python conversion tool is not working and gives exec format error.
Kindly help me getting it to convert the model to required format…
And also how does a python script utilize the NPU for inference?

Hello @Arjun_Gupta

@Electr1 will help you then.

1 Like

Hello @Arjun_Gupta

you need to use the convert tool on Ubuntu x86_64 linux host, it doesn’t work on VIM3 itself.

The KSNN library provides the python classes and interface with the NPU libraries.
you can check the examples to see how to load the model, run inference etc.

Regards.

2 Likes

Oh ok it wasn’t mentioned and since I don’t use linux on daily basis I dont have a linux PC so will WSL work?

Yes, you can use WSL

P.S no need to make a duplicate post

Ok so I was trying to convert YOLOv5 model via the sdk could you please explain me what im doing wrong since im a beginner and havent worked with any NPU based SBC before…

./convert --model-name yolov5s --platform pytorch --model yolov5s.pt --input-size-list '640,480,3' --inputs input --outputs yolov5/Predictions/Reshape_1 --mean-values '128 128 128 0.0078125' --quantized-dtype asymmetric_affine --source-files ./data/dataset/dataset0.txt --kboard VIM3 --print-level 0


--+ KSNN Convert tools v1.3 +--


Start import model ...
2023-11-06 14:45:03.304173: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /home/wsl/aml_npu_sdk/acuity-toolkit/bin/acuitylib:/tmp/_MEIh0Uh2z
2023-11-06 14:45:03.304194: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
I Namespace(config=None, import='pytorch', input_size_list='640,480,3', inputs='input', model='yolov5s.pt', output_data='Model.data', output_model='Model.json', outputs='yolov5/Predictions/Reshape_1', size_with_batch=None, which='import')
I Start importing pytorch...
[7267] Failed to execute script pegasus
Traceback (most recent call last):
  File "pegasus.py", line 131, in <module>
  File "pegasus.py", line 112, in main
  File "acuitylib/app/importer/commands.py", line 294, in execute
  File "acuitylib/vsi_nn.py", line 242, in load_pytorch_by_onnx_backend
  File "acuitylib/onnx_ir/frontend/pytorch_frontend/pytorch_frontend.py", line 45, in __init__
  File "torch/jit/__init__.py", line 228, in load
RuntimeError: [enforce fail at inline_container.cc:208] . file not found: archive/constants.pkl

Can you share your model ? I will check from my side.
Also provide your input example image and other parameters.

BTW I’m using it with a web cam stream.

@Arjun_Gupta it seems your model is using a different version of Pytorch operators than the SDK, you need to convert it to another model in a version supported by the convert tool.

You can try to convert as onnx model and convert it that way.

I have converted it still I am getting issues…

./convert --model-name yolov5s --platform onnx --model yolov5s.onnx --input-size-list '224,224,3' --inputs input --outputs yolov5s/Predictions/Reshape_1 --mean-values '128 128 128 0.00781
25' --quantized-dtype asymmetric_affine --source-files ./data/dataset/dataset0.txt --kboard VIM3 --print-level 0


--+ KSNN Convert tools v1.3 +--


Start import model ...
2023-11-06 18:20:07.930533: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /home/wsl/aml_npu_sdk/acuity-toolkit/bin/acuitylib:/tmp/_MEIlx34MR
2023-11-06 18:20:07.930559: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
I Namespace(config=None, import='pytorch', input_size_list='224,224,3', inputs='input', model='yolov5s.onnx', output_data='Model.data', output_model='Model.json', outputs='yolov5s/Predictions/Reshape_1', size_with_batch=None, which='import')
I Start importing pytorch...
[490] Failed to execute script pegasus
Traceback (most recent call last):
  File "pegasus.py", line 131, in <module>
  File "pegasus.py", line 112, in main
  File "acuitylib/app/importer/commands.py", line 294, in execute
  File "acuitylib/vsi_nn.py", line 242, in load_pytorch_by_onnx_backend
  File "acuitylib/onnx_ir/frontend/pytorch_frontend/pytorch_frontend.py", line 45, in __init__
  File "torch/jit/__init__.py", line 228, in load
RuntimeError: [enforce fail at inline_container.cc:143] . PytorchStreamReader failed reading zip archive: failed finding central directory

I have also tried this retinanet model-

Worked the conversion out but still having errors
Tried YOLOv8 and YOLOv5 in onnx as well as pb format
Im able to convert the inception v3 example model no problems but my model isn’t working

./convert --model-name yolov5s --platform onnx --model yolov5su.onnx --input-size-list '640,480,3' --inputs images --outputs output0 --mean-values '128 128 128 0.0078125' --quantized-dtype asymmetric_affine --source-files ./data/dataset/dataset1.txt --kboard VIM3 --print-level 0


--+ KSNN Convert tools v1.3 +--


Start import model ...
2023-11-07 00:36:50.259779: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'libcudart.so.10.1'; dlerror: libcudart.so.10.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: /home/wsl/aml_npu_sdk/acuity-toolkit/bin/acuitylib:/tmp/_MEIfeaOhy
2023-11-07 00:36:50.259799: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
I Namespace(import='onnx', input_dtype_list=None, input_size_list='640,480,3', inputs='images', model='yolov5su.onnx', output_data='Model.data', output_model='Model.json', outputs='output0', size_with_batch=None, which='import')
I Start importing onnx...
WARNING: ONNX Optimizer has been moved to https://github.com/onnx/optimizer.
All further enhancements and fixes to optimizers will be done in this new repo.
The optimizer code in onnx/onnx repo will be removed in 1.9 release.

W Call onnx.optimizer.optimize fail, skip optimize
I Current ONNX Model use ir_version 8 opset_version 17
I Call acuity onnx optimize 'eliminate_option_const' success
W The concat shape can not connect cause dim 3 not same shape of 2 and 1
W The concat shape can not connect cause dim 3 not same shape of 4 and 1
W Call acuity onnx optimize 'froze_const_branch' fail, skip this optimize
I Call acuity onnx optimize 'froze_if' success
I Call acuity onnx optimize 'merge_sequence_construct_concat_from_sequence' success
I Call acuity onnx optimize 'merge_lrn_lowlevel_implement' success
W The concat shape can not connect cause dim 3 not same shape of 2 and 1
W The concat shape can not connect cause dim 3 not same shape of 4 and 1
[6689] Failed to execute script pegasus
Traceback (most recent call last):
  File "pegasus.py", line 131, in <module>
  File "pegasus.py", line 112, in main
  File "acuitylib/app/importer/commands.py", line 245, in execute
  File "acuitylib/vsi_nn.py", line 171, in load_onnx
  File "acuitylib/app/importer/import_onnx.py", line 123, in run
  File "acuitylib/converter/onnx/convert_onnx.py", line 61, in __init__
  File "acuitylib/converter/onnx/convert_onnx.py", line 761, in _shape_inference
  File "acuitylib/onnx_ir/onnx_numpy_backend/shape_inference.py", line 65, in infer_shape
  File "acuitylib/onnx_ir/onnx_numpy_backend/smart_graph_engine.py", line 70, in smart_onnx_scanner
  File "acuitylib/onnx_ir/onnx_numpy_backend/smart_node.py", line 48, in calc_and_assign_smart_info
  File "acuitylib/onnx_ir/onnx_numpy_backend/smart_toolkit.py", line 770, in split_shape
  File "acuitylib/onnx_ir/onnx_numpy_backend/onnx_backend.py", line 105, in inference
  File "/home/wsl/aml_npu_sdk/acuity-toolkit/bin/acuitylib/acuitylib/onnx_ir/onnx_numpy_backend/ops/conv.py", line 96, in Conv
    groups
RuntimeError: Given groups=1, weight of size [32, 3, 6, 6], expected input[1, 640, 480, 3] to have 3 channels, but got 640 channels instead

Hello @Arjun_Gupta here your input shape is wrong, I checked the model using netron

The expected shape is 3,640,640.

$ ./convert --model-name yolov5s --platform onnx --model yolov5su.onnx --input-size-list '3,640,640' --inputs images --outputs output0 --mean-values '128 128 128 0.0078125' --quantized-dtype asymmetric_affine --source-files ./data/dataset/dataset1.txt --kboard VIM3 --print-level 0


--+ KSNN Convert tools v1.3 +--


Start import model ...
Done.import model success !!!


Start to Generate inputmeta ...
Done.Gerate inputmeta success !!!


Start quantize ...
Done.Quantize success !!!


Start export model ...
Done.Export model success !!!


All Done.

You can follow my command to convert the model without issue.

Yes it worked.
But will the mean values remain same or will they be changed?
Also my script seems to be not working for drawing bounding boxes over the objects
Could it be issue with torch-onnx conversion or with the script

Mean values can be the same.

It could be issue with your script, try printing the outputs and checking the values.

import numpy as np
import os      
import argparse
import sys
from ksnn.api import KSNN
from ksnn.types import *
import cv2 as cv
import time

classes = [
    "person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck",
    "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench",
    "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra",
    "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
    "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove",
    "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
    "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange",
    "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa",
    "pottedplant", "bed", "diningtable", "toilet", "tvmonitor", "laptop", "mouse",
    "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink",
    "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier",
    "toothbrush"
]

def draw_boxes(frame, class_id, confidence, x, y, w, h, color):
    cv.rectangle(frame, (x,y), (x+w,y+h), color, 2)
    label = str(classes[class_id])
    print(label)
    cv.putText(frame, label, (x,y), cv.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)

def softmax(x):
    return np.exp(x - np.max(x)) / np.sum(np.exp(x - np.max(x)))

if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    parser.add_argument("--library", help="Path to C static library file")
    parser.add_argument("--model", help="Path to nbg file")
    parser.add_argument("--level", help="Information printer level: 0/1/2")

    args = parser.parse_args()
    if args.model :
        if os.path.exists(args.model) == False:
            sys.exit('Model \'{}\' not exist'.format(args.model))
        model = args.model
    else :
        sys.exit("NBG file not found !!! Please use format: --model")
    if args.library :
        if os.path.exists(args.library) == False:
            sys.exit('C static library \'{}\' not exist'.format(args.library))
        library = args.library
    else :
        sys.exit("C static library not found !!! Please use format: --library")
    if args.level == '1' or args.level == '2' :
        level = int(args.level)
    else :
        level = 0

    resnet50 = KSNN('VIM3')
    print(' |--- KSNN Version: {} +---| '.format(resnet50.get_nn_version()))
    print('Start init neural network ...')
    resnet50.nn_init(library=library, model=model, level=level)
    print('Done.')
    
    cap = cv.VideoCapture(1)
    assert cap.isOpened()
    color = (0, 255, 0)
    while True:
        ret, frame = cap.read()
        if not ret:
            break
        frame = cv.resize(frame, (640, 480))
        outputs = resnet50.nn_inference(frame[:, :, ::-1], platform='ONNX')
        output = outputs[0]
        #print(output)
        output = np.reshape(output, (84, 8400))
        class_probabilities = softmax(output[:80])
        bounding_boxes = output[80:]
        #print(bounding_boxes.shape)
        for i in range(8400):
            class_id = np.argmax(class_probabilities[:, i])
            confidence = class_probabilities[class_id, i]
            if confidence > 0.5:
                x,y, w, h = bounding_boxes[:4, :i]
                draw_boxes(frame, class_id, confidence, x, y, w, h, color)
                print(x,y,w,h)
           
        cv.imshow('Camera Stream', frame)

        if cv.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv.destroyAllWindows()

this is my code
I am unable to make sense of the outputs…

Hello @Arjun_Gupta ,

I will check this today and let you know.

@Arjun_Gupta Can you provide the model and library you used with this code ?

@Electr1
khadas npu - Google Drive

@Arjun_Gupta it seems your code is wrong, it’s able to get outputs from the model after inference, maybe you need to check if you are getting the bounding box data for the class and drawing them correctly.

@Electr1 I am getting an error when trying to convert my tflite model the error occur after last layers conversion i dont what understand what this error indicates can you please help me. Check the script code below which i used for import.
$pegasus import tflite
–model ${NAME}.tflite
–inputs ‘input.1 input.7’
–input-size-list ‘1,640,640,3#1,640,640,3’
–size-with-batch ‘True#True’
–output-model ${NAME}.json
–output-data ${NAME}.data
–outputs ‘509 513 517 521’

here is the logs:
D Convert layer conv_2d
D Convert layer dequantize
D Convert layer conv_2d
D Convert layer conv_2d
D Convert layer dequantize
D Convert layer conv_2d
D Convert layer conv_2d
D Convert layer dequantize
D Convert layer conv_2d
D Convert layer conv_2d
D Convert layer dequantize
Traceback (most recent call last):
File “pegasus.py”, line 131, in
File “pegasus.py”, line 112, in main
File “acuitylib/app/importer/commands.py”, line 224, in execute
File “acuitylib/vsi_nn.py”, line 122, in load_tflite
File “acuitylib/vsi_nn.py”, line 709, in generate_inputmeta
File “acuitylib/app/console/inputmeta.py”, line 119, in generate_inputmeta
IndexError: list index out of range
[24800] Failed to execute script pegasus