python 加载数据集_在jupyter笔记本python中加载数据集

在尝试使用load_CIFAR10函数加载CIFAR-10数据集时遇到了'ascii' codec的UnicodeDecodeError。问题可能在于pickle文件的读取方式。更新后的代码使用了`_pickle`模块,并在加载数据时进行了形状转换。解决方案是确保使用与数据文件匹配的pickle版本或使用兼容的解码方式打开文件。
摘要由CSDN通过智能技术生成

我在jupyter notebook python中运行以下代码:# Run some setup code for this notebook.

import random

import numpy as np

from cs231n.data_utils import load_CIFAR10

import matplotlib.pyplot as plt

# This is a bit of magic to make matplotlib figures appear inline in the notebook

# rather than in a new window.

%matplotlib inline

plt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots

plt.rcParams['image.interpolation'] = 'nearest'

plt.rcParams['image.cmap'] = 'gray'

# Some more magic so that the notebook will reload external python modules;

# see http://stackoverflow.com/questions/1907993/autoreload-of-modules-in-ipython

%load_ext autoreload

%autoreload 2

然后是下面的说明:

^{pr2}$

通过运行第二部分,我可能会得到以下错误:---------------------------------------------------------------------------

UnicodeDecodeError Traceback (most recent call last)

in ()

1 # Load the raw CIFAR-10 data.

2 cifar10_dir = 'cs231n/datasets/cifar-10-batches-py'

----> 3 X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)

4

5 # As a sanity check, we print out the size of the training and test data.

C:\Users\lenovo\assignment1\cs231n\data_utils.py in load_CIFAR10(ROOT)

20 for b in range(1,6):

21 f = os.path.join(ROOT, 'data_batch_%d' % (b, ))

---> 22 X, Y = load_CIFAR_batch(f)

23 xs.append(X)

24 ys.append(Y)

C:\Users\lenovo\assignment1\cs231n\data_utils.py in load_CIFAR_batch(filename)

7 """ load single batch of cifar """

8 with open(filename, 'rb') as f:

----> 9 datadict = pickle.load(f)

10 X = datadict['data']

11 Y = datadict['labels']

UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6: ordinal not in range(128)

如何解决此错误?我用Annaconda3来运行这个代码。上面的代码似乎是用annaunda2版本编写的。有没有解决这些错误的建议?在

更多细节:

编辑:

添加数据_实用工具.py包含负载定义import _pickle as pickle

import numpy as np

import os

from scipy.misc import imread

def load_CIFAR_batch(filename):

""" load single batch of cifar """

with open(filename, 'rb') as f:

datadict = pickle.load(f)

X = datadict['data']

Y = datadict['labels']

X = X.reshape(10000, 3, 32, 32).transpose(0,2,3,1).astype("float")

Y = np.array(Y)

return X, Y

def load_CIFAR10(ROOT):

""" load all of cifar """

xs = []

ys = []

for b in range(1,6):

f = os.path.join(ROOT, 'data_batch_%d' % (b, ))

X, Y = load_CIFAR_batch(f)

xs.append(X)

ys.append(Y)

Xtr = np.concatenate(xs)

Ytr = np.concatenate(ys)

del X, Y

Xte, Yte = load_CIFAR_batch(os.path.join(ROOT, 'test_batch'))

return Xtr, Ytr, Xte, Yte

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值