We First install ubuntu OS on the SD card and boot it on KV260 following the instruction given on https://www.xilinx.com/products/som/kria/kv260-vision-starter-kit/kv260-getting-started-ubuntu/setting-up-the-sd-card-image.html
Next we install PYNQ on KV260's ubuntu using https://github.com/Xilinx/Kria-PYNQ GitHub repository instructions.
Next we tested Servo Motor's running and deeplearning classification code.
basic experimental setup is as below
First we 3D printed a robotic arm design from the thingverse website using Ender 3 3D Printer.
Based on the objects classification model detected by the KV260 from the Brio Captured image the Robotic Arm with pick and place the objects.
Robotic ARM's working inverse kinematics is as below which is required when moving the robotic arm's servo to an angle based on its end point position it needs to move.
Code of accessing the servo motor is as following
from kv260 import BaseOverlay
from pynq_peripherals import PmodGroveAdapter
base = BaseOverlay('base.bit')
from time import sleep
adapter1 = PmodGroveAdapter(base.PMODA, G1='grove_servo')
servo1 = adapter1.G1
for x in range(2):
for angle in range(0, 180, 1):
#print(f"{angle} degree")
servo1.set_angular_position(angle)
sleep(0.05)
for angle in range(180, 0, -1):
#print(f"{angle} degree")
servo1.set_angular_position(angle)
sleep(0.05)
Code for classification is as following
img_folder = './yoloimages/'
img_file = os.path.join(img_folder, random.choice(os.listdir(img_folder)))
file_name = c_char_p(img_file.encode())
img = load_image(file_name,0,0)
img_letterbox = letterbox_image(img,416,416)
img_copy = np.copy(np.ctypeslib.as_array(img_letterbox.data, (3,416,416)))
img_copy = np.swapaxes(img_copy, 0,2)
free_image(img)
free_image(img_letterbox)
im = Image.open(img_file)
im
start = datetime.now()
img_copy = img_copy[np.newaxis, :, :, :]
conv0_ouput = utils.conv_layer(img_copy,conv0_weights_correct,b=conv0_bias_broadcast,stride=2,padding=1)
conv0_output_quant = conv0_ouput.clip(0.0,4.0)
conv0_output_quant = utils.quantize(conv0_output_quant/4,3)
end = datetime.now()
micros = int((end - start).total_seconds() * 1000000)
print("First layer SW implementation took {} microseconds".format(micros))
print(micros, file=open('timestamp.txt', 'w'))
out_dim = net['conv7']['output'][1]
out_ch = net['conv7']['output'][0]
conv_output = classifier.get_accel_buffer(out_ch, out_dim)
conv_input = classifier.prepare_buffer(conv0_output_quant*7);
start = datetime.now()
classifier.inference(conv_input, conv_output)
end = datetime.now()
conv7_out = classifier.postprocess_buffer(conv_output)
micros = int((end - start).total_seconds() * 1000000)
print("HW implementation took {} microseconds".format(micros))
print(micros, file=open('timestamp.txt', 'a'))
start = datetime.now()
conv7_out_reshaped = conv7_out.reshape(out_dim,out_dim,out_ch)
conv7_out_swapped = np.swapaxes(conv7_out_reshaped, 0, 1) # exp 1
conv7_out_swapped = conv7_out_swapped[np.newaxis, :, :, :]
conv8_output = utils.conv_layer(conv7_out_swapped,conv8_weights_correct,b=conv8_bias_broadcast,stride=1)
conv8_out = conv8_output.ctypes.data_as(ctypes.POINTER(ctypes.c_float))
end = datetime.now()
micros = int((end - start).total_seconds() * 1000000)
print("Last layer SW implementation took {} microseconds".format(micros))
print(micros, file=open('timestamp.txt', 'a'))
lib.forward_region_layer_pointer_nolayer(net_darknet,conv8_out)
tresh = c_float(0.3)
tresh_hier = c_float(0.5)
file_name_out = c_char_p("/home/xilinx/jupyter_notebooks/qnn/detection".encode())
file_name_probs = c_char_p("/home/xilinx/jupyter_notebooks/qnn/probabilities.txt".encode())
file_names_voc = c_char_p("/opt/darknet/data/voc.names".encode())
darknet_path = c_char_p("/opt/darknet/".encode())
lib.draw_detection_python(net_darknet, file_name, tresh, tresh_hier,file_names_voc, darknet_path, file_name_out, file_name_probs);
#Print probabilities
file_content = open(file_name_probs.value,"r").read().splitlines()
detections = []
for line in file_content[0:]:
name, probability = line.split(": ")
detections.append((probability, name))
for det in sorted(detections, key=lambda tup: tup[0], reverse=True):
print("class: {}\tprobability: {}".format(det[1], det[0]))
Comments