10 Star 20 Fork 12

DeepSpark/DeepSparkInference

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
common.py 2.53 KB
Copy Edit Raw Blame History
honglyua authored 5 months ago . update classification
# Copyright (c) 2024, Shanghai Iluvatar CoreX Semiconductor Co., Ltd.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import cv2
import glob
import torch
import tensorrt
import numpy as np
import pycuda.driver as cuda
def eval_batch(batch_score, batch_label):
batch_score = torch.tensor(torch.from_numpy(batch_score), dtype=torch.float32)
values, indices = batch_score.topk(5)
top1, top5 = 0, 0
for idx, label in enumerate(batch_label):
if label == indices[idx][0]:
top1 += 1
if label in indices[idx]:
top5 += 1
return top1, top5
def create_engine_context(engine_path, logger):
with open(engine_path, "rb") as f:
runtime = tensorrt.Runtime(logger)
assert runtime
engine = runtime.deserialize_cuda_engine(f.read())
assert engine
context = engine.create_execution_context()
assert context
return engine, context
def get_io_bindings(engine):
# Setup I/O bindings
inputs = []
outputs = []
allocations = []
for i in range(engine.num_bindings):
is_input = False
if engine.binding_is_input(i):
is_input = True
name = engine.get_binding_name(i)
dtype = engine.get_binding_dtype(i)
shape = engine.get_binding_shape(i)
if is_input:
batch_size = shape[0]
size = np.dtype(tensorrt.nptype(dtype)).itemsize
for s in shape:
size *= s
allocation = cuda.mem_alloc(size)
binding = {
"index": i,
"name": name,
"dtype": np.dtype(tensorrt.nptype(dtype)),
"shape": list(shape),
"allocation": allocation,
}
print(f"binding {i}, name : {name} dtype : {np.dtype(tensorrt.nptype(dtype))} shape : {list(shape)}")
allocations.append(allocation)
if engine.binding_is_input(i):
inputs.append(binding)
else:
outputs.append(binding)
return inputs, outputs, allocations
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/deep-spark/deepsparkinference.git
git@gitee.com:deep-spark/deepsparkinference.git
deep-spark
deepsparkinference
DeepSparkInference
master

Search

371d5123 14472233 46e8bd33 14472233