前言
之前在进行深度学习训练的时候,偶然发现使用PIL读取图片训练的效果要比使用python-opencv读取出来训练的效果稍好一些,也就是训练更容易收敛。可能的原因是两者读取出来的数据转化为pytorch中Tensor变量稍有不同,这里进行测试。
之后的代码都导入了:
from PIL import Image import matplotlib.pyplot as plt import numpy as np import torch import cv2
测试
使用PIL和cv2读取图片时会有细微的区别,通过下面的代码可以发现两者读取图片是有区别的,也就是使用PIL读取出来的图片转为numpy格式和直接使用cv读取的图片在像素点上并不是完全一致:
In[11]: image = cv2.imread('datasets/0_target.jpg') In[18]: image_pil = Image.open('datasets/0_target.jpg').convert('RGB') In[19]: image_pil = np.array(image_pil) In[20]: image_cv = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) In[21]: image_cv == image_pil Out[21]: array([[[ True, True, False], [ True, False, False], [False, False, False], ..., [ True, True, True], [ True, True, True], [ True, True, True]], [[ True, True, False], [ True, True, True], [False, True, False], ..., [ True, True, False], [ True, True, True], [ True, True, True]], [[ True, True, False], [ True, True, True], [False, False, False], ..., [ True, True, True], [ True, True, True], [ True, True, False]], ..., [[ True, True, True], [ True, True, True], [ True, True, True], ..., [False, False, True], [ True, True, True], [False, False, False]], [[ True, True, True], [ True, True, True], [ True, True, True], ..., [ True, True, True], [ True, True, True], [False, False, False]], [[ True, False, False], [ True, False, False], [ True, False, False], ..., [ True, True, True], [False, False, False], [ True, False, False]]]) In[26]: image_cv.shape Out[26]: (682, 700, 3) In[27]: image_pil.shape Out[27]: (682, 700, 3) In[28]: image_pil - image_cv Out[28]: array([[[ 0, 0, 1], [ 0, 255, 3], [255, 1, 2], ..., [ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0]], [[ 0, 0, 2], [ 0, 0, 0], [255, 0, 2], ..., [ 0, 0, 254], [ 0, 0, 0], [ 0, 0, 0]], [[ 0, 0, 2], [ 0, 0, 0], [255, 1, 2], ..., [ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 254]], ..., [[ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0], ..., [254, 1, 0], [ 0, 0, 0], [ 1, 255, 3]], [[ 0, 0, 0], [ 0, 0, 0], [ 0, 0, 0], ..., [ 0, 0, 0], [ 0, 0, 0], [ 2, 254, 4]], [[ 0, 1, 253], [ 0, 1, 253], [ 0, 1, 255], ..., [ 0, 0, 0], [ 1, 254, 1], [ 0, 255, 2]]], dtype=uint8)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年10月07日
2024年10月07日
- 班得瑞原装进口《第二张新世纪专辑:寂静山林》1CD[APE/CUE分轨][327.1MB]
- Beyond.1989-真的见证(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 陈怡婷.2024-予你的心谁来赔【福茂】【FLAC分轨】
- 陈冠希.2003-HITS.OR.MISSES【英皇娱乐】【WAV+CUE】
- 班得瑞原装进口《第三张新世纪专辑:春野》1CD[APE/CUE分轨][278.9MB]
- 班得瑞原装进口《第四张新世纪专辑:蓝色天际》1CD[APE/CUE分轨][284.8MB]
- 班得瑞原装进口《第五张新世纪专辑:迷雾森林+EP》1CD[APE/CUE分轨][334.2MB]
- 田震.1997-顺其自然(日版)【红星生产社】【WAV+CUE】
- 小虫.1996-想得太美【滚石】【WAV+CUE】
- 谢采妘.2016-邓丽君名曲专辑【丰荣】【WAV+CUE】
- 张含韵.2007-一人一梦【天中文化】【WAV+CUE】
- 蟑螂乐队.1999-帮个忙【福茂】【WAV+CUE】
- 张宇.2001-替身【EMI百代】【WAV+CUE】
- 班得瑞原装进口《第六张新世纪专辑:日光海岸》1CD[APE/CUE分轨][339.5MB]
- 班得瑞原装进口《第七张新世纪专辑:梦花园》1CD[APE/CUE分轨][331.4MB]