faster json序列化

前言:在实际的json序列化过程中,json的dump(dumps)方式比较慢,浪费时间,有没有一种比较快速的替代方式(非自己手动实现并优化)? 使用优化后的orjson库代替json库

在实际的项目中 当我们序列化一个矩阵时,以CV任务中传入图像数组为例,比如传入的数组大小为 [10,640,640,3]
下面是两个不同json库的dumps时间:

1、Json

在这里插入图片描述

2、Orjson

在这里插入图片描述

3、Conclution

总的来说,orjson库的处理方式比json的处理方式快了约14倍(在该例子中),可以作为json的替代品

参考:Choosing a faster JSON library for Python

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用以下步骤将Faster R-CNN模型的预测结果可视化: 1. 导入必要的库和模块: ```python import torch import torchvision.transforms as T from torchvision.models.detection.faster_rcnn import FastRCNNPredictor from PIL import Image import matplotlib.pyplot as plt ``` 2. 加载训练好的Faster R-CNN模型: ```python model = torch.load('path_to_model.pth') model.eval() ``` 3. 定义图像预处理函数: ```python def preprocess_image(image_path): image = Image.open(image_path).convert('RGB') transform = T.Compose([T.ToTensor()]) image = transform(image) return image ``` 4. 运行模型并得到预测结果: ```python image_path = 'path_to_image.jpg' image = preprocess_image(image_path) preds = model([image]) ``` 5. 可视化预测结果: ```python image = Image.open(image_path).convert('RGB') plt.imshow(image) boxes = preds[0]['boxes'].detach().cpu().numpy() labels = preds[0]['labels'].detach().cpu().numpy() scores = preds[0]['scores'].detach().cpu().numpy() for box, label, score in zip(boxes, labels, scores): if score > 0.5: # 设置一个阈值,只显示置信度大于0.5的物体 x1, y1, x2, y2 = box plt.rectangle((x1, y1), (x2, y2), color='red', linewidth=2) plt.text(x1, y1, f'{label}', color='red') plt.text(x1, y2, f'{score}', color='red') plt.axis('off') plt.show() ``` 请确保将`path_to_model.pth`替换为您训练好的模型的路径,将`path_to_image.jpg`替换为您要进行预测和可视化的图像路径。 这样,您就可以通过这段代码来可视化Faster R-CNN模型的预测结果了。希望能对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值