pytorch 批次遍历数据集打印数据的例子

yipeiwu_com5年前Python基础

我就废话不多说了,直接上代码吧!

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 批次遍历数据集打印数据的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python Scapy随心所欲研究TCP协议栈

1. 前言 如果只需要研究Linux的tcp协议栈行为,只需要使用packetdrill就能够满足我的所有需求。packetdrill才是让我随心所欲地撩tcp协议栈。packetdri...

Python爬豆瓣电影实例

Python爬豆瓣电影实例

文件结构 html_downloader.py - 下载网页html内容 #!/usr/bin/python # -*- coding: UTF-8 -*- import urll...

Python使用Redis实现作业调度系统(超简单)

概述 Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案。 Redis从它的许多竞争继承来的三个主要特点: Redis数据库完全在...

Python version 2.7 required, which was not found in the registry

Python version 2.7 required, which was not found in the registry

安装PIL库的时候,直接提示:Python version 2.7 required, which was not found in the registry。 如图: 大意是说找不到...

Python3内置模块random随机方法小结

前言 random是Python中与随机数相关的模块,其本质就是一个伪随机数生成器,我们可以利用random模块基础生成各种不同的随机数,以及一些基于随机数的操作。 生成随机数相关 生...