TRT_LOGGER = trt.Logger(trt.Logger.INFO) AttributeError: module 'tensorrt' has no attribute 'Logger'
时间: 2023-12-05 22:41:11 浏览: 302
这个错误通常是由于TensorRT版本不兼容或未正确安装所致。请确保您已正确安装TensorRT并使用正确的版本。您可以尝试使用以下命令检查TensorRT版本:
```shell
dpkg -l | grep nvinfer
```
如果您使用的是TensorRT 7,则应该使用以下代码创建Logger:
```python
TRT_LOGGER = trt.Logger(trt.Logger.INFO)
```
如果您使用的是TensorRT 6或更早版本,则应该使用以下代码创建Logger:
```python
TRT_LOGGER = trt.Logger(trt.Logger.VERBOSE)
```
相关问题
AttributeError: 'tensorrt_bindings.tensorrt.Builder' object has no attribute 'set_calibration_profile'
I apologize for the confusion. The `set_calibration_profile()` method was introduced in TensorRT 7.0. If you are using an earlier version of TensorRT, you can set the optimization profile using the `set_shape()` method on the input tensor of the network. Here is an example:
```python
import tensorrt as trt
# Create a builder object
builder = trt.Builder(trt.Logger(trt.Logger.INFO))
# Create a network object
network = builder.create_network()
# Add layers to the network
...
# Get the input tensor of the network
input_tensor = network.get_input(0)
# Set the input shape for the optimization profile
profile = builder.create_optimization_profile()
profile.set_shape(input_tensor.name, (1, 3, 224, 224), (8, 3, 224, 224), (4, 3, 224, 224))
builder.set_optimization_profile(profile)
# Build the engine
engine = builder.build_cuda_engine(network)
```
In this example, we create a network object and add layers to it. We then get the input tensor of the network using `network.get_input(0)`. We create an optimization profile using `builder.create_optimization_profile()`, and set the input shape for the profile using `profile.set_shape()`. We set the optimization profile for the builder using `builder.set_optimization_profile()`, and then build the engine using `builder.build_cuda_engine(network)`.
Note that the `set_shape()` method takes three arguments: the minimum shape, the maximum shape, and the preferred shape. You can adjust these shapes to suit your network's requirements.
import numpy as np import tensorrt as trt import pycuda.driver as cuda import pycuda.autoinit import time import torch # 1. 确定batch size大小,与导出的trt模型保持一致 BATCH_SIZE = 32 # 2. 选择是否采用FP16精度,与导出的trt模型保持一致 USE_FP16 = True target_dtype = np.float16 if USE_FP16 else np.float32 # 3. 创建Runtime,加载TRT引擎 f = open("resnet_engine.trt", "rb") # 读取trt模型 runtime = trt.Runtime(trt.Logger(trt.Logger.WARNING)) # 创建一个Runtime(传入记录器Logger) engine = runtime.deserialize_cuda_engine(f.read()) # 从文件中加载trt引擎 context = engine.create_execution_context() # 创建context # 4. 分配input和output内存 input_batch = np.random.randn(BATCH_SIZE, 224, 224, 3).astype(target_dtype) output = np.empty([BATCH_SIZE, 1000], dtype = target_dtype) d_input = cuda.mem_alloc(1 * input_batch.nbytes) d_output = cuda.mem_alloc(1 * output.nbytes) bindings = [int(d_input), int(d_output)] stream = cuda.Stream() # 5. 创建predict函数 def predict(batch): # result gets copied into output # transfer input data to device cuda.memcpy_htod_async(d_input, batch, stream) # execute model context.execute_async_v2(bindings, stream.handle, None) # 此处采用异步推理。如果想要同步推理,需将execute_async_v2替换成execute_v2 # transfer predictions back cuda.memcpy_dtoh_async(output, d_output, stream) # syncronize threads stream.synchronize() return output # 6. 调用predict函数进行推理,并记录推理时间 def preprocess_input(input): # input_batch无法直接传给模型,还需要做一定的预处理 # 此处可以添加一些其它的预处理操作(如标准化、归一化等) result = torch.from_numpy(input).transpose(0,2).transpose(1,2) # 利用torch中的transpose,使(224,224,3)——>(3,224,224) return np.array(result, dtype=target_dtype) preprocessed_inputs = np.array([preprocess_input(input) for input in input_batch]) # (BATCH_SIZE,224,224,3)——>(BATCH_SIZE,3,224,224) print("Warming up...") pred = predict(preprocessed_inputs) print("Done warming up!") t0 = time.time() pred = predict(preprocessed_inputs) t = time.time() - t0 print("Prediction cost {:.4f}s".format(t)) 请将这部分代码,改成可以输入电脑摄像头视频的
这段代码是用Python语言实现的,用于引入一些常用的库,例如numpy、tensorrt、pycuda等等。同时,还引入了torch库。其中,numpy是Python中用于快速处理大型数组和矩阵计算的库;tensorrt是NVIDIA推出的深度学习推理引擎,可以将训练好的模型转化为高效的推理模型;pycuda是一个Python接口,用于与CUDA(Compute Unified Device Architecture)进行交互,使得Python能够利用GPU进行高性能计算;torch是一个基于Python的科学计算包,主要面向深度学习领域。
阅读全文