溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

pytorch如何批次遍歷數(shù)據(jù)集打印數(shù)據(jù)

發(fā)布時間:2021-07-24 14:38:20 來源:億速云 閱讀:165 作者:小新 欄目:開發(fā)技術(shù)

小編給大家分享一下pytorch如何批次遍歷數(shù)據(jù)集打印數(shù)據(jù),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

代碼

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)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI