pytorch 批次遍歷數(shù)據(jù)集打印數(shù)據(jù)的例子
更新時間:2019年12月30日 10:12:58 作者:風(fēng)澤茹嵐
今天小編就為大家分享一篇pytorch 批次遍歷數(shù)據(jù)集打印數(shù)據(jù)的例子,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
我就廢話不多說了,直接上代碼吧!
from os import listdir import os from time import time import torch.utils.data as data import torchvision.transforms as transforms from torch.utils.data import DataLoader def printProgressBar(iteration, total, prefix='', suffix='', decimals=1, length=100, fill='=', empty=' ', tip='>', begin='[', end=']', done="[DONE]", clear=True): percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total))) filledLength = int(length * iteration // total) bar = fill * filledLength if iteration != total: bar = bar + tip bar = bar + empty * (length - filledLength - len(tip)) display = '\r{prefix}{begin}{bar}{end} {percent}%{suffix}' \ .format(prefix=prefix, begin=begin, bar=bar, end=end, percent=percent, suffix=suffix) print(display, end=''), # comma after print() required for python 2 if iteration == total: # print with newline on complete if clear: # display given complete message with spaces to 'erase' previous progress bar finish = '\r{prefix}{done}'.format(prefix=prefix, done=done) if hasattr(str, 'decode'): # handle python 2 non-unicode strings for proper length measure finish = finish.decode('utf-8') display = display.decode('utf-8') clear = ' ' * max(len(display) - len(finish), 0) print(finish + clear) else: print('') class DatasetFromFolder(data.Dataset): def __init__(self, image_dir): super(DatasetFromFolder, self).__init__() self.photo_path = os.path.join(image_dir, "a") self.sketch_path = os.path.join(image_dir, "b") self.image_filenames = [x for x in listdir(self.photo_path) if is_image_file(x)] transform_list = [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))] self.transform = transforms.Compose(transform_list) def __getitem__(self, index): # Load Image input = load_img(os.path.join(self.photo_path, self.image_filenames[index])) input = self.transform(input) target = load_img(os.path.join(self.sketch_path, self.image_filenames[index])) target = self.transform(target) return input, target def __len__(self): return len(self.image_filenames) if __name__ == '__main__': dataset = DatasetFromFolder("./dataset/facades/train") dataloader = DataLoader(dataset=dataset, num_workers=8, batch_size=1, shuffle=True) total = len(dataloader) for epoch in range(20): t0 = time() for i, batch in enumerate(dataloader): real_a, real_b = batch[0], batch[1] printProgressBar(i + 1, total + 1, length=20, prefix='Epoch %s ' % str(1), suffix=', d_loss: %d' % 1) printProgressBar(total, total, done='Epoch [%s] ' % str(epoch) + ', time: %.2f s' % (time() - t0) )
以上這篇pytorch 批次遍歷數(shù)據(jù)集打印數(shù)據(jù)的例子就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
OpenCV實(shí)現(xiàn)常見的四種圖像幾何變換
這篇文章主要介紹了利用OpenCV實(shí)現(xiàn)的四種圖像幾何變換:縮放、翻轉(zhuǎn)、仿射變換及透視。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下2022-04-04Python列表(List)知識點(diǎn)總結(jié)
在本篇文章中小編給大家分享了關(guān)于Python列表(List)知識點(diǎn)一直對應(yīng)的實(shí)例內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-02-02python實(shí)現(xiàn)網(wǎng)上購物系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)網(wǎng)上購物系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Python XML模塊數(shù)據(jù)解析與生成利器的使用掌握
這篇文章主要為大家介紹了Python XML模塊數(shù)據(jù)解析與生成利器的使用實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01Python自動提取項(xiàng)目中導(dǎo)入的庫及其版本信息
在我們有時需要遷移或部署項(xiàng)目時,需要知道項(xiàng)目所依賴的三方包和版本,本文就來介紹一下Python自動提取項(xiàng)目中導(dǎo)入的庫及其版本信息,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03基于python實(shí)現(xiàn)簡單C/S模式代碼實(shí)例
這篇文章主要介紹了基于python實(shí)現(xiàn)簡單C/S模式代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09