Classification Examples

The classification examples use a quantized starter model from TensorFlow Lite:

  • i.MX 8M Plus:
    • mobilenet_v1_1.0_224_quant.tflite

    • mobilenet_v2_1.0_224_quant.tflite

  • i.MX 93:
    • mobilenet_v1_1.0_224_quant_vela.tflite

    • mobilenet_v2_1.0_224_quant_vela.tflite

Image Classification

Run the Image Classification Example on MPlus

  1. Retrieve the example, and execute it on the SoM:

curl -LJO https://github.com/varigit/pyvar/raw/master/examples/ml/classification/image_classification_tflite.py
python3 image_classification_tflite.py
  1. The output should be similar as the one below:

Image Example

Image Example Classified

car-plus

car-converted-plus

Image Classification Example Source Code for MPlus: image_classification_tflite.py
 1# Copyright 2021 Variscite LTD
 2# SPDX-License-Identifier: BSD-3-Clause
 3
 4from argparse import ArgumentParser
 5
 6from pyvar.ml.engines.tflite import TFLiteInterpreter
 7from pyvar.ml.utils.label import Label
 8from pyvar.ml.utils.overlay import Overlay
 9from pyvar.ml.utils.resizer import Resizer
10from pyvar.ml.utils.retriever import FTP
11from pyvar.multimedia.helper import Multimedia
12
13ftp = FTP()
14parser = ArgumentParser()
15parser.add_argument('--num_threads', type=int)
16args = parser.parse_args()
17
18if ftp.retrieve_package(category="classification"):
19    model_file_path = ftp.model
20    label_file_path = ftp.label
21    image_file_path = ftp.image
22
23labels = Label(label_file_path)
24labels.read_labels("classification")
25
26engine = TFLiteInterpreter(model_file_path=model_file_path,
27                           num_threads=args.num_threads)
28
29resizer = Resizer()
30resizer.set_sizes(engine_input_details=engine.input_details)
31
32image = Multimedia(image_file_path)
33resizer.resize_image(image.video_src)
34
35engine.set_input(resizer.image_resized)
36engine.run_inference()
37engine.get_result("classification")
38
39draw = Overlay()
40
41output_image = draw.info(category="classification",
42                         image=resizer.image,
43                         top_result=engine.result,
44                         labels=labels.list,
45                         inference_time=engine.inference_time,
46                         model_name=model_file_path,
47                         source_file=resizer.image_path)
48
49image.show_image("TFLite: Image Classification", output_image)

Run the Image Classification Example on 93

  1. Retrieve the example, and execute it on the SoM:

curl -LJO https://github.com/varigit/pyvar/raw/master/examples/ml/classification/image_classification_ethosu.py
python3 image_classification_ethosu.py
  1. The output should be similar as the one below:

Image Example

Image Example Classified

car-93

car-converted-93

Image Classification Example Source Code for 93: image_classification_ethosu.py
 1# Copyright 2023 Variscite LTD
 2# SPDX-License-Identifier: BSD-3-Clause
 3
 4from argparse import ArgumentParser
 5
 6from pyvar.ml.engines.ethosu import EthosuInterpreter
 7from pyvar.ml.utils.label import Label
 8from pyvar.ml.utils.overlay import Overlay
 9from pyvar.ml.utils.resizer import Resizer
10from pyvar.ml.utils.retriever import FTP
11from pyvar.multimedia.helper import Multimedia
12
13ftp = FTP()
14parser = ArgumentParser()
15args = parser.parse_args()
16
17if ftp.retrieve_package(category="classification"):
18    model_file_path = ftp.model
19    label_file_path = ftp.label
20    image_file_path = ftp.image
21
22labels = Label(label_file_path)
23labels.read_labels("classification")
24
25engine = EthosuInterpreter(model_file_path=model_file_path)
26
27resizer = Resizer()
28resizer.set_sizes(engine_input_details=engine.input_details)
29
30image = Multimedia(image_file_path)
31resizer.resize_image(image.video_src)
32
33engine.set_input(resizer.image_resized)
34engine.run_inference()
35engine.get_result("classification")
36
37draw = Overlay()
38
39output_image = draw.info(category="classification",
40                         image=resizer.image,
41                         top_result=engine.result,
42                         labels=labels.list,
43                         inference_time=engine.inference_time,
44                         model_name=model_file_path,
45                         source_file=resizer.image_path)
46
47image.show_image("Ethosu: Image Classification", output_image)



Video Classification

Run the Video Classification Example on MPlus

  1. Retrieve the example, and execute it on the SoM:

curl -LJO https://github.com/varigit/pyvar/raw/master/examples/ml/classification/video_classification_tflite.py
python3 video_classification_tflite.py
  1. The output should be similar as the one below:

Video Example

Video Example Classified

street-plus

street-classified-plus

Video Classification Example Source code for MPlus: video_classification_tflite.py
 1# Copyright 2021 Variscite LTD
 2# SPDX-License-Identifier: BSD-3-Clause
 3
 4from argparse import ArgumentParser
 5
 6from pyvar.ml.engines.tflite import TFLiteInterpreter
 7from pyvar.ml.utils.label import Label
 8from pyvar.ml.utils.overlay import Overlay
 9from pyvar.ml.utils.resizer import Resizer
10from pyvar.ml.utils.retriever import FTP
11from pyvar.multimedia.helper import Multimedia
12
13ftp = FTP()
14parser = ArgumentParser()
15parser.add_argument('--num_threads', type=int)
16args = parser.parse_args()
17
18if ftp.retrieve_package(category="classification"):
19    model_file_path = ftp.model
20    label_file_path = ftp.label
21    video_file_path = ftp.video
22
23labels = Label(label_file_path)
24labels.read_labels("classification")
25
26engine = TFLiteInterpreter(model_file_path=model_file_path,
27                           num_threads=args.num_threads)
28
29resizer = Resizer()
30resizer.set_sizes(engine_input_details=engine.input_details)
31
32video = Multimedia(video_file_path)
33video.set_v4l2_config()
34
35draw = Overlay()
36
37while video.loop:
38    frame = video.get_frame()
39    resizer.resize_frame(frame)
40
41    engine.set_input(resizer.frame_resized)
42    engine.run_inference()
43    engine.get_result("classification")
44
45    output_frame = draw.info(category="classification",
46                             image=resizer.frame,
47                             top_result=engine.result,
48                             labels=labels.list,
49                             inference_time=engine.inference_time,
50                             model_name=model_file_path,
51                             source_file=video.video_src)
52
53    video.show("TFLite: Video Classification", output_frame)
54
55video.destroy()

Run the Video Classification Example on 93

  1. Retrieve the example, and execute it on the SoM:

curl -LJO https://github.com/varigit/pyvar/raw/master/examples/ml/classification/video_classification_ethosu.py
python3 video_classification_ethosu.py
  1. The output should be similar as the one below:

Video Example

Video Example Classified

street-93

street-classified-93

Video Classification Example Source code for 93: video_classification_ethosu.py
 1# Copyright 2023 Variscite LTD
 2# SPDX-License-Identifier: BSD-3-Clause
 3
 4from argparse import ArgumentParser
 5
 6from pyvar.ml.engines.ethosu import EthosuInterpreter
 7from pyvar.ml.utils.label import Label
 8from pyvar.ml.utils.overlay import Overlay
 9from pyvar.ml.utils.resizer import Resizer
10from pyvar.ml.utils.retriever import FTP
11from pyvar.multimedia.helper import Multimedia
12
13ftp = FTP()
14parser = ArgumentParser()
15args = parser.parse_args()
16
17if ftp.retrieve_package(category="classification"):
18    model_file_path = ftp.model
19    label_file_path = ftp.label
20    video_file_path = ftp.video
21
22labels = Label(label_file_path)
23labels.read_labels("classification")
24
25engine = EthosuInterpreter(model_file_path=model_file_path)
26
27resizer = Resizer()
28resizer.set_sizes(engine_input_details=engine.input_details)
29
30video = Multimedia(video_file_path)
31video.set_v4l2_config()
32
33draw = Overlay()
34
35while video.loop:
36    frame = video.get_frame()
37    resizer.resize_frame(frame)
38
39    engine.set_input(resizer.frame_resized)
40    engine.run_inference()
41    engine.get_result("classification")
42
43    output_frame = draw.info(category="classification",
44                             image=resizer.frame,
45                             top_result=engine.result,
46                             labels=labels.list,
47                             inference_time=engine.inference_time,
48                             model_name=model_file_path,
49                             source_file=video.video_src)
50
51    video.show("Ethosu: Video Classification", output_frame)
52
53video.destroy()



Real Time Classification

Run the Real Time Classification Example

  1. Retrieve the example, and execute it on the SoM:

curl -LJO https://github.com/varigit/pyvar/raw/master/examples/ml/classification/realtime_classification_tflite.py
python3 realtime_classification_tflite.py
Real Time Classification Example Source code: realtime_classification_tflite.py
 1# Copyright 2021 Variscite LTD
 2# SPDX-License-Identifier: BSD-3-Clause
 3
 4from argparse import ArgumentParser
 5
 6from pyvar.ml.engines.tflite import TFLiteInterpreter
 7from pyvar.ml.utils.framerate import Framerate
 8from pyvar.ml.utils.label import Label
 9from pyvar.ml.utils.overlay import Overlay
10from pyvar.ml.utils.resizer import Resizer
11from pyvar.ml.utils.retriever import FTP
12from pyvar.multimedia.helper import Multimedia
13
14ftp = FTP()
15parser = ArgumentParser()
16parser.add_argument('--num_threads', type=int)
17args = parser.parse_args()
18
19if ftp.retrieve_package(category="classification"):
20    model_file_path = ftp.model
21    label_file_path = ftp.label
22
23labels = Label(label_file_path)
24labels.read_labels("classification")
25
26engine = TFLiteInterpreter(model_file_path=model_file_path,
27                           num_threads=args.num_threads)
28
29resizer = Resizer()
30resizer.set_sizes(engine_input_details=engine.input_details)
31
32camera = Multimedia("/dev/video1", resolution="vga")
33camera.set_v4l2_config()
34
35framerate = Framerate()
36
37draw = Overlay()
38draw.framerate_info = True
39
40while camera.loop:
41    with framerate.fpsit():
42        frame = camera.get_frame()
43        resizer.resize_frame(frame)
44
45        engine.set_input(resizer.frame_resized)
46        engine.run_inference()
47        engine.get_result("classification")
48
49        output_frame = draw.info(category="classification",
50                                 image=resizer.frame,
51                                 top_result=engine.result,
52                                 labels=labels.list,
53                                 inference_time=engine.inference_time,
54                                 model_name=model_file_path,
55                                 source_file=camera.dev.name,
56                                 fps=framerate.fps)
57
58        camera.show("TFLite: Real Time Classification", output_frame)
59
60camera.destroy()