1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
| import os import time import numpy as np
from ais_bench.infer.interface import InferSession,MultiDeviceSession from ais_bench.infer.common.utils import logger_print
model_path_rec = "/home/aicc/mineru/model/d_n_recfix.om" model_path_det = "/home/aicc/mineru/model/d_n_decfix_linux_aarch64.om" class AisBenchInfer: _instance = None
def __new__(cls, device_id=1): if cls._instance is None: cls._instance = super(AisBenchInfer, cls).__new__(cls) cls._instance._initialized = False return cls._instance
def __init__(self, device_id=1): """ 初始化推理模型 Args: device_id: 设备ID model_path: 模型路径 """ if not self._initialized: self.device_id = device_id self.model_path_rec = model_path_rec self.session_rec = InferSession(device_id, self.model_path_rec) self.model_path_det = model_path_det self.multi_session_det = MultiDeviceSession(self.model_path_det) print("初始化完成:") self._initialized = True def muti_infer_det(self, norm_img_batch: np.ndarray): """ 执行推理 Args: norm_img_batch: 输入的图像批次数据 Returns: 推理输出结果 """ outputs = self.multi_session_det.infer({self.device_id: [[norm_img_batch]]}, mode='dymshape', custom_sizes=1000000) print("推理成功") return outputs def infer_rec(self, norm_img_batch: np.ndarray): """ 执行推理 Args: norm_img_batch: 输入的图像批次数据 Returns: 推理输出结果 """ outputs = self.session_rec.infer([norm_img_batch], mode='dymbatch') print("推理成功") return outputs def infer_det(self, norm_img_batch: np.ndarray): """ 执行推理 Args: norm_img_batch: 输入的图像批次数据 Returns: 推理输出结果 """ outputs = self.session_det.infer([norm_img_batch], mode='dymshape') print("type(outputs):", type(outputs)) print("type(outputs[0]):", type(outputs[0])) print("outputs[0].dtype:", outputs[0].dtype) print("outputs[0].shape:", outputs[0].shape) print("outputs:", outputs) print(len(outputs)) print("推理成功") return outputs def free_resource(self): """释放模型资源""" if hasattr(self, 'session'): self.session.free_resource() @staticmethod def infer_with_file(bin_file_path, device_id=0, model_path='/home/aicc/mineru/model/d_model_rec_linux_aarch64.om'): """ 使用文件执行动态批量推理 Args: bin_file_path: 二进制输入文件路径 device_id: 设备ID model_path: 模型路径 Returns: 推理输出结果 """ session = InferSession(device_id, model_path) ndata = np.fromfile(bin_file_path, dtype=np.float32) print("ndata shape:", ndata.shape) print("ndata元素数量:", ndata.size) print("ndata数据类型:", ndata.dtype) ndata = ndata.reshape(6, 3, 48, 320) print("重塑后的ndata shape:", ndata.shape) outputs = session.infer([ndata], mode='dymshape') print(type(outputs)) print(type(outputs[0])) print(outputs[0].dtype) print(outputs[0].shape) session.free_resource() return outputs @staticmethod def infer_with_file_det(bin_file_path, device_id=0, model_path='/home/aicc/mineru/model/d_n_decfix_linux_aarch64.om'): """ 使用文件执行动态批量推理 Args: bin_file_path: 二进制输入文件路径 device_id: 设备ID model_path: 模型路径 Returns: 推理输出结果 """ session = InferSession(device_id, model_path) ndata = np.fromfile(bin_file_path, dtype=np.float32) print("ndata shape:", ndata.shape) print("ndata元素数量:", ndata.size) print("ndata数据类型:", ndata.dtype) ndata = ndata.reshape(1, 3, 800, 704) print("重塑后的ndata shape:", ndata.shape) outputs = session.infer([ndata], mode='dymshape') print(type(outputs)) print(type(outputs[0])) print(outputs[0].dtype) print(outputs[0].shape) session.free_resource() return outputs
@staticmethod def infer_folder_det(folder_path, device_id=0, model_path='/home/aicc/mineru/model/d_n_decfix_linux_aarch64.om'): """ 处理文件夹中的所有bin文件进行检测推理 Args: folder_path: 包含bin文件和shape.txt文件的文件夹路径 device_id: 设备ID model_path: 模型路径 Returns: 所有bin文件的推理结果字典,键为bin文件名,值为推理输出 """ session = MultiDeviceSession( model_path) results = {} bin_files = [f for f in os.listdir(folder_path) if f.endswith('.bin') and not f.endswith('.shape.txt')] for bin_file in bin_files: bin_file_path = os.path.join(folder_path, bin_file) shape_file_path = bin_file_path + '.shape.txt' if not os.path.exists(shape_file_path): print(f"跳过 {bin_file}: 找不到shape文件") continue with open(shape_file_path, 'r') as f: shape_str = f.read().strip() shape = tuple(map(int, shape_str.split(','))) ndata = np.fromfile(bin_file_path, dtype=np.float32) print(f"处理 {bin_file}") print(f"原始数据shape: {ndata.shape}") print(f"从shape文件读取的形状: {shape}") try: ndata = ndata.reshape(shape) print(f"重塑后的数据shape: {ndata.shape}") outputs = session.infer({device_id: [[ndata]]}, mode='dymshape', custom_sizes=10000000) print(f"{bin_file} 推理成功") results[bin_file] = outputs except Exception as e: print(f"处理 {bin_file} 时出错: {e}") return results @staticmethod def infer_folder_rec(folder_path, device_id=0, model_path='/home/aicc/mineru/model/d1001_n_recfix_linux_aarch64.om'): """ 处理文件夹中的所有bin文件进行识别推理 Args: folder_path: 包含bin文件和shape.txt文件的文件夹路径 device_id: 设备ID model_path: 模型路径 Returns: 所有bin文件的推理结果字典,键为bin文件名,值为推理输出 """ session = InferSession(device_id, model_path) results = {} bin_files = [f for f in os.listdir(folder_path) if f.endswith('.bin') and not f.endswith('.shape.txt')] for bin_file in bin_files: bin_file_path = os.path.join(folder_path, bin_file) shape_file_path = bin_file_path + '.shape.txt' if not os.path.exists(shape_file_path): print(f"跳过 {bin_file}: 找不到shape文件") continue with open(shape_file_path, 'r') as f: shape_str = f.read().strip() shape = tuple(map(int, shape_str.split(','))) ndata = np.fromfile(bin_file_path, dtype=np.float32) print(f"处理 {bin_file}") print(f"原始数据shape: {ndata.shape}") print(f"从shape文件读取的形状: {shape}") try: ndata = ndata.reshape(shape) print(f"重塑后的数据shape: {ndata.shape}") outputs = session.infer([ndata], mode='dymbatch') print(f"{bin_file} 推理成功") results[bin_file] = outputs except Exception as e: print(f"处理 {bin_file} 时出错: {e}") session.free_resource() return results
|