最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python圖像讀寫方法對比

 更新時(shí)間:2020年11月16日 08:42:45   作者:頎周  
這篇文章主要介紹了Python圖像讀寫方法對比的相關(guān)資料,幫助大家更好的理解和使用python,感興趣的朋友可以了解下

1  實(shí)驗(yàn)標(biāo)準(zhǔn)

  因?yàn)橛?xùn)練使用的框架是Pytorch,因此讀取的實(shí)驗(yàn)標(biāo)準(zhǔn)如下:

  1、讀取分辨率都為1920x1080的5張圖片(png格式一張,jpg格式四張)并保存到數(shù)組。

  2、將讀取的數(shù)組轉(zhuǎn)換為維度順序?yàn)镃xHxW的Pytorch張量,并保存到顯存中(我使用GPU訓(xùn)練),其中三個(gè)通道的順序?yàn)镽GB。

  3、記錄各個(gè)方法在以上操作中所耗費(fèi)的時(shí)間。因?yàn)閜ng格式的圖片大小差不多是質(zhì)量有微小差異的jpg格式的10倍,所以數(shù)據(jù)集通常不會(huì)用png來保存,就不比較這兩種格式的讀取時(shí)間差異了。

  寫入的實(shí)驗(yàn)標(biāo)準(zhǔn)如下:

  1、將5張1920x1080的5張圖像對應(yīng)的Pytorch張量轉(zhuǎn)換為對應(yīng)方法可使用的數(shù)據(jù)類型數(shù)組。

  2、以jpg格式保存五張圖片。

  3、記錄各個(gè)方法保存圖片所耗費(fèi)的時(shí)間。

2  實(shí)驗(yàn)情況

2.1  cv2

  因?yàn)橛蠫PU,所以cv2讀取圖片有兩種方式:

  1、先把圖片都讀取為一個(gè)numpy數(shù)組,再轉(zhuǎn)換成保存在GPU中的pytorch張量。

  2、初始化一個(gè)保存在GPU中的pytorch張量,然后將每張圖直接復(fù)制進(jìn)這個(gè)張量中。

  第一種方式實(shí)驗(yàn)代碼如下:

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讀取 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 讀取時(shí)間1:', time() - start_t) 
# cv2保存
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 保存時(shí)間:', time() - start_t)

 實(shí)驗(yàn)結(jié)果:

cv2 讀取時(shí)間1: 0.39693760871887207
cv2 保存時(shí)間: 0.3560612201690674

第二種方式實(shí)驗(yàn)代碼如下:

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讀取 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 讀取時(shí)間2:', time() - start_t) 
# cv2保存
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 保存時(shí)間:', time() - start_t)

  實(shí)驗(yàn)結(jié)果:

cv2 讀取時(shí)間2: 0.23636841773986816
cv2 保存時(shí)間: 0.3066873550415039

2.2  matplotlib

  同樣兩種讀取方式,第一種代碼如下:

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 讀取 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 讀取時(shí)間1:', time() - start_t) 
# matplotlib 保存
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 保存時(shí)間:', time() - start_t)

  實(shí)驗(yàn)結(jié)果:

matplotlib 讀取時(shí)間1: 0.45380306243896484
matplotlib 保存時(shí)間: 0.768944263458252

  第二種方式實(shí)驗(yàn)代碼:

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 讀取 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 讀取時(shí)間2:', time() - start_t) 
# matplotlib 保存
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 保存時(shí)間:', time() - start_t)

  實(shí)驗(yàn)結(jié)果:

matplotlib 讀取時(shí)間2: 0.2044532299041748
matplotlib 保存時(shí)間: 0.4737534523010254

  需要注意的是,matplotlib讀取png格式圖片獲取的數(shù)組的數(shù)值是在[0,1][0,1]范圍內(nèi)的浮點(diǎn)數(shù),而jpg格式圖片卻是在[0,255][0,255]范圍內(nèi)的整數(shù)。所以如果數(shù)據(jù)集內(nèi)圖片格式不一致,要注意先轉(zhuǎn)換為一致再讀取,否則數(shù)據(jù)集的預(yù)處理就麻煩了。

2.3  PIL

  PIL的讀取與寫入并不能直接使用pytorch張量或numpy數(shù)組,要先轉(zhuǎn)換為Image類型,所以很麻煩,時(shí)間復(fù)雜度上肯定也是占下風(fēng)的,就不實(shí)驗(yàn)了。

2.4  torchvision

  torchvision提供了直接從pytorch張量保存圖片的功能,和上面讀取最快的matplotlib的方法結(jié)合,代碼如下:

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 讀取 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 讀取時(shí)間2:', time() - start_t) 
# torchvision 保存
start_t = time() 
for i in range(imgs.shape[0]):  
 utils.save_image(imgs[i], write_path + str(i) + '.jpg')
print('torchvision 保存時(shí)間:', time() - start_t)

  實(shí)驗(yàn)結(jié)果:

matplotlib 讀取時(shí)間2: 0.15358829498291016
torchvision 保存時(shí)間: 0.14760661125183105

  可以看出這兩個(gè)是最快的讀寫方法。另外,要讓圖片的讀寫盡量不影響訓(xùn)練進(jìn)程,我們還可以讓這兩個(gè)過程與訓(xùn)練并行。另外,utils.save_image可以將多張圖片拼接成一張來保存,具體使用方法如下:

utils.save_image(tensor = imgs,   # 要保存的多張圖片張量 shape = [n, C, H, W]
         fp = 'test.jpg',  # 保存路徑
         nrow = 5,     # 多圖拼接時(shí),每行所占的圖片數(shù)
         padding = 1,    # 多圖拼接時(shí),每張圖之間的間距
         normalize = True, # 是否進(jìn)行規(guī)范化,通常輸出圖像用tanh,所以要用規(guī)范化 
         range = (-1,1))  # 規(guī)范化的范圍

以上就是Python圖像讀寫方法對比的詳細(xì)內(nèi)容,更多關(guān)于python 圖像讀寫的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python類中的self和變量用法及說明

    python類中的self和變量用法及說明

    這篇文章主要介紹了python類中的self和變量用法及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 利用Python編寫一個(gè)簡單的聊天機(jī)器人

    利用Python編寫一個(gè)簡單的聊天機(jī)器人

    這篇文章主要為大家詳細(xì)介紹了如何利用Python編寫一個(gè)簡單的聊天機(jī)器人,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-01-01
  • Python?tkinter?列表框Listbox屬性詳情

    Python?tkinter?列表框Listbox屬性詳情

    這篇文章主要介紹了Python?tkinter?列表框Listbox屬性詳情,列表框控件顯示多行文本,用戶可以選中一行或者多行。所有的文本只能使用一種字體,不能混合使用多種字體
    2022-07-07
  • Python+tkinter實(shí)現(xiàn)樹形圖繪制

    Python+tkinter實(shí)現(xiàn)樹形圖繪制

    Treeview是ttk中的樹形表組件,功能十分強(qiáng)大,非常適用于系統(tǒng)路徑的表達(dá),下面我們就來看看如何利用這一組件實(shí)現(xiàn)樹形圖的繪制吧,有需要的可以參考下
    2023-09-09
  • Django自定義分頁效果

    Django自定義分頁效果

    這篇文章主要為大家詳細(xì)介紹了Django自定義分頁效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • python實(shí)現(xiàn)井字棋游戲

    python實(shí)現(xiàn)井字棋游戲

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)井字棋游戲的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • 對Python Class之間函數(shù)的調(diào)用關(guān)系詳解

    對Python Class之間函數(shù)的調(diào)用關(guān)系詳解

    今天小編就為大家分享一篇對Python Class之間函數(shù)的調(diào)用關(guān)系詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • 如何實(shí)現(xiàn)更換Jupyter Notebook內(nèi)核Python版本

    如何實(shí)現(xiàn)更換Jupyter Notebook內(nèi)核Python版本

    這篇文章主要介紹了如何實(shí)現(xiàn)更換Jupyter Notebook內(nèi)核Python版本,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • 解決python 輸出是省略號(hào)的問題

    解決python 輸出是省略號(hào)的問題

    下面小編就為大家分享一篇解決python 輸出是省略號(hào)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • 在Python的struct模塊中進(jìn)行數(shù)據(jù)格式轉(zhuǎn)換的方法

    在Python的struct模塊中進(jìn)行數(shù)據(jù)格式轉(zhuǎn)換的方法

    這篇文章主要介紹了在Python的struct模塊中進(jìn)行數(shù)據(jù)格式轉(zhuǎn)換的方法,文中還給出了C語言和Python語言的數(shù)據(jù)類型比較,需要的朋友可以參考下
    2015-06-06

最新評論

宁远县| 图们市| 石台县| 景宁| 射阳县| 台北市| 游戏| 大连市| 治县。| 铜鼓县| 嘉善县| 衡山县| 察雅县| 合川市| 曲阳县| 镇安县| 朝阳区| 永州市| 高要市| 南涧| 南安市| 福安市| 龙门县| 青冈县| 三河市| 眉山市| 安陆市| 德格县| 曲靖市| 江达县| 庆安县| 定安县| 铅山县| 吉林市| 青海省| 得荣县| 麻阳| 枞阳县| 河北省| 洛宁县| 突泉县|