__enter__ and no attribute 'create_execution_context' issues with .engine file

Hi,
First, I tried to open a .engine file with the code below

def get_engine(engine_path):
    # If a serialized engine exists, use it instead of building an engine.
    print("Reading engine from file {}".format(engine_path))
    with open(engine_path, "rb") as f, trt.Runtime(TRT_LOGGER) as runtime:
        print("A serialized engine already exist.")
        return runtime.deserialize_cuda_engine(f.read())

Then I got this error message

AttributeError                            Traceback (most recent call last)
Input In [8], in <module>
      3 frame_count = 0
      4 frame0_flag = 0  # stands for status of the first frame
----> 5 with get_engine(engine_path) as engine, engine.create_execution_context() as context:
      6     buffers = allocate_buffers(engine, 1)
      7     # image_size = (416, 416)

AttributeError: __enter__

After searching about the error I changed the loading function to a class like this:

class get_engine():
    def __init__(self, engine_path):
        self.engine_path = engine_path
        self.f = open(self.engine_path, "rb")
        print("Reading engine from file {}".format(engine_path))
        print("A serialized engine already exist.")
        self.engine = runtime.deserialize_cuda_engine(self.f.read())
        return self.engine
    def __enter__(self):
        return self
    
    def __exit__(self, exc_type, exc_value, traceback):
        return self

And now I am getting this:

AttributeError                            Traceback (most recent call last)
Input In [121], in <module>
      3 frame_count = 0
      4 frame0_flag = 0  # stands for status of the first frame
----> 6 with get_engine(engine_path) as engine, engine.create_execution_context() as context:
      7     buffers = allocate_buffers(engine, 1)
      8     # image_size = (416, 416)

AttributeError: 'get_engine' object has no attribute 'create_execution_context'

I double-checked the model’s path but had no success.
Any ideas, please?

Hi,
It is not clear what the issue is. Please provide more information. Do you run DeepStream SDK? Or use jetson_multimedia_api? And please share your release version:
$ cat /etc/nv_tegra_release

Hi, I am a newbie and my goal is to use jetson multimedia API but first I want to run the code on my computer,
I am using this code
I am using tensorrt version 8 and cuda version 11.6
python 3.9 and windows 10

Hi,
If you would like to run Yolo models in your use-case, we suggest use DeepStream SDK. Please follow README here:

/opt/nvidia/deepstream/deepstream-6.0/sources/objectDetector_Yolo/

For YoloV4, please check
https://github.com/NVIDIA-AI-IOT/yolov4_deepstream
For YoloV4 tiny, please check
Jetson/L4T/TRT Customized Example - eLinux.org

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.