import numpy as np import pandas as pd data_test = pd.read_csv('test3.csv') data_test = np.array(data_test.iloc[:, 1:]) print(data_test) data_test = data_test.reshape(data_test.shape[0], 1, 28, 28) data_test = data_test.astype('float32') data_test /= 255 argmax = lambda res: np.argmax(res.reshape(10)) cases = [] labels = [] cases.append(data_test) # Append the image to list of images to process labels.append(0) # Append the correct answer to compare later # Create a runtime engine from plan file using TensorRT Lite API engine_single = Engine(PLAN="test_engine.engine", postprocessors={"dense_2/Softmax": argmax}) results = [] for image in cases: result = engine_single.infer(image) # Single function for inference results.append(result) print(results)
import os import time import glob import cv2 import scipy import matplotlib.pyplot as plt from tensorrt.lite import Engine from tensorrt.infer import LogSeverity import tensorrt def imsave(image, path): return scipy.misc.imsave(path, image) engine_single = Engine(PLAN="dncnn.engine") GT_data_dir = 'data/Test/Set12' GT_file_list = sorted(glob.glob(GT_data_dir + '/*.png')) print("TensorRT Testing...") for i in range(len(GT_file_list)): GT_img = cv2.imread(GT_file_list[i], 0) / 255 GT_img_reshape = GT_img.reshape(1, GT_img.shape[0], GT_img.shape[1], 1) noise = np.random.normal(0, 25 / 255.0, GT_img_reshape.shape) test_img = GT_img_reshape + noise print(test_img.shape) test_img_transpose = np.transpose(test_img, [0, 3, 1, 2]) start_time2 = time.time() result = engine_single.infer(test_img_transpose) # 这是个list类型的输出
def load_TRT_engine(plan): engine = Engine(PLAN=plan, postprocessors={"dense_2/Softmax": analyze}) return engine
#Arguments to create lite engine network = { "framework": "tf", #Source framework "path": DATA + "/resnet50/resnet50-infer-5.pb", #Path to frozen model "input_nodes": { "input": (3, 224, 224) }, #Dictionary of input nodes and their associated dimensions "output_nodes": ["GPU_0/tower_0/Softmax"], #List of output nodes "logger_severity": LogSeverity.INFO, #Debugging info "postprocessors": { "GPU_0/tower_0/Softmax": analyze } } #Postprocessor function table engine = Engine(**network) #Web service app = Flask(__name__) @app.route("/classify", methods=["POST"]) def json_classify(): if request.method == 'POST': img = Image.open(request.files['file']) #Format image to Numpy CHW and run inference, get the results of the single output node results = engine.infer(image_to_np_CHW(img))[0] #Retrive the results created by the post processor callback top_class_label, top5 = results[0], results[1] #Format data for JSON
def load_TRT_engine(plan): engine = Engine(PLAN=plan, postprocessors={"fc2/Relu": analyze}) return engine
def load_TRT_engine(plan): if os.path.exists(plan): engine = Engine(PLAN=plan, postprocessors={"dense_2/Softmax": analyze}) return engine else: print('{} not exist.'.format(plan))
# This step performs (1) Tensor fusion (2) Reduced precision # (3) Target autotuning (4) Tensor memory management engine = trt.utils.uff_to_trt_engine(G_LOGGER, uff_model, parser, 1, 1<<20, trt.infer.DataType.FLOAT) #FP32 FLOAT, FP16 HALF # Serialize TensorRT engine to a file for when you are ready to deploy your model. trt.utils.write_engine_to_file(engine_name, engine.serialize()) # Create a runtime engine from plan file using TensorRT Lite API engine_single = Engine(PLAN=engine_name, postprocessors={output_node_name:analyze}) #Image processing args = iCap.parse_args() cap = iCap.open_cam_onboard(args.image_width, args.image_height) if not cap.isOpened(): sys.exit('Failed to open camera!') for i in range(0, 1500): abc, vid = cap.read() #vid = load_and_preprocess_images() if vid.shape[0] != input_width: if vid.shape[1] != input_height: