Comparison of image reading and writing methods in Python

itread01 2020-11-15 01:20:34
comparison image reading writing methods


When training a visual related neural network model , It's always about reading and writing images . There are many ways , such as matplotlib、cv2、PIL etc. . Here are some ways to read and write , To choose the fastest way , Speed up your training .

Experimental standards

Because the training framework is Pytorch, So the experimental criteria for reading are as follows :

1、 The reading resolution is 1920x1080 Of 5 Pictures (png Format a piece of ,jpg There are four forms ) And store it in the array .

2、 Convert the read array into dimensions in the order of CxHxW Of Pytorch Tensors , And stored in video memory ( I use GPU Training ), The order of the three channels is RGB.

3、 Record the time spent by each method in the above operations . Because png The size of the image in the format is almost the same as the quality jpg Format 10 times , So datasets don't usually use png To store , Do not compare the reading time difference between the two formats .

The experimental criteria are as follows :

1、 Will 5 Zhang 1920x1080 Of 5 The image corresponds to Pytorch The tensor is converted to an array of data types that can be used by the corresponding method .

2、 With jpg Format to store five pictures .

3、 Record the time taken by each method to store images .

The experimental situation

cv2

Because of GPU, therefore cv2 There are two ways to read pictures :

1、 First read all the pictures as one numpy Array , And then convert it to be stored in GPU Medium pytorch Tensors .

2、 Initialize a stored in GPU Medium pytorch Tensors , And then copy each graph directly into this tensor .

The first way to experiment is as follows :

import os, torch
import cv2 as cv
import numpy as np
from time import time
read_path = 'D:test'
write_path = 'D:test\\write\\'
# cv2 Read 1
start_t = time()
imgs = np.zeros([5, 1080, 1920, 3])
for img, i in zip(os.listdir(read_path), range(5)):
img = cv.imread(filename=os.path.join(read_path, img))
imgs[i] = img
imgs = torch.tensor(imgs).to('cuda')[...,[2,1,0]].permute([0,3,1,2])/255
print('cv2 Read time 1:', time() - start_t)
# cv2 Store
start_t = time()
imgs = (imgs.permute([0,2,3,1])[...,[2,1,0]]*255).cpu().numpy()
for i in range(imgs.shape[0]):
cv.imwrite(write_path + str(i) + '.jpg', imgs[i])
print('cv2 Storage time :', time() - start_t) 

The results of the experiment :

cv2 Read time 1: 0.39693760871887207
cv2 Storage time : 0.3560612201690674

The second way to experiment is as follows :

import os, torch
import cv2 as cv
import numpy as np
from time import time
read_path = 'D:test'
write_path = 'D:test\\write\\'
# cv2 Read 2
start_t = time()
imgs = torch.zeros([5, 1080, 1920, 3], device='cuda')
for img, i in zip(os.listdir(read_path), range(5)):
img = torch.tensor(cv.imread(filename=os.path.join(read_path, img)), device='cuda')
imgs[i] = img
imgs = imgs[...,[2,1,0]].permute([0,3,1,2])/255
print('cv2 Read time 2:', time() - start_t)
# cv2 Store
start_t = time()
imgs = (imgs.permute([0,2,3,1])[...,[2,1,0]]*255).cpu().numpy()
for i in range(imgs.shape[0]):
cv.imwrite(write_path + str(i) + '.jpg', imgs[i])
print('cv2 Storage time :', time() - start_t) 

The results of the experiment :

cv2 Read time 2: 0.23636841773986816
cv2 Storage time : 0.3066873550415039

matplotlib

There are two ways to read , The first code is as follows :

import os, torch
import numpy as np
import matplotlib.pyplot as plt
from time import time
read_path = 'D:test'
write_path = 'D:test\\write\\'
# matplotlib Read 1
start_t = time()
imgs = np.zeros([5, 1080, 1920, 3])
for img, i in zip(os.listdir(read_path), range(5)):
img = plt.imread(os.path.join(read_path, img))
imgs[i] = img
imgs = torch.tensor(imgs).to('cuda').permute([0,3,1,2])/255
print('matplotlib Read time 1:', time() - start_t)
# matplotlib Store
start_t = time()
imgs = (imgs.permute([0,2,3,1])).cpu().numpy()
for i in range(imgs.shape[0]):
plt.imsave(write_path + str(i) + '.jpg', imgs[i])
print('matplotlib Storage time :', time() - start_t) 

The results of the experiment :

matplotlib Read time 1: 0.45380306243896484
matplotlib Storage time : 0.768944263458252

The second way to experiment with code :

import os, torch
import numpy as np
import matplotlib.pyplot as plt
from time import time
read_path = 'D:test'
write_path = 'D:test\\write\\'
# matplotlib Read 2
start_t = time()
imgs = torch.zeros([5, 1080, 1920, 3], device='cuda')
for img, i in zip(os.listdir(read_path), range(5)):
img = torch.tensor(plt.imread(os.path.join(read_path, img)), device='cuda')
imgs[i] = img
imgs = imgs.permute([0,3,1,2])/255
print('matplotlib Read time 2:', time() - start_t)
# matplotlib Store
start_t = time()
imgs = (imgs.permute([0,2,3,1])).cpu().numpy()
for i in range(imgs.shape[0]):
plt.imsave(write_path + str(i) + '.jpg', imgs[i])
print('matplotlib Storage time :', time() - start_t) 

The results of the experiment :

matplotlib Read time 2: 0.2044532299041748
matplotlib Storage time : 0.4737534523010254

It should be noted that ,matplotlib Read png Format image gets the array value in $[0, 1]$ Floating point numbers in range , and jpg The format picture is in $[0, 255]$ Integers in range . So if the format of the images in the dataset is inconsistent , Be careful to convert to consistent before reading , Otherwise, the data set preprocessing will be troublesome .

PIL

PIL Can't be used directly pytorch Tensor or numpy Array , You have to convert to Image Type , So it's troublesome , Time complexity must have been the underdog , No more experiments .

torchvision

torchvision Provides direct access from pytorch The function of tensor to store pictures , And the fastest read above matplotlib The method of combining , The code is as follows :

import os, torch
import matplotlib.pyplot as plt
from time import time
from torchvision import utils
read_path = 'D:test'
write_path = 'D:test\\write\\'
# matplotlib Read 2
start_t = time()
imgs = torch.zeros([5, 1080, 1920, 3], device='cuda')
for img, i in zip(os.listdir(read_path), range(5)):
img = torch.tensor(plt.imread(os.path.join(read_path, img)), device='cuda')
imgs[i] = img
imgs = imgs.permute([0,3,1,2])/255
print('matplotlib Read time 2:', time() - start_t)
# torchvision Store
start_t = time()
for i in range(imgs.shape[0]):
utils.save_image(imgs[i], write_path + str(i) + '.jpg')
print('torchvision Storage time :', time() - start_t) 

The results of the experiment :

matplotlib Read time 2: 0.15358829498291016
torchvision Storage time : 0.14760661125183105

You can see that these two are the fastest ways to read and write . in addition , Try to make the reading and writing of pictures not affect the training program , We can also run these two processes in parallel with training . in addition ,utils.save_image Multiple images can be spliced into one to store , The specific use method is as follows :

utils.save_image(tensor = imgs, # Multiple picture tensors to store shape = [n, C, H, W]
fp = 'test.jpg', # Storage path
nrow = 5, # When multiple graphs are spliced , Number of pictures per line
padding = 1, # When multiple graphs are spliced , The spacing between each graph
normalize = True, # Whether to standardize , Usually used to output images tanh, So we need to standardize
range = (-1,1)) # The scope of normalization 

&n

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database