Classification Examples

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

  • mobilenet_v1_1.0_224_quant.tgz;

  • mobilenet_v2_1.0_224_quant.tgz.

Image Classification

Run the Image Classification Example

  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

car-converted

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

Note

You can try the same example using Arm NN as inference engine image_classification_armnn.py.




Video Classification

Run the Video Classification Example

  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

street-classified

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

Note

You can try the same example using Arm NN as inference engine video_classification_armnn.py.




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 pyvar.ml.engines.tflite import TFLiteInterpreter
 5from pyvar.ml.utils.framerate import Framerate
 6from pyvar.ml.utils.label import Label
 7from pyvar.ml.utils.overlay import Overlay
 8from pyvar.ml.utils.resizer import Resizer
 9from pyvar.ml.utils.retriever import FTP
10from pyvar.multimedia.helper import Multimedia
11
12ftp = FTP()
13
14if ftp.retrieve_package(category="classification"):
15    model_file_path = ftp.model
16    label_file_path = ftp.label
17
18labels = Label(label_file_path)
19labels.read_labels("classification")
20
21engine = TFLiteInterpreter(model_file_path)
22
23resizer = Resizer()
24resizer.set_sizes(engine_input_details=engine.input_details)
25
26camera = Multimedia("/dev/video1", resolution="vga")
27camera.set_v4l2_config()
28
29framerate = Framerate()
30
31draw = Overlay()
32draw.framerate_info = True
33
34while camera.loop:
35    with framerate.fpsit():
36        frame = camera.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=camera.dev.name,
50                                 fps=framerate.fps)
51
52        camera.show("TFLite: Real Time Classification", output_frame)
53
54camera.destroy()

Note

You can try the same example using Arm NN as inference engine realtime_classification_armnn.py.