欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pytorch 數(shù)據(jù)加載性能對(duì)比分析

 更新時(shí)間:2021年03月06日 09:09:54   作者:ShellCollector  
這篇文章主要介紹了pytorch 數(shù)據(jù)加載性能對(duì)比分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

傳統(tǒng)方式需要10s,dat方式需要0.6s

import os
import time
import torch
import random
from common.coco_dataset import COCODataset
def gen_data(batch_size,data_path,target_path):
 os.makedirs(target_path,exist_ok=True)
 dataloader = torch.utils.data.DataLoader(COCODataset(data_path,
               (352, 352),
               is_training=False, is_scene=True),
            batch_size=batch_size,
            shuffle=False, num_workers=0, pin_memory=False,
            drop_last=True) # DataLoader
 start = time.time()
 for step, samples in enumerate(dataloader):
  images, labels, image_paths = samples["image"], samples["label"], samples["img_path"]
  print("time", images.size(0), time.time() - start)
  start = time.time()
  # torch.save(samples,target_path+ '/' + str(step) + '.dat')
  print(step)
def cat_100(target_path,batch_size=100):
 paths = os.listdir(target_path)
 li = [i for i in range(len(paths))]
 random.shuffle(li)
 images = []
 labels = []
 image_paths = []
 start = time.time()
 for i in range(len(paths)):
  samples = torch.load(target_path + str(li[i]) + ".dat")
  image, label, image_path = samples["image"], samples["label"], samples["img_path"]
  images.append(image.cuda())
  labels.append(label.cuda())
  image_paths.append(image_path)
  if i % batch_size == batch_size - 1:
   images = torch.cat((images), 0)
   print("time", images.size(0), time.time() - start)
   images = []
   labels = []
   image_paths = []
   start = time.time()
  i += 1
if __name__ == '__main__':
 os.environ["CUDA_VISIBLE_DEVICES"] = '3'
 batch_size=320
 # target_path='d:/test_1000/'
 target_path='d:\img_2/'
 data_path = r'D:\dataset\origin_all_datas\_2train'
 gen_data(batch_size,data_path,target_path)
 # get_data(target_path,batch_size)
 # cat_100(target_path,batch_size)

這個(gè)讀取數(shù)據(jù)也比較快:320 batch_size 450ms

def cat_100(target_path,batch_size=100):
 paths = os.listdir(target_path)
 li = [i for i in range(len(paths))]
 random.shuffle(li)
 images = []
 labels = []
 image_paths = []
 start = time.time()
 for i in range(len(paths)):
  samples = torch.load(target_path + str(li[i]) + ".dat")
  image, label, image_path = samples["image"], samples["label"], samples["img_path"]
  images.append(image)#.cuda())
  labels.append(label)#.cuda())
  image_paths.append(image_path)
  if i % batch_size < batch_size - 1:
   i += 1
   continue
  i += 1
  images = torch.cat(([image.cuda() for image in images]), 0)
  print("time", images.size(0), time.time() - start)
  images = []
  labels = []
  image_paths = []
  start = time.time()

補(bǔ)充:pytorch數(shù)據(jù)加載和處理問題解決方案

最近跟著pytorch中文文檔學(xué)習(xí)遇到一些小問題,已經(jīng)解決,在此對(duì)這些錯(cuò)誤進(jìn)行記錄:

在讀取數(shù)據(jù)集時(shí)報(bào)錯(cuò):

AttributeError: 'Series' object has no attribute 'as_matrix'

在顯示圖片是時(shí)報(bào)錯(cuò):

ValueError: Masked arrays must be 1-D

顯示單張圖片時(shí)figure一閃而過

在顯示多張散點(diǎn)圖的時(shí)候報(bào)錯(cuò):

TypeError: show_landmarks() got an unexpected keyword argument 'image'

解決方案

主要問題在這一行: 最終目的是將Series轉(zhuǎn)為Matrix,即調(diào)用np.mat即可完成。

修改前

landmarks =landmarks_frame.iloc[n, 1:].as_matrix()

修改后

landmarks =np.mat(landmarks_frame.iloc[n, 1:])

打散點(diǎn)的x和y坐標(biāo)應(yīng)該均為向量或列表,故將landmarks后使用tolist()方法即可

修改前

plt.scatter(landmarks[:,0],landmarks[:,1],s=10,marker='.',c='r')

修改后

plt.scatter(landmarks[:,0].tolist(),landmarks[:,1].tolist(),s=10,marker='.',c='r')

前面使用plt.ion()打開交互模式,則后面在plt.show()之前一定要加上plt.ioff()。這里直接加到函數(shù)里面,避免每次plt.show()之前都用plt.ioff()

修改前

def show_landmarks(imgs,landmarks):
 '''顯示帶有地標(biāo)的圖片'''
 plt.imshow(imgs)
 plt.scatter(landmarks[:,0].tolist(),landmarks[:,1].tolist(),s=10,marker='.',c='r')#打上紅色散點(diǎn)
 plt.pause(1)#繪圖窗口延時(shí)

修改后

def show_landmarks(imgs,landmarks):
 '''顯示帶有地標(biāo)的圖片'''
 plt.imshow(imgs)
 plt.scatter(landmarks[:,0].tolist(),landmarks[:,1].tolist(),s=10,marker='.',c='r')#打上紅色散點(diǎn)
 plt.pause(1)#繪圖窗口延時(shí)
 plt.ioff()

網(wǎng)上說對(duì)于字典類型的sample可通過 **sample的方式獲取每個(gè)鍵下的值,但是會(huì)報(bào)錯(cuò),于是把輸入寫的詳細(xì)一點(diǎn),就成功了。

修改前

show_landmarks(**sample)

修改后

show_landmarks(sample['image'],sample['landmarks'])

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • python單元測試之pytest的使用

    python單元測試之pytest的使用

    Pytest是Python的一種單元測試框架,與 Python 自帶的 Unittest 測試框架類似,但是比 Unittest 框架使用起來更簡潔,效率更高,今天給大家詳細(xì)介紹一下pytest的使用,需要的朋友可以參考下
    2021-06-06
  • 詳解PyQt5 GUI 接收UDP數(shù)據(jù)并動(dòng)態(tài)繪圖的過程(多線程間信號(hào)傳遞)

    詳解PyQt5 GUI 接收UDP數(shù)據(jù)并動(dòng)態(tài)繪圖的過程(多線程間信號(hào)傳遞)

    這篇文章主要介紹了PyQt5 GUI 接收UDP數(shù)據(jù)并動(dòng)態(tài)繪圖(多線程間信號(hào)傳遞),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Python3多目標(biāo)賦值及共享引用注意事項(xiàng)

    Python3多目標(biāo)賦值及共享引用注意事項(xiàng)

    這篇文章主要介紹了Python3多目標(biāo)賦值及共享引用注意事項(xiàng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05
  • Python?Asyncio?庫之同步原語常用函數(shù)詳解

    Python?Asyncio?庫之同步原語常用函數(shù)詳解

    這篇文章主要為大家介紹了Python?Asyncio?庫之同步原語常用函數(shù)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • 解決python報(bào)錯(cuò):AttributeError:?'ImageDraw'?object?has?no?attribute?'textbbox'

    解決python報(bào)錯(cuò):AttributeError:?'ImageDraw'?object?h

    這篇文章主要給大家介紹了關(guān)于解決python報(bào)錯(cuò):AttributeError:?'ImageDraw'?object?has?no?attribute?'textbbox'的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-01-01
  • Python通過遞歸函數(shù)輸出嵌套列表元素

    Python通過遞歸函數(shù)輸出嵌套列表元素

    這篇文章主要介紹了Python通過遞歸函數(shù)輸出嵌套列表元素,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 利用Python-iGraph如何繪制貼吧/微博的好友關(guān)系圖詳解

    利用Python-iGraph如何繪制貼吧/微博的好友關(guān)系圖詳解

    這篇文章主要給大家介紹了關(guān)于利用Python-iGraph如何繪制貼吧/微博好友關(guān)系圖的相關(guān)資料,文中顯示介紹了在windows系統(tǒng)下安裝python-igraph的步驟,然后通過示例代碼演示了繪制好友關(guān)系圖的方法,需要的朋友可以參考下。
    2017-11-11
  • 解決Django migrate No changes detected 不能創(chuàng)建表的問題

    解決Django migrate No changes detected 不能創(chuàng)建表的問題

    今天小編就為大家分享一篇解決Django migrate No changes detected 不能創(chuàng)建表的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • Python基礎(chǔ)知識(shí)之變量的詳解

    Python基礎(chǔ)知識(shí)之變量的詳解

    這篇文章主要介紹了Python基礎(chǔ)知識(shí)之變量的詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-04-04
  • 如何在python中寫hive腳本

    如何在python中寫hive腳本

    這篇文章主要介紹了如何在python中寫hive腳本,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11

最新評(píng)論