ph_chann = 3 fb0 = fb(shrink=args.shrink) if True: fbB = fb(shrink=1) assert os.path.exists(args.background) background = cv2.imread(args.background) os.system('clear') if os.system('which clear') == 0: os.system('clear') fbB.imshow('back', background) os.system("banner HST") if os.system('which setterm') == 0: os.system('setterm -blank 0;echo setterm -blank 0') fbB = fb(shrink=1) fbB.close() print("virtual_size:", fb0.vw, fb0.vh) devmem_image = devmem(0xe018c000, ph_height * ph_width * ph_chann) devmem_start = devmem(0xe0c00004, 4) devmem_stat = devmem(0xe0c00008, 0x4) devmem_pred = devmem(0xe0000000, 0xc15c) n_classes = 20 grid_h = 9 grid_w = 11 box_coord = 4 n_b_boxes = 5 n_info_per_grid = box_coord + 1 + n_classes classes = [ "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"
def main(): # Definition of the paths # weights_path = './yolov2-tiny-voc_352_288_final.weights' input_img_path = './dog.jpg' input_img_path = './horses.jpg' output_image_path = './result.jpg' # If you do not have the checkpoint yet keep it like this! When you will run test.py for the first time it will be created automatically # ckpt_folder_path = './ckpt/' # Definition of the parameters ph_height = 288 # placeholder height ph_width = 352 # placeholder width score_threshold = 0.3 iou_threshold = 0.3 # Definition of the session # sess = tf.InteractiveSession() # tf.global_variables_initializer().run() # Check for an existing checkpoint and load the weights (if it exists) or do it from binary file # print('Looking for a checkpoint...') # saver = tf.train.Saver() # _ = weights_loader.load(sess,weights_path,ckpt_folder_path,saver) verbose = True # Preprocess the input image print('Preprocessing...') preprocessed_nchwRGB = preprocessing(input_img_path, ph_height, ph_width) cnt = 0 with open('preprocessed_nchwRGB.txt', 'w') as f: for i in preprocessed_nchwRGB.reshape(-1): cnt += 1 f.write("%2x\n" % i) print('dump preprocessed_nchwRGB.txt:', cnt) d = preprocessed_nchwRGB.reshape(-1).astype(np.uint8).tostring() devmem(0xe018c000, len(d), verbose=verbose).write(d).close() print("start FPGA accelerator") s = np.asarray([0x1], dtype=np.uint32).tostring() devmem(0xe0c00004, len(s)).write(s).close() #sleep(1) for i in range(10000): mem = devmem(0xe0c00008, 0x4) status = mem.read(np.uint32) mem.close() if status[0] == 0x2000: break print("fpga status:0x%08x" % (status[0])) print("preprocessing to NCHW-RGB", preprocessed_nchwRGB.shape) # Compute the predictions on the input image print('Computing predictions...') # predictions = inference(sess,preprocessed_image) if True: v = True v = False mem = devmem(0xe0000000, 0xc15c, v) predictions = mem.read(np.float32) mem.close() assert predictions[0] == predictions[ 0], "invalid mem values:{}".format(predictions[:8]) print("inference from FPGA", predictions.shape) else: filename = 'featuremap_8.txt' with open(filename) as f: txt_v = f.read().strip().split() predictions = np.asarray( [np.float(re.sub(',', '', i)) for i in txt_v]) print("inference dummy", predictions.shape, filename) # _predictions________________________________________________________ # | 4 entries |1 entry | 20 entries | # | x..x | y..y | w..w | h..h | c .. c | p0 - p19 .. p0 - p19| x 5(==num) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # entiry size == grid_w x grid_h dets = [] for i in range(5): entries = [] off = grid_h * grid_w * n_info_per_grid * i for j in range(n_info_per_grid): off2 = off + j * grid_h * grid_w * 1 entry = predictions[off2:off2 + grid_h * grid_w * 1].reshape( grid_h, grid_w, 1) entries.append(entry) dets.append(np.concatenate(entries, axis=2)) predictions = np.stack(dets, axis=2) # _predictions_________________________________________ # | 25 float32 words | # grid_h, grid_w, num, | x | y | w | h | c | p0..p19 | # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # predictions.shape=( 9,11,5,25) print("predictions.shape", predictions.shape) # Postprocess the predictions and save the output image print('Postprocessing...') output_image = postprocessing(predictions, input_img_path, score_threshold, iou_threshold, ph_height, ph_width) cv2.imwrite(output_image_path, output_image)
def main(): # Definition of the paths input_img_path = './dog.jpg' input_img_path = './horses.jpg' input_img_path = './person.jpg' output_image_path = './result.jpg' # Definition of the parameters ph_height = 288 # placeholder height ph_width = 352 # placeholder width score_threshold = 0.3 iou_threshold = 0.3 verbose = True # Preprocess the input image print('Preprocessing...') input_image = cv2.imread(input_img_path) # HWC BGR preprocessed_nchwRGB = preprocessing(input_image, ph_height, ph_width) cnt = 0 d = preprocessed_nchwRGB.reshape(-1).astype(np.uint8).tostring() devmem(0xe018c000, len(d), verbose=verbose).write(d).close() print("start FPGA accelerator") s = np.asarray([0x1], dtype=np.uint32).tostring() devmem(0xe0c00004, len(s)).write(s).close() #sleep(1) for i in range(10000): mem = devmem(0xe0c00008, 0x4) status = mem.read(np.uint32) mem.close() if status[0] == 0x2000: break print("fpga status:0x%08x" % (status[0])) print("preprocessing to NCHW-RGB", preprocessed_nchwRGB.shape) # Compute the predictions on the input image print('Computing predictions...') mem = devmem(0xe0000000, 0xc15c) predictions = mem.read(np.float32) mem.close() assert predictions[0] == predictions[0], "invalid mem values:{}".format( predictions[:8]) print("inference from FPGA", predictions.shape) # _predictions________________________________________________________ # | 4 entries |1 entry | 20 entries | # | x..x | y..y | w..w | h..h | c .. c | p0 - p19 .. p0 - p19| x 5(==num) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # entiry size == grid_w x grid_h print("predictions.shape", predictions.shape) # Postprocess the predictions and save the output image print('Postprocessing...') im_h, im_w = input_image.shape[:2] res = dn.postprocessing(predictions, im_w, im_h, 0.5, 0.5) # res: object_name, confidence, ( centerX, centerY, boxW, boxH ) for r in res: name, conf, bbox = r[:3] obj_col = colors[classes.index(r[0])] rect = box2rect(bbox) print("{}".format(bbox)) print("{}".format(rect)) cv2.rectangle( input_image, (rect[0], rect[1]), (rect[2], rect[3]), #(255,255,255) obj_col) cv2.putText( input_image, name, (int(bbox[0]), int(bbox[1])), cv2.FONT_HERSHEY_SIMPLEX, 1, #(255,255,255) obj_col, 2) cv2.imwrite('result.jpg', input_image)
print("virtual_size:",fb0.vw,fb0.vh) print("camra :",args.cam_w, args.cam_h, "shrink:1/%d"%args.shrink, "thread:", args.thread, "DMA Mode:", args.dma) fb0 = fb(shrink=args.shrink) backgrounder(args.background) if os.system('which setterm') == 0: os.system('setterm -blank 0;echo setterm -blank 0') image_area_addr = 0xe018c000 if args.dma and os.path.exists(args.phys_addr): with open(args.phys_addr) as f: cmd = "image_area_addr = %s"%(f.read().strip()) exec(cmd) else: args.dma=False print("image_area_addr:%x"%image_area_addr) devmem_image = devmem(image_area_addr, ph_height*ph_width*ph_chann) devmem_start = devmem(0xe0c00004,4) devmem_stat = devmem(0xe0c00008,0x4) devmem_pred = devmem(0xe0000000,0xc15c) devmem_dmac = devmem(0xe0c00018,4) devmem_pfmc = devmem(0xe0c00020,4) if args.dma: print("DMA-Mode:On") c = np.asarray([0x00000000],dtype=np.uint32).tostring() b = np.asarray([image_area_addr],dtype=np.uint32).tostring() devmem_dmab = devmem(0xe0c00010,4) devmem_dmab.write(b) devmem_dmab.close() else: print("DMA-Mode:Off") c = np.asarray([0x80000000],dtype=np.uint32).tostring()
import os, sys, re import numpy as np from devmemX import devmem import cv2 from time import sleep, time from pdb import * devmem_image = devmem(0xe018c000, 352 * 288 * 3) devmem_start = devmem(0xe0c00004, 4) devmem_stat = devmem(0xe0c00008, 0x4) devmem_pred = devmem(0xe0000000, 0xc15c) n_classes = 20 grid_h = 9 grid_w = 11 box_coord = 4 n_b_boxes = 5 n_info_per_grid = box_coord + 1 + n_classes classes = [ "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor" ] colors = [(254.0, 254.0, 254), (239.8, 211.6, 127), (225.7, 169.3, 0), (211.6, 127.0, 254), (197.5, 84.6, 127), (183.4, 42.3, 0), (169.3, 0.0, 254), (155.2, -42.3, 127), (141.1, -84.6, 0), (127.0, 254.0, 254), (112.8, 211.6, 127), (98.7, 169.3, 0), (84.6, 127.0, 254), (70.5, 84.6, 127), (56.4, 42.3, 0), (42.3, 0.0, 254), (28.2, -42.3, 127), (14.1, -84.6, 0), (0.0, 254.0, 254), (-14.1, 211.6, 127)]