def get_predictions_board(X_pairs, model_filepath, imgs_per_cluster=1000, sw=False): phi = [] eta = [] pt = [] iterations = int(len(X_pairs) / imgs_per_cluster) #ie = microndla.MDLA() ie = fwdnxt.FWDNXT() ie.SetFlag('debug', 'wb') ie.SetFlag("options", "V") ie.SetFlag('imgs_per_cluster', str(imgs_per_cluster)) swnresults = ie.Compile('4x1x1', model_filepath, 'save.bin', 1, 1) nresults = ie.Init('save.bin', '') result = np.ndarray(swnresults, dtype=np.float32) start = 0 end = 0 for iter_i in range(0, len(X_pairs), imgs_per_cluster): start = iter_i end = start + imgs_per_cluster print(iter_i) print(start) print(end) current_batch = np.ascontiguousarray(np.swapaxes( np.expand_dims(np.expand_dims(X_pairs[start:end, :], 0), 0), 0, 2), dtype=np.float32()) print(current_batch.shape) print(current_batch[0][0][0]) result = np.ndarray(swnresults, dtype=np.float32) ie.Run_sw(current_batch, result) cur_phi_pred, cur_eta_pred, cur_pt_pred = parse_board_results( result, imgs_per_cluster, outputs=3) phi.extend(cur_phi_pred) eta.extend(cur_eta_pred) pt.extend(cur_pt_pred) ie.Free() return phi, eta, pt
def ieprocess(image_file, network_file): # load image and resize it: img = Image.open(image_file) #Resize it to the size expected by the network img = img.resize((224, 224), resample=Image.BILINEAR) #Convert to numpy float img = np.array(img).astype(np.float32) / 255 #Transpose to plane-major, as required by our API img = np.ascontiguousarray(img.transpose(2, 0, 1)) # print(img) print('Image shape:', img.shape) #Normalize images stat_mean = list([0.485, 0.456, 0.406]) stat_std = list([0.229, 0.224, 0.225]) for i in range(3): img[i] = (img[i] - stat_mean[i]) / stat_std[i] #Create and initialize the Inference Engine object ie = fwdnxt.FWDNXT() #Compile to a file swnresults = ie.Compile("{:d}x{:d}x{:d}".format(224, 224, 3), network_file, 'save.bin') #Init fpga nresults = ie.Init('save.bin', '') #Create the storage for the result and run one inference result = np.ndarray(swnresults, dtype=np.float32) ie.Run(img, result) #Convert to numpy and print top-5 idxs = (-result).argsort() rstring = [] with open("categories.txt") as f: categories = f.read().splitlines() for i in range(5): rstring.append( str(categories[idxs[i]]) + ', ' + str(result[idxs[i]])) #Free ie.Free() return rstring
class VectorAdd(torch.nn.Module): def __init__(self): super(VectorAdd, self).__init__() def forward(self, x1, x2): x3 = x1 + x2 return x3 D = args.d inVec1 = torch.randn(1, D, 1, 1, dtype=torch.float32) inVec2 = torch.randn(1, D, 1, 1, dtype=torch.float32) modelProd = VectorAdd() torch.onnx.export(modelProd, (inVec1, inVec2), "net_vector_add.onnx") sf = fwdnxt.FWDNXT() if args.verbose: sf.SetFlag('debug', 'b') #debug options # Compile to generate binary snwresults = sf.Compile( '{:d}x{:d}x{:d};{:d}x{:d}x{:d}'.format(1, 1, D, 1, 1, D), 'net_vector_add.onnx', 'net_vector_add.bin', 1, 1) sf.Init("./net_vector_add.bin", "") in_1 = np.ascontiguousarray(inVec1) in_2 = np.ascontiguousarray(inVec2) result = np.ascontiguousarray(np.ndarray((1, 1, snwresults), dtype=np.float32)) sf.Run((in_1, in_2), result) outhw = modelProd(inVec1, inVec2)
for i in range(2): #Convert to numpy float img[i] = np.array(img[i]).astype(np.float32) / 255 #Transpose to plane-major, as required by our API img[i] = np.ascontiguousarray(img[i].transpose(2, 0, 1)) #Normalize images stat_mean = list([0.485, 0.456, 0.406]) stat_std = list([0.229, 0.224, 0.225]) for j in range(3): img[i][j] = (img[i][j] - stat_mean[j]) / stat_std[j] #Create and initialize the Inference Engine object ie = fwdnxt.FWDNXT() #ie.SetFlag('hwlinear','0') #ie.SetFlag('debug','bw') #Compile to a file ie.Compile("{:d}x{:d}x{:d}".format(args.res1[2], args.res1[1], args.res1[0]), args.modelpath1, 'save1.bin') ie.Compile("{:d}x{:d}x{:d}".format(args.res2[2], args.res2[1], args.res2[0]), args.modelpath2, 'save2.bin') ie.Loadmulti(('save1.bin', 'save2.bin')) #Init fpga nresults = ie.Init('', args.load) #Create the storage for the result and run one inference result1 = np.ndarray(nresults[0], dtype=np.float32)