首页 > python web

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

时间:2020-12-22 python web 查看: 718

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

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博客。

展开全文
上一篇:Django框架教程之中间件MiddleWare浅析
下一篇:利用python3 的pygame模块实现塔防游戏
输入字:
相关知识
django学习之ajax post传参的2种格式实例

AJAX除了异步的特点外,还有一个就是:浏览器页面局部刷新,下面这篇文章主要给大家介绍了关于django学习之ajax post传参的2种格式的相关资料,需要的朋友可以参考下

Python djanjo之csrf防跨站攻击实验过程

csrf攻击,即cross site request forgery跨站(域名)请求伪造,这里的forgery就是伪造的意思。这篇文章主要给大家介绍了关于Python djanjo之csrf防跨站攻击的相关资料,需要的朋友可以参考下

django admin实现动态多选框表单的示例代码

借助django-admin,可以快速得到CRUD界面,但若需要创建多选标签字段时,需要对表单进行调整,本文通过示例代码给大家介绍django admin多选框表单的实现方法,感兴趣的朋友跟随小编一起看看吧

Flask登录注册项目的简单实现

一个简单的用户注册和登录的页面,涉及到验证,数据库存储等等,本文主要介绍了Flask登录注册项目的简单实现,从目录结构开始,感兴趣的可以了解一下