新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
小编给大家分享一下pytorch如何批次遍历数据集打印数据,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
创新互联是专业的汉阴网站建设公司,汉阴接单;提供成都网站制作、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行汉阴网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!代码
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如何批次遍历数据集打印数据”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联成都网站设计公司行业资讯频道!
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。