NVIDIA / TensorRT

NVIDIA® TensorRT™ is an SDK for high-performance deep learning inference on NVIDIA GPUs. This repository contains the open source components of TensorRT.
https://developer.nvidia.com/tensorrt
Apache License 2.0
10.78k stars 2.13k forks source link

AttributeError: __enter__ #958

Closed wwdok closed 3 years ago

wwdok commented 3 years ago

My Environment is : Ubuntu18.04, tensorrt 7.2.1.6, cuda 10.2.89 I encounter this error when i try to inference with tensorrt engine. I encountered this error for all my three engines. These three engine are:

  1. tsn_r50_320p_1x1x8_100e_kinetics400_rgb_20200702-ef80e3d7-1-1-3-320-569
  2. ircsn_ig65m_pretrained_bnfrozen_r152_32x2x1_58e_kinetics400_rgb_20200812-9037a758-1-1-3-32-224-224
  3. i3d_nl_embedded_gaussian_r50_32x2x1_100e_kinetics400_rgb_20200813-6e6aef1b-1-1-3-32-256-256 They are converted by trtexec with these three onnx model:
  4. tsn_r50_320p_1x1x8_100e_kinetics400_rgb_20200702-ef80e3d7-1-1-3-320-569
  5. ircsn_ig65m_pretrained_bnfrozen_r152_32x2x1_58e_kinetics400_rgb_20200812-9037a758-1-1-3-32-224-224
  6. i3d_nl_embedded_gaussian_r50_32x2x1_100e_kinetics400_rgb_20200813-6e6aef1b-1-1-3-32-256-256

My python code is :

# actions_recognition_webcam.py
import cv2
import numpy as np
import tensorrt as trt
import os
from PIL import Image
import common

TRT_LOGGER = trt.Logger()

def get_engine(engine_file_path=""):
    if os.path.exists(engine_file_path):
        print("Reading engine from file {}".format(engine_file_path))
        with open(engine_file_path, "rb") as f, trt.Runtime(TRT_LOGGER) as runtime:
            return runtime.deserialize_cuda_engine(f.read())

def load_image_into_numpy_array(image):
        (im_width, im_height) = image.size
        return np.array(image).reshape(
            (im_height, im_width, 3)
        ).astype(np.uint8)

def load_img_webcam(arr):
        image = Image.fromarray(np.uint8(arr))
        image_resized = image.resize(
            size=(model_input_width, model_input_height),
            resample=Image.BILINEAR
        )
        img_np = load_image_into_numpy_array(image_resized)
        img_np = img_np.transpose((2, 0, 1))
        img_np = (2.0 / 255.0) * img_np - 1.0
        img_np = img_np.ravel()
        return img_np

def main():
    with get_engine(engine_file_path) as engine, engine.create_execution_context() as context:
        inputs, outputs, bindings, stream = common.allocate_buffers(engine)

        cap = cv2.VideoCapture(0) 

        while True:
            ret, image_np = cap.read()
            img = load_img_webcam(image_np)
            np.copyto(inputs[0].host, img.ravel())
            scores = common.do_inference_v2(context, bindings=bindings, inputs=inputs, outputs=outputs, stream=stream)
            print(scores)

if __name__ == '__main__':
    # Configure parameters
    engine_file_path = "tsn_r50_320p_1x1x8_100e_kinetics400_rgb_20200702-ef80e3d7-1-1-3-320-569.engine" # AttributeError: __enter__
    # engine_file_path = "ircsn_ig65m_pretrained_bnfrozen_r152_32x2x1_58e_kinetics400_rgb_20200812-9037a758-1-1-3-32-224-224.engine"  # AttributeError: __enter__
    # engine_file_path = "i3d_nl_embedded_gaussian_r50_32x2x1_100e_kinetics400_rgb_20200813-6e6aef1b-1-1-3-32-256-256.engine"  # AttributeError: __enter__
    model_input_width = 569
    model_input_height = 320
    main()

Its output log :

Traceback (most recent call last):
  File "/home/weidawang/Repo/Python-AI-Action-Utils/actions_recognition_webcam.py", line 60, in <module>
    main()
  File "/home/weidawang/Repo/Python-AI-Action-Utils/actions_recognition_webcam.py", line 39, in main
    with get_engine(engine_file_path) as engine, engine.create_execution_context() as context:
AttributeError: __enter__
wwdok commented 3 years ago

Can anyone give me some tips ?I am new to TensorRT, please give me an idea to debug it, thanks !

pranavm-nvidia commented 3 years ago

@wwdok Most likely the issue is happening during engine build. Can you check the logging output to see where the failure is?

It may help to increase the logging verbosity as well:

TRT_LOGGER = trt.Logger(trt.Logger.VERBOSE)
wwdok commented 3 years ago

In addition to adding trt.Logger.VERBOSE in trt.Logger(), is there other places that i need to modify ? because just midified this line of code, i found the logging output is still the same as before.

pranavm-nvidia commented 3 years ago

That should be it. Can you share the full logging output?

wwdok commented 3 years ago

The full logging output are as the same as above, just 6 lines. You can try it on your computer, you just need to download one of the engines(because three engines all have the same error), like tsn_r50_320p_1x1x8_100e_kinetics400_rgb_20200702-ef80e3d7-1-1-3-320-569, and run the script i provided above, thanks !

wwdok commented 3 years ago

Well, it is a careless mistake, the engine_file_path is wrong that lacks of parent directory...