Samsung / ONE

On-device Neural Engine
Other
429 stars 152 forks source link

[onert] Tutorial Code for NNFW Python API #11595

Open blue4683 opened 11 months ago

blue4683 commented 11 months ago

Tutorial Code for classifying image with inception_v3 using NNFW Python API from #11589 (comment)

Install

$ pip install -i https://test.pypi.org/simple/ nnfwapi==0.1.1
$ pip install numpy
$ pip install opencv-python

Command

$ python3 minimal.py nnpackage_path backends(optional) operations(optional) label_path image_path

Arguments

Example

$ python3 minimal.py inception_v3 cpu label.txt goldfish.jfif

Code

# minimal.py

from nnfwapi.libnnfw_api_pybind import *
import numpy as np
import sys, cv2

def main(argv):
    args = len(argv)
    backends = "cpu"
    operations = ""

    if args < 3 or args > 6:
        print("Syntax Error")
        return

    if args == 3:
        nnpackage_path, label_path, image_path = argv
    elif args == 4:
        nnpackage_path, backends, label_path, image_path = argv
    else:
        nnpackage_path, backends, operations, label_path, image_path = argv

    labels = open(label_path).read().splitlines()
    session = nnfw_session(nnpackage_path, backends, operations)

    inputs = []

    image = cv2.imread(image_path)
    image = cv2.resize(image, (299, 299))
    input_image = np.array(image) / 255
    inputs.append(input_image)

    input_size = session.input_size()
    session.set_inputs(input_size, inputs)

    outputs = session.inference()

    print("[Python]\tResult : " + labels[np.argmax(outputs[0])])
    print()
    print("[Python]\tRun success")
    print()
    return

if __name__ == "__main__":
    argv = sys.argv[1:]
    main(argv)
blue4683 commented 11 months ago

@glistening

Should I upload label.txt and image sample?

glistening commented 11 months ago

No, but it would be helpful if you specify how you can label.txt.