pytorch 批次遍历数据集打印数据的例子
作者:风泽茹岚 时间:2022-06-09 08:23:46
我就废话不多说了,直接上代码吧!
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)
)
来源:https://blog.csdn.net/luolinll1212/article/details/82983520
标签:pytorch,遍历,数据集,打印
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
使用python实现哈希表、字典、集合操作
2023-11-24 21:09:13
![](https://img.aspxhome.com/file/2023/8/117268_0s.png)
我们用什么来衡量设计3:定量研究的风险
2009-07-19 14:14:00
Jupyter notebook无法导入第三方模块的解决方式
2023-05-23 08:28:53
![](https://img.aspxhome.com/file/2023/1/72791_0s.jpg)
OpenCV实现相机标定
2023-11-13 00:36:45
![](https://img.aspxhome.com/file/2023/7/96937_0s.jpg)
此数据库没有有效所有者,因此无法安装数据库关系图支持对象
2012-01-29 18:15:11
基于Tensorflow的MNIST手写数字识别分类
2023-12-01 11:35:18
![](https://img.aspxhome.com/file/2023/5/103295_0s.jpg)
使用Python解析JSON数据的基本方法
2021-07-03 13:54:21
Python 字符串操作实现代码(截取/替换/查找/分割)
2023-07-14 06:14:00
详解python第三方库的安装、PyInstaller库、random库
2023-03-13 06:57:46
![](https://img.aspxhome.com/file/2023/7/83837_0s.png)
pycharm2020.2 配置使用的方法详解
2022-10-01 12:23:23
![](https://img.aspxhome.com/file/2023/5/114995_0s.png)
PyQt5 QTable插入图片并动态更新的实例
2022-02-11 03:22:43
![](https://img.aspxhome.com/file/2023/9/107459_0s.jpg)
使用PyCharm调试程序实现过程
2023-09-30 10:27:08
![](https://img.aspxhome.com/file/2023/4/101704_0s.png)
讲解设计应用程序时避免阻塞的八个准则
2009-01-04 14:16:00
Pycharm无法显示动态图片的解决方法
2023-01-29 23:13:42
![](https://img.aspxhome.com/file/2023/0/89420_0s.jpg)
Python简单实现区域生长方式
2023-08-08 12:07:38
python关于集合的知识案例详解
2021-01-25 05:11:28
![](https://img.aspxhome.com/file/2023/8/82788_0s.png)
教你在SQL Server 2000数据库中使用分区
2008-11-25 11:55:00
![](https://img.aspxhome.com/file/UploadPic/200811/20081130132452228.jpg)
python 爬取小说并下载的示例
2023-08-24 19:31:49
![](https://img.aspxhome.com/file/2023/2/61962_0s.gif)
python GUI库图形界面开发之PyQt5窗口布局控件QStackedWidget详细使用方法
2023-11-13 13:35:51
![](https://img.aspxhome.com/file/2023/5/99445_0s.png)
如何使用flask将模型部署为服务
2021-11-11 06:02:48
![](https://img.aspxhome.com/file/2023/3/95883_0s.png)