I have a dataset consisting of chairs that was trained with the Resnet 18 neural network. The dataset has pictures in a ‘blocked’ and ‘free’ folder. When I try to load the trained model onto the Nano, I get an error. This is the preprocessing function which works:
import torchvision.transforms as transforms
import torch.nn.functional as F
import cv2
import PIL.Image
import numpy as np
mean = torch.Tensor([0.485, 0.456, 0.406]).cuda().half()
std = torch.Tensor([0.229, 0.224, 0.225]).cuda().half()
normalize = torchvision.transforms.Normalize(mean, std)
def preprocess(image):
image = PIL.Image.fromarray(image)
image = transforms.functional.to_tensor(image).to(device).half()
image.sub_(mean[:, None, None]).div_(std[:, None, None])
return image[None, ...]
This is displaying the camera with a ‘blocked’ slider (which works):
import traitlets
from IPython.display import display
import ipywidgets.widgets as widgets
from jetbot import Camera, bgr8_to_jpeg
camera = Camera.instance(
camera_type='csi',
device_id=0,
width=224,
height=224,
capture_fps=30,
gstreamer_pipeline='nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1280, height=(int)720, format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink'
)
image = widgets.Image(format='jpeg', width=224, height=224)
blocked_slider = widgets.FloatSlider(description='blocked', 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]))
This is creating the neural network execution function which works:
from jetbot import Robot
robot = Robot()
import torch.nn.functional as F
import time
def update(change):
global blocked_slider, robot
x = change['new']
x = preprocess(x)
y = model(x)
y = F.softmax(y, dim=1)
prob_blocked = float(y.flatten()[0])
blocked_slider.value = prob_blocked
time.sleep(0.001)
update({'new': camera.value})
All of this runs, however, when I try to use:
camera.observe(update, names='value')
I get an error. It is ‘TypeError: ‘dict’ object is not callable’:
I’m not sure how to fix this. My goal is to move the CSI camera which is attached to the Nano infront of an obstacle (chair) and see if the slider moves up to 1. If it does then I know my model works and if it doesn’t, I probably need to fix it. However, I get an error when running this and I’m not sure how to fix it.
Any help would be appreciated. Thanks in advance!