I Added a GPU% Slider to Collision_Avoidance

Thought I would share. Enjoy.

Traitlet Definition Change
import traitlets
from IPython.display import display
import ipywidgets.widgets as widgets
from jetbot import Camera, bgr8_to_jpeg

camera = Camera.instance(width=224, height=224)
image = widgets.Image(format=‘jpeg’, width=224, height=224)
blocked_slider = widgets.FloatSlider(description=‘blocked’, min=0.0, max=1.0, orientation=‘vertical’)
#DMF Added Next Line
gpu_slider = widgets.FloatSlider(description=“GPU%”, min=0.0, max=1.0, orientation=‘vertical’)

camera_link = traitlets.dlink((camera, ‘value’), (image, ‘value’), transform=bgr8_to_jpeg)

display(widgets.HBox([image, blocked_slider, gpu_slider]))

Traitlet Update Change

import torch.nn.functional as F
import time

#DMF Added gpu_usage() section
def gpu_usage():
“”"Gets the Jetson’s current GPU usage fraction

Returns:
    float: The current GPU usage fraction.
"""
with open('/sys/devices/gpu.0/load', 'r') as f:
    return float(f.read().strip('\n')) / 1000.0

def update(change):
#DMF edited Next Line
global blocked_slider, gpu_slider, robot
x = change[‘new’]
x = preprocess(x)
y = model(x)

# we apply the `softmax` function to normalize the output vector so it sums to 1 (which makes it a probability distribution)
y = F.softmax(y, dim=1)

#DMF Added Next Line
gpu_slider.value = gpu_usage()

prob_blocked = float(y.flatten()[0])

blocked_slider.value = prob_blocked

if prob_blocked < 0.4:
    robot.forward(0.25)
else:
    robot.left(0.15)

time.sleep(0.001)

update({‘new’: camera.value}) # we call the function once to intialize
robot.stop()

Nice! Thanks for the sharing.