def main(args): input_file_path = args.input_image serialized_plan_fp32 = args.engine_file HEIGHT = args.height WIDTH = args.width image = np.asarray(Image.open(input_file_path)) img = rescale_image(image, (HEIGHT, WIDTH), order=1) im = np.array(img, dtype=np.float32, order='C') im = im.transpose((2, 0, 1)) im = sub_mean_chw(im) engine = eng.load_engine(trt_runtime, serialized_plan_fp32) h_input, d_input, h_output, d_output, stream = inf.allocate_buffers( engine, 1, trt.float32) out = inf.do_inference(engine, im, h_input, d_input, h_output, d_output, stream, 1, HEIGHT, WIDTH) out = color_map(out) colorImage_trt = Image.fromarray(out.astype(np.uint8)) colorImage_trt.save('trt_output.png') semantic_model = keras.models.load_model(args.hdf5_file) out_keras = semantic_model.predict(im.reshape(-1, 3, HEIGHT, WIDTH)) out_keras = color_map(out_keras) colorImage_k = Image.fromarray(out_keras.astype(np.uint8)) colorImage_k.save('keras_output.png')
def video(): cap = cv2.VideoCapture(input_file_path) while cap.isOpened(): _, frame = cap.read() resized = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) resized = cv2.resize(resized, (HEIGHT, WIDTH)) resized = (2.0 / 255.0) * resized - 1.0 resized = resized.transpose((2, 0, 1)) # resized = cv2.resize(frame, (WIDTH, HEIGHT)) # resized = resized.astype(np.float32) # resized = np.rollaxis(resized, 2, 0) h_input, d_input, h_output, d_output, stream = inf.allocate_buffers( engine, 1, trt.float32) out = inf.do_inference(engine, resized, h_input, d_input, h_output, d_output, stream, 1, HEIGHT, WIDTH) #pr = out.reshape(( 360, 640 , 256 ) ).argmax( axis=2 ) #pr = pr.astype(np.uint8) # cv2.imshow('frame',frame) print(out) if cv2.waitKey(1) & 0xFF == ord('q'): break
def main(args): serialized_plan_fp32 = args.engine_file print("[INFO] Loading Engine...") engine = eng.load_engine(trt_runtime, serialized_plan_fp32) print("[INFO] Allocate Buffer...") print("[INFO] Apply Inference...") disp_tensors_pred = [] disp_tensors_gt = [] for i in range(config.NUM_VAL//config.batch_size): (force_tensor,disp_tensor_gt) = next(gen) h_input, d_input, h_output, d_output, stream = inf.allocate_buffers(engine, config.batch_size, trt.float16)#batch_size start = time.time() TensorRT_pred = inf.do_inference(engine, force_tensor, h_input, d_input, h_output, d_output, stream, config.batch_size) #batch_size end = time.time() print("inference time including buffer copy", end-start) print("TensorRT_pred",TensorRT_pred.shape) disp_tensors_pred.append(TensorRT_pred) disp_tensors_gt.append(disp_tensor_gt) #break disp_tensors_pred = np.asarray(disp_tensors_pred).reshape(-1,config.data_shape[0],config.data_shape[1],config.data_shape[2],config.data_shape[3]) disp_tensors_gt = np.asarray(disp_tensors_gt).reshape(-1,config.data_shape[0],config.data_shape[1],config.data_shape[2],config.data_shape[3]) print(disp_tensors_pred.shape) Visualize.gen_video( disp_tensors_pred, disp_tensors_gt, config) #visualize the results
def __init__(self, label_file, model_file): self.labels = self.load_labels(label_file) self.engine = eng.load_engine(trt_runtime, model_file) self.h_input, self.d_input, self.h_output, self.d_output, self.stream = inf.allocate_buffers( self.engine, 1, trt.float32) self.context = self.engine.create_execution_context() self.width = 224 self.height = 224
def sub_mean_chw(data): data = data.transpose((1, 2, 0)) # CHW -> HWC data -= np.array(MEAN) # Broadcast subtract data = data.transpose((2, 0, 1)) # HWC -> CHW return data def rescale_image(image, output_shape, order=1): image = skimage.transform.resize(image, output_shape, order=order, preserve_range=True, mode='reflect') return image import engine as eng import inference as inf import tensorrt as trt input_file_path = "data/yolact_example_0.png" serialized_plan_fp32 = "my_engine.trt" HEIGHT = 550 WIDTH = 550 import cv2 img = cv2.imread(input_file_path) print(img.shape) dim = (WIDTH, HEIGHT) img = cv2.resize(img, dim, interpolation = cv2.INTER_AREA) print(img.shape) engine = eng.load_engine(trt_runtime, serialized_plan_fp32) h_input, d_input, h_output, d_output, stream = inf.allocate_buffers(engine, 1, trt.float32) out = inf.do_inference(engine, img, h_input, d_input, h_output, d_output, stream, 1, HEIGHT, WIDTH)