YOLOv5 分类模型 OpenCV和PyTorch两者实现预处理的差异

news/2024/7/11 1:31:36 标签: YOLO, 分类, opencv

YOLOv5 分类模型 OpenCV和PyTorch两者实现预处理的差异

flyfish

PyTorch封装了PIL库
简单对比下两者的使用方法

import cv2
from PIL import Image
import numpy as np

full_path_file_name="/media/a//ILSVRC2012_val_00001244.JPEG"


#OpenCV读取图像默认是BGR顺序
cv_image=cv2.imread(full_path_file_name) #BGR
print(cv_image.shape)
cv_image=cv2.cvtColor(cv_image,cv2.COLOR_BGR2RGB)
#print("cv_image:",cv_image)#(400, 500, 3) HWC

#PIL读取图像默认是RGB顺序
pil_image=Image.open(full_path_file_name)
print("pil_image:",pil_image)
numpy_image=np.array(pil_image)
print(numpy_image.shape)#(400, 500, 3) HWC BGR
#print("numpy_image:",numpy_image)

在这里插入图片描述

这样OpenCV和PIL返回的是相同的数据

如果是height > width的情况下,图像缩放大小是
( size × height width , size ) \left(\text{size} \times \frac{\text{height}}{\text{width}}, \text{size}\right) (size×widthheight,size)

https://github.com/pytorch/vision/
vision/torchvision/transforms/functional.py

产生的问题
PyTorch中使用transforms.Resizetransforms.Resize使用了双线性插值和抗锯齿antialiasing,与cv2.resize处理不同。所以会造成推理结果有差异

def resize(img: Tensor, size: List[int], interpolation: InterpolationMode = InterpolationMode.BILINEAR,
           max_size: Optional[int] = None) -> Tensor:
The output image might be different depending on its type: when downsampling, the interpolation of PIL images
and tensors is slightly different, because PIL applies antialiasing. This may lead to significant differences
in the performance of a network. Therefore, it is preferable to train and serve a model with the same input
types.

对比下差异

from skimage.metrics import structural_similarity as ssim
from skimage.metrics import peak_signal_noise_ratio as psnr
from skimage.metrics import mean_squared_error as mse


target_size =224

img_w = pil_image.width
img_h = pil_image.height

image_width, image_height =0,0
if(img_h >= img_w):# hw
    image_width, image_height =target_size, int(target_size * img_h / img_w)
else:
    image_width, image_height =int(target_size * img_w  / img_h),target_size
    


print(image_width, image_height)
pil_resize_img = pil_image.resize((image_width, image_height), Image.BILINEAR)

#print("pil_resize_img:",np.array(pil_resize_img))

pil_resize_img=np.array(pil_resize_img)

cv_resize_img0 = cv2.resize(cv_image, (image_width, image_height), interpolation=cv2.INTER_CUBIC)
#print("cv_resize_img:",cv_resize_img0)
cv_resize_img1 = cv2.resize(cv_image, (image_width, image_height), interpolation=cv2.INTER_NEAREST)
cv_resize_img2 = cv2.resize(cv_image, (image_width, image_height), interpolation=cv2.INTER_LINEAR)
cv_resize_img3 = cv2.resize(cv_image, (image_width, image_height), interpolation=cv2.INTER_AREA)
cv_resize_img4 = cv2.resize(cv_image, (image_width, image_height), interpolation=cv2.INTER_LANCZOS4)
cv_resize_img5 = cv2.resize(cv_image, (image_width, image_height), interpolation=cv2.INTER_LINEAR_EXACT)
cv_resize_img6 = cv2.resize(cv_image, (image_width, image_height), interpolation=cv2.INTER_NEAREST_EXACT)


print(mse(pil_resize_img,pil_resize_img))
print(mse(pil_resize_img,cv_resize_img0))
print(mse(pil_resize_img,cv_resize_img1))
print(mse(pil_resize_img,cv_resize_img2))
print(mse(pil_resize_img,cv_resize_img3))
print(mse(pil_resize_img,cv_resize_img4))
print(mse(pil_resize_img,cv_resize_img5))
print(mse(pil_resize_img,cv_resize_img6))

可以使用structural_similarity、peak_signal_noise_ratio 、mean_squared_error对比
这里使用mean_squared_error

0.0
30.721508290816328
103.37267219387755
13.030575042517007
2.272438350340136
36.33767538265306
13.034412202380953
51.2258237670068

PyTorch推荐做法是 Therefore, it is preferable to train and serve a model with the same input types.训练和部署使用相同的输入


http://www.niftyadmin.cn/n/5203864.html

相关文章

③【List】Redis常用数据类型: List [使用手册]

个人简介:Java领域新星创作者;阿里云技术博主、星级博主、专家博主;正在Java学习的路上摸爬滚打,记录学习的过程~ 个人主页:.29.的博客 学习社区:进去逛一逛~ Redis List ③Redis List 操作命令汇总1. lpus…

【React-Router】嵌套路由

1. 嵌套路由 在一级路由中又内嵌了其他路由&#xff0c;这种关系就叫做嵌套路由。 2. 嵌套路由配置 // /page/About/index.js const About () > {return (<div>二级路由 About 组件</div>) }export default About// /page/Layout/index.js import { Outlet, …

游戏开发引擎Cocos Creator和Unity如何对接广告-AdSet聚合广告平台

在游戏开发方面&#xff0c;游戏引擎的选择对开发过程和最终的产品质量有着重大的影响&#xff0c;Unity和Cocos是目前全球两大商用、通用交互内容开发工具&#xff0c;这两款引擎受到广泛关注&#xff0c;本文将从多个维度对两者进行比较&#xff0c;为开发者提供正确的选择建…

Android U 匹配不到APN,无法发起数据建立的问题分析

问题 打开数据开关后&#xff0c;没有data PDN请求发起&#xff0c;因此无法上网。 根据日志确定是没有找到合适的data profile&#xff0c;原因一般有&#xff1a; 1、APN 没有配置 2、APN 类型/网络能力不满足——APN type或bearer 3、APN 配置了但被disable了——APN p…

财报解读:将低价作为“唯一性基础武器”的京东,效果在慢慢显现

近日&#xff0c;京东集团公布了2023年第三季度财报。这是自创始人刘强东去年11月强势回归京东一线、主导一系列战略调整和人事组织改革近一年后的一份“成绩单”。 财报显示&#xff0c;第三季度京东实现收入2477亿元&#xff0c;同比增长1.7%&#xff1b;归属于公司普通股股…

2.4G无线收发芯片 XL2400P使用手册

XL2400P 系列芯片是工作在 2.400~2.483GHz 世界通用 ISM 频段的单片无线收发芯片。该芯片集成射 频收发机、频率收生器、晶体振荡器、调制解调器等功能模块&#xff0c;并且支持一对多组网和带 ACK 的通信模 式。发射输出功率、工作频道以及通信数据率均可配置。芯片已将多颗外…

编译 CUDA加速的 OpenCV-4.8.0 版本

文章目录 前言一、编译环境二、前期准备三、CMake编译四、VS编译OpenCV.sln五、问题 前言 由于项目需要用上CUDA加速的OpenCV&#xff0c;编译时也踩了不少坑&#xff0c;所以这里记录一下。 一、编译环境 我的编译环境是&#xff1a; Win10 RTX4050 CUDA-12.0 CUDNN 8.9.…

JOSEF信号继电器 JX-18A/2 电压 220VAC辅助电源 板后接线

JX-18/2A系列信号继电器 JX-18A/2A1信号继电器&#xff1b; JX-18A/2A2信号继电器&#xff1b; JX-18B /2A1信号继电器; JX-18B/2A2信号继电器; JX-18C/2A1信号继电器; JX-18C/2A2信号继电器; JX-18E/2A1信号继电器; JX-18E/2A2信号继电器; JX-18D/2A1信号继电器; JX…