我正在尝试使用此代码提取CIFAR 10数据库但我收到以下错误请帮我删除此错误。
是否有其他方法可以提取此数据库。我试图解决这个错误,但我无法做到。请帮帮我。
import numpy as np
import matplotlib.pyplot as plt
import pickle
import os
def unpickle(file):
if os.path.getsize(file) > 0:
# load files from dict
with open(file, 'rb') as f:
data = pickle.load(f, encoding='bytes')
return data
def load_cifar10_data(data_dir):
#Return train_data, train_labels, test_data, test_labels
train_data = None
train_labels = []
for i in range(1, 6):
data_dic = unpickle(data_dir + "/data_batch_{}".format(i))
if i == 1:
train_data = data_dic['data']
else:
train_data = np.vstack((train_data, data_dic['data']))
train_labels += data_dic['labels']
test_data_dic = unpickle(data_dir + "/test_batch")
test_data = test_data_dic['data']
test_labels = test_data_dic['labels']
train_data = train_data.reshape((len(train_data), 3, 32, 32))
train_data = np.rollaxis(train_data, 1, 4)
train_labels = np.array(train_labels)
test_data = test_data.reshape((len(test_data), 3, 32, 32))
test_data = np.rollaxis(test_data, 1, 4)
test_labels = np.array(test_labels)
return train_data, train_labels, test_data, test_labels
data_dir = 'D:\Database\cifar-10-batches-py'
train_data, train_labels, test_data, test_labels = load_cifar10_data(data_dir)
print(train_data.shape)
print(train_labels.shape)
print(test_data.shape)
print(test_labels.shape)
# In order to check where the data shows an image correctly
plt.imshow(train_data[2])
plt.show()
错误:
文件“D:/ Program files / Spyder files / deep learning / untitled6.py”,行 37, 在load_cifar10_data中 train_data = np.vstack((train_data,data_dic ['data']))
KeyError: 'data'
答案 0 :(得分:0)
很抱歉回答得这么晚,但是请使用data_dic[b'data']
和data_dic[b'labels']
,因为它们是字节格式。