从tensor转换回numpy是否会掉精度,如何查看tensor值
收藏回复举报
从tensor转换回numpy是否会掉精度,如何查看tensor值
t('forum.solved') 已解决
发表于2023-09-04 13:11:54
0 查看

我在atlas 200i DK A2上将mindspore模型导出的AIR转换为om文件之后进行推理,发现和导出onnx推理的结果相差较大,想查看tensor的数值,有什么办法吗,另外我这个代码是否会使tensor掉精度

# 加载模型
    model = base.model(modelPath=args.model_path, deviceId=args.device_id)

    for image_filename in input_images:
        # 构建输入图像的完整路径
        input_image_path = os.path.join(args.input_folder, image_filename)

        # 读取输入图像
        img = cv2.imread(input_image_path)

        # 进行预处理
        #img_crop = img[0:args.img_size, 0:args.img_size]
        img_crop = cv2.resize(img, (args.img_size, args.img_size))
        img2 = np.expand_dims(img_crop, axis=0)
        mean = [0.5 * 255] * 3
        std = [0.5 * 255] * 3
        img2 = (img2 - np.array(mean)[np.newaxis, np.newaxis, np.newaxis, :]) / np.array(std)[np.newaxis, np.newaxis, np.newaxis, :]
        img2 = np.float32(img2)
        img2 = img2.transpose([0, 3, 1, 2])  # HWC2CHW
        img2 = np.ascontiguousarray(img2)
        print(img2[0][0])
        # 将预处理后的图像数据包装成MindX SDK的Tensor对象
        img2 = Tensor(img2)
        img2.to_device(0)
        img2.to_host()
        img2 = np.array(img2)
        print(img2[0][0])
        # 进行推理
        output = model.infer(img2)
        # 后处理
        result = output[0]
        result.to_host()
        print("result:", output[0].shape)
        result_array = np.array(result)
        print(result_array.shape)
        mean = 0.5 * 255
        std = 0.5 * 255
        #image = result_array[0].astype(np.uint8).transpose((1,2,0))
        image = (result_array[0] * std + mean).astype(np.uint8).transpose((1, 2, 0))

        # 构建输出图像的完整路径
        output_image_path = os.path.join(args.output_folder, image_filename)

        # 保存结果图像
        PIL_image = mgz.fromarray(image)
        PIL_image.save(output_image_path)

我要发帖子