Evan Rust
Published © GPL3+

Porting Existing Jetson Nano Projects to the TI SK-TDA4VM

See how to take an existing edge machine vision project from the Jetson Nano to the SK-TDA4VM starter kit

BeginnerProtip1,506
Porting Existing Jetson Nano Projects to the TI SK-TDA4VM

Things used in this project

Story

Read more

Code

Jetson Model Runner

Python
import numpy as np
import time
import glob
import tensorflow as tf
from PIL import Image

interpreter = tf.lite.Interpreter(model_path='<MODEL PATH>')
interpreter.allocate_tensors()

input_fmt = interpreter.get_input_details()
output_fmt = interpreter.get_output_details()

input_shape = (input_fmt[0]['shape'][1], input_fmt[0]['shape'][2])
print(f'Model expects {input_shape[0]}x{input_shape[1]} image', input_fmt[0]['shape'])

image_array = []
image_files = glob.glob('./images/original/*.jpg')

for file in image_files:
    im = Image.open(file)
    image_array.append(np.reshape(np.asarray(im.resize(input_shape)), input_fmt[0]['shape']))
    im.close()

total_time = 0
NUM_ITERATIONS = 20

for i in range(NUM_ITERATIONS):
    input_data = image_array[i]
    interpreter.set_tensor(input_fmt[0]['index'], input_data)

    start = time.time()

    interpreter.invoke()
    
    time_delta = time.time() - start
    total_time += time_delta
    
    results = [interpreter.get_tensor(output_detail['index']) \
                    for output_detail in output_fmt]

    print('Time taken:', f'{time_delta:.5f}', f'seconds - iteration {i}')
    
print('Average:', f'{total_time / NUM_ITERATIONS:.5f}', 'seconds')

TDA4VM Runner

Python
from run_times import tflitert
import time
import numpy as np
from types import SimpleNamespace
import glob
from PIL import Image

params = {
    "artifacts": "<ARTIFACTS PATH>",
    "model_path": "<MODEL PATH>"
}

options = SimpleNamespace(**params)

print('options:', options)

runtime = tflitert(options)

input_shape = runtime.input_details[0]['shape']
image_size = (input_shape[1], input_shape[2])
print(f'Model expects {image_size[0]}x{image_size[1]} image', input_shape)

image_array = []
image_files = glob.glob('./images/original/*.jpg')

for file in image_files:
    im = Image.open(file)
    image_array.append(np.reshape(np.asarray(im.resize(image_size)), input_shape))
    im.close()

total_time = 0
NUM_ITERATIONS = 20

for i in range(NUM_ITERATIONS):
    input_data = image_array[i]

    start = time.time()

    results = runtime(input_data)
    
    time_delta = time.time() - start
    total_time += time_delta

    print('Time taken:', f'{time_delta:.5f}', f'seconds - iteration {i}')
    
print('Average:', f'{total_time / NUM_ITERATIONS:.5f}', 'seconds')

Texas Instruments EdgeAI TIDL Tools

Credits

Evan Rust

Evan Rust

122 projects • 1091 followers
IoT, web, and embedded systems enthusiast. Contact me for product reviews or custom project requests.

Comments