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

news2024/9/20 16:33:59

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.coloradmin.cn/o/1238521.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

防止恶意攻击,服务器DDoS防御软件科普

作为一种恶意的攻击方式,DDoS攻击正以超出服务器承受能力的流量淹没网站,让网站变得不可用。近几年,这种攻击持续增多,由此优秀服务器DDoS防御软件的需求也随之增长。那么如何选择服务器DDoS防御软件,从根本上根除DDoS…

美国云服务器:CN2/纯国际/高防线路介绍

​  谈到国外云服务器,美国云服务器必有一席之地。但是,一般来说使用美国云服务器,线路质量是一个重要的考虑因素。如果线路选择不合理,就有可能造成速度减慢或者安全隐患问题产生。本文将介绍美国云服务器的CN2/纯国际/高防三种…

【MATLAB源码-第86期】基于matlab的QC-LDPC码性能仿真,输出误码率曲线。

操作环境: MATLAB 2022a 1、算法描述 QC-LDPC(准循环低密度奇偶校验)编码是一种高效的错误校正编码方式,广泛应用于通信系统和数据存储中以提高数据的可靠性。它是低密度奇偶校验(LDPC)编码的一种特殊形…

【计算机方向】通信、算法、自动化、机器人、电子电气、计算机工程、控制工程、计算机视觉~~~~~合集!!!

◆本文为大家梳理了近期可投的EI国际会议,涵盖计算机各个学科方向,均可EI检索 本期EI会议汇总合集涵盖领域:计算机视觉、物联网、算法、通信、智能技术、人工智能、人机交互、机器人、电子电气等众多领域! 本期所推荐的EI会议有…

AIGC前沿技术与数字创新应用合作交流和论坛发布活动圆满落幕

2023年11月17日下午,AIGC前沿技术与数字创新应用合作交流和论坛发布活动在北京市海淀区牡丹科技楼B座B1报告厅成功举办。 在这个以技术为驱动力的时代,AIGC等这些前沿技术正以惊人的速度改变着我们的生活和产业格局。利用新兴技术和数字化工具来解决问题…

CNVD-2023-12632:泛微E-cology9 browserjsp SQL注入漏洞复现 [附POC]

文章目录 泛微E-cology9 browserjsp SQL注入漏洞(CNVD-2023-12632)漏洞复现 [附POC]0x01 前言0x02 漏洞描述0x03 影响版本0x04 漏洞环境0x05 漏洞复现1.访问漏洞环境2.构造POC3.复现 0x06 修复建议 泛微E-cology9 browserjsp SQL注入漏洞(CNVD-2023-12632)漏洞复现 [附POC] 0x…

AIOps探索 | 应急处置中排障的降本增效方法探索(上)

文章来源:公众号ID-布博士(擎创科技资深产品专家) 哈喽~友友们大家好,最近运维界也是蛮热闹的,前有语雀多次崩溃,后有阿里全系产品集体故障,不管是哪种,都足够逼疯一个运维工程师。…

Power Apps-Timer

插入一个计时器 右侧属性面板,持续时间的单位是毫秒,60000就是60秒(一分钟);开启重复是指60秒结束后重新开始计时;自动启动是指当从其他页面进入时是否自动开始计时;自动暂停是指当离开这个页面…

Android:Google三方库之Firebase集成详细步骤(一)

前提条件 安装最新版本的 Android Studio,或更新为最新版本。使用您的 Google 账号登录 Firebase请注意,依赖于 Google Play 服务的 Firebase SDK 要求设备或模拟器上必须安装 Google Play 服务 将Firebase添加到应用: 方式:使用…

vue3中v-for报错 ‘item‘ is of type ‘unknown‘

报错 在写vue3ts的项目,得到一个数组,需要循环展示,使用v-for循环,写完之后发现有个报错,如下: 解决 props的时候使用PropType将数据类型完整标注即可 以为没有显示的表示出list中item的类型&#xff…

除夕不放假HR如何做

国务院办公厅发布了 关于2024年部分节假日安排的通知 全文如下 各省、自治区、直辖市人民政府,国务院各部委、各直属机构: 经国务院批准,现将2024年元旦、春节、清明节、劳动节、端午节、中秋节和国庆节放假调休日期的具体安排通知如下。 …

企业如何选择一款高效的ETL工具

企业如何选择一款高效的ETL工具? 在企业发展至一定规模后,构建数据仓库(Data Warehouse)和商业智能(BI)系统成为重要举措。在这个过程中,选择一款易于使用且功能强大的ETL平台至关重要,因为数…

【问题定位】通过看Mybatis源码解决系统问题

开发需求好好的,运维同事突然发现了一个问题,某个任务的详情页面加载不出来。看日志,系统在进行查询操作的时候抛出空指针异常。感觉是Mybatis内部异常,所以就跟踪源码看下Mybatis运行到哪一步报错的。 DefaultSqlSession#select…

二、Gitee使用方法

目录 (1)首先可以注册一个 gitee 账号,注册很方便,自行注册 (2)登陆后进入你的主页 (3)创建仓库 (3)克隆 (4)代码提交 &#xf…

SQLite3 数据库学习(四):Qt 数据库基础操作

参考引用 SQLite 权威指南&#xff08;第二版&#xff09;SQLite3 入门 1. 创建连接执行 sql 语句 在 Qt 中使用数据库要在工程文件中添加QT sql1.1 main.cpp #include "createsqlapp.h" #include <QApplication> #include <QSqlDatabase> #include &l…

vite构建项目不能使用require解决方案

在utils文件夹下创建一个getImgUrl.ts文件 /** vite的特殊性, 需要处理图片 */ export const require (imgPath: string) > {try {const handlePath imgPath.replace(, ..)console.log(handlePath::, imgPath)return new URL(handlePath, import.meta.url).href} catch (…

微信怎么设置自动回复?

自动回复的用处 微信自动回复可以提高沟通效率。当你无法立即回复消息时&#xff0c;设置自动回复可以让对方知道你的情况&#xff0c;并且不会因为长时间没有回复而产生误解或不满。 微信自动回复可以节省时间和精力。如果你经常收到类似的询问或回复&#xff0c;通过设置自动…

Linux常用操作 Vim一般使用 SSH介绍 SSH密钥登录

目录 1. 常用命令 2. vim一般使用 3. SSH介绍 4. ssh密钥登录 1. 常用命令 1&#xff09;# 与 $ 提示的区别 # 表示用户有root权限&#xff0c;一般的以root用户登录提示符为#&#xff0c; $提示符表示用户为普通用户 2&#xff09;ifconfig 查看ip地址 eno1: 代表由主板…