【吐槽】

啊,代码,你这个大猪蹄子

自己写了cifar10的数据接口,跟官方接口load的数据一样,

沾沾自喜,以为自己会写数据接口了

几天之后,突然想,自己的代码为啥有点慢呢,这数据集不大啊

用了官方接口,真快啊。。。

啊啊啊啊啊啊啊啊

但这是好事,至少我明白了一点知识对吧

【lesson】

看了cifar10的接口,发现自己在数据集初始化的地方写的太少了,应该在初始化的时候就把所有数据读进来,这样的话在__getitem__的时候才能快。

人家的初始化:

 if self.train:
self.train_data = []
self.train_labels = []
for fentry in self.train_list:
f = fentry[0]
file = os.path.join(self.root, self.base_folder, f)
fo = open(file, 'rb')
if sys.version_info[0] == 2:
entry = pickle.load(fo)
else:
entry = pickle.load(fo, encoding='latin1')
self.train_data.append(entry['data'])
if 'labels' in entry:
self.train_labels += entry['labels']
else:
self.train_labels += entry['fine_labels']
fo.close() self.train_data = np.concatenate(self.train_data)
self.train_data = self.train_data.reshape((50000, 3, 32, 32))
self.train_data = self.train_data.transpose((0, 2, 3, 1)) # convert to HWC

人家的getitem

    def __getitem__(self, index):
"""
Args:
index (int): Index Returns:
tuple: (image, target) where target is index of the target class.
"""
if self.train:
img, target = self.train_data[index], self.train_labels[index]
else:
img, target = self.test_data[index], self.test_labels[index] # doing this so that it is consistent with all other datasets
# to return a PIL Image
img = Image.fromarray(img) if self.transform is not None:
img = self.transform(img) if self.target_transform is not None:
target = self.target_transform(target) return img, target

自己:(都写到getitem里面了)

 def __init__(self, root, transforms=transform(), train=True, test=False):
self.root = root
self.transform = transforms
self.train = train
self.test = test
if self.test:
self.train = False def __getitem__(self, item):
x = math.floor(item / 10000) + 1
y = item % 10000
if not self.train and not self.test:
x = 5
y = 5000+item imgpath = os.path.join(self.root, "data_batch_"+str(x))
with open(imgpath, 'rb') as fo:
dict = pickle.load(fo, encoding='bytes')
d_decode = {}
for k,v in dict.items():
d_decode[k.decode('utf8')] = v
dict = d_decode
data = dict['data'][y] # 3*32*32==3072
data = np.reshape(data,(3,32,32))
data = data.transpose(1,2,0)
data = self.transform(data)
label = dict['labels'][y]
# label = torch.from_numpy(label) return data, label

附自己的代码和人家的代码全部

人家:

 base_folder = 'cifar-10-batches-py'
url = "https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz"
filename = "cifar-10-python.tar.gz"
tgz_md5 = 'c58f30108f718f92721af3b95e74349a'
train_list = [
['data_batch_1', 'c99cafc152244af753f735de768cd75f'],
['data_batch_2', 'd4bba439e000b95fd0a9bffe97cbabec'],
['data_batch_3', '54ebc095f3ab1f0389bbae665268c751'],
['data_batch_4', '634d18415352ddfa80567beed471001a'],
['data_batch_5', '482c414d41f54cd18b22e5b47cb7c3cb'],
] test_list = [
['test_batch', '40351d587109b95175f43aff81a1287e'],
] def __init__(self, root, train=True,
transform=None, target_transform=None,
download=False):
self.root = os.path.expanduser(root)
self.transform = transform
self.target_transform = target_transform
self.train = train # training set or test set if download:
self.download() if not self._check_integrity():
raise RuntimeError('Dataset not found or corrupted.' +
' You can use download=True to download it') # now load the picked numpy arrays
if self.train:
self.train_data = []
self.train_labels = []
for fentry in self.train_list:
f = fentry[0]
file = os.path.join(self.root, self.base_folder, f)
fo = open(file, 'rb')
if sys.version_info[0] == 2:
entry = pickle.load(fo)
else:
entry = pickle.load(fo, encoding='latin1')
self.train_data.append(entry['data'])
if 'labels' in entry:
self.train_labels += entry['labels']
else:
self.train_labels += entry['fine_labels']
fo.close() self.train_data = np.concatenate(self.train_data)
self.train_data = self.train_data.reshape((50000, 3, 32, 32))
self.train_data = self.train_data.transpose((0, 2, 3, 1)) # convert to HWC
else:
f = self.test_list[0][0]
file = os.path.join(self.root, self.base_folder, f)
fo = open(file, 'rb')
if sys.version_info[0] == 2:
entry = pickle.load(fo)
else:
entry = pickle.load(fo, encoding='latin1')
self.test_data = entry['data']
if 'labels' in entry:
self.test_labels = entry['labels']
else:
self.test_labels = entry['fine_labels']
fo.close()
self.test_data = self.test_data.reshape((10000, 3, 32, 32))
self.test_data = self.test_data.transpose((0, 2, 3, 1)) # convert to HWC def __getitem__(self, index):
"""
Args:
index (int): Index Returns:
tuple: (image, target) where target is index of the target class.
"""
if self.train:
img, target = self.train_data[index], self.train_labels[index]
else:
img, target = self.test_data[index], self.test_labels[index] # doing this so that it is consistent with all other datasets
# to return a PIL Image
img = Image.fromarray(img) if self.transform is not None:
img = self.transform(img) if self.target_transform is not None:
target = self.target_transform(target) return img, target

pytorch 读数据接口 制作数据集 data.dataset的更多相关文章

  1. pytorch人脸识别——自己制作数据集

    这是一篇面向新手的博文:因为本人也是新手,记录一下自己在做这个项目遇到的大大小小的坑. 按照下面的例子写就好了 import torch as t from torch.utils import da ...

  2. Pytorch数据集读入——Dataset类,实现数据集打乱Shuffle

    在进行相关平台的练习过程中,由于要自己导入数据集,而导入方法在市面上五花八门,各种库都可以应用,在这个过程中我准备尝试torchvision的库dataset torchvision.datasets ...

  3. 使用tensorflow.data.Dataset构造batch数据集(具体用法在下一篇博客介绍)

    import tensorflow as tf import numpy as np def _parse_function(x): num_list = np.arange(10) return n ...

  4. 『计算机视觉』Mask-RCNN_训练网络其一:数据集与Dataset类

    Github地址:Mask_RCNN 『计算机视觉』Mask-RCNN_论文学习 『计算机视觉』Mask-RCNN_项目文档翻译 『计算机视觉』Mask-RCNN_推断网络其一:总览 『计算机视觉』M ...

  5. [PyTorch 学习笔记] 2.1 DataLoader 与 DataSet

    thumbnail: https://image.zhangxiann.com/jeison-higuita-W19AQY42rUk-unsplash.jpg toc: true date: 2020 ...

  6. 基于pytorch实现Resnet对本地数据集的训练

    本文是使用pycharm下的pytorch框架编写一个训练本地数据集的Resnet深度学习模型,其一共有两百行代码左右,分成mian.py.network.py.dataset.py以及train.p ...

  7. TensorFlow2.0(10):加载自定义图片数据集到Dataset

    .caret, .dropup > .btn > .caret { border-top-color: #000 !important; } .label { border: 1px so ...

  8. MindSpore数据集mindspore::dataset

    MindSpore数据集mindspore::dataset ResizeBilinear #include <image_process.h> bool ResizeBilinear(L ...

  9. PyTorch中的MIT ADE20K数据集的语义分割

    PyTorch中的MIT ADE20K数据集的语义分割 代码地址:https://github.com/CSAILVision/semantic-segmentation-pytorch Semant ...

随机推荐

  1. UVALive - 5857 Captain Q's Treasure

    UVALive - 5857 思路: 状压dp,用map写 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimi ...

  2. postman(十):配置jenkins自动发送邮件(邮件包含测试报告)

    继续说一下jenkins与postman的集成 上一篇通过jenkins远程执行postman导出的脚本,并把html报告指定输出到了jenkins对应的job工作空间,接下来配置一下当jenkins ...

  3. 20175317 《Java程序设计》第八周学习总结

    20175317 <Java程序设计>第八周学习总结 教材学习内容总结 第八周我学习了教材第十五章的内容,认识了什么是泛型与集合框架,具体内容如下: 泛型 1. 如何声明泛型类 2. 如何 ...

  4. 键盘坏了几个键位之后,linux上的remap方法

    Use xev command to find the keycode xmodmap -pke |more To Change keymapping for this Laptop: 我是日文键盘, ...

  5. JVM垃圾回收(四)- GC算法:实现(1)

    GC算法:实现 上面我们介绍了GC算法中的核心概念,接下来我们看一下JVM里的具体实现.首先必须了解的一个重要的事实是:对于大部分的JVM来说,两种不同的GC算法是必须的,一个是清理Young Gen ...

  6. ASP.NET MVC CSRF (XSRF) security

    CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种对网站 ...

  7. linux环境下 python环境import找不到自定义的模块

    linux环境下 python环境import找不到自定义的模块 问题现象: Linux环境中自定义的模块swport,import swport 出错.swport模块在/root/sw/目录下. ...

  8. 括号配对问题-java:Stack

    题目描述: 现在,有一行括号序列,请你检查这行括号是否配对. 输入描述: 第一行输入一个数N(0<N<=100),表示有N组测试数据.后面的N行输入多组输入数据,每组输入数据都是一个字符串 ...

  9. setting.xml

    <?xml version="1.0" encoding="UTF-8"?><settings xmlns="http://mave ...

  10. Windows Socket 编程_单个服务器对多个客户端简单通讯

    单个服务器对多个客户端程序: 一.简要说明 二.查看效果 三.编写思路 四.程序源代码 五.存在问题 一.简要说明: 程序名为:TcpSocketOneServerToMulClient 程序功能:实 ...