How to solve the error which occurs when using fro

2019-09-03 15:57发布

问题:

I would like to load a dataset in IPython Environment and and use it.

In the directory containing the dataset, I've got these files:

  • batches.meta
  • data_batch_1
  • data_batch_2
  • data_batch_3
  • data_batch_4
  • data_batch_5
  • readme
  • test_batch

I wrote this code:

import os
import pickle as pickle
import numpy as np
import matplotlib.pyplot as plt

#Function Definition
def load_CIFAR(ROOT):
xs=[];
ys=[];
for b in range(6):
    f = os.path.join(ROOT, "data_batch_%d"%(b+1));
    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

#Function Definition
def load_CIFAR_batch(filename):
with open(filename, 'r') as f:

    ****** Here is where  error occurs
    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;

But, when I used this function for loading this dataset with following command, I came across with [a bytes-like object is required, not 'str'] error.

#The directory of my dataset in my hard drive
url = 'D:\\OTIWU\\data\\cifar10'
Xtr, Ytr, Xte, Yte = load_CIFAR(url) 

above is a command that I've used.

The whole error:
---------------------------------------------------------------------------
TypeError  Traceback (most recent call last)
<ipython-input-14-f0576df4fbda> in <module>()
----> 1 Xtr, Ytr, Xte, Yte = load_CIFAR(url)

<ipython-input-10-fedf6bd7c144> in load_CIFAR(ROOT)
      4     for b in range(1,6):
      5         f=os.path.join(ROOT, "data_batch_%d" % (b, ));
      ----> 6         X, Y=load_CIFAR_batch(f);
      7         xs.append(X);
      8         ys.append(Y);

      <ipython-input-13-368cd3e9d8d2> in load_CIFAR_batch(filename)
      1 def load_CIFAR_batch(filename):
      2     with open(filename, 'r') as f:
      ----> 3         datadict = pickle.load(f);
      4 
      5         X = datadict['data'];

      TypeError: a bytes-like object is required, not 'str'

How can I solve such problem?

回答1:

I found the solution. It's a problem related to python 3.x. when I ran it with python 2.x, I could read all the data from dataset. I also have to say that I've changed a little bit the source code. I mean I used from cPickle library Instead of Pickle and all of the source code except this issue is same as before.



回答2:

You need to open your file in binary mode:

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