我正在尝试使用tensorflow编码cnn,但我不断提出此错误:
UnicodeDecodeError Traceback (most recent call last)
<ipython-input-20-a02172d91c0c> in <module>()
39 # Load all the data batches.
40 for i in range(5):
---> 41 data_batch = unpickle( 'data_batch_' + str(i + 1))
42
43 train_data = np.append(train_data, data_batch[b'data'])
<ipython-input-20-a02172d91c0c> in unpickle(file)
27 import pickle
28 with open(file, 'rb') as fo:
---> 29 dict = pickle.load(fo)
30 dict = dict.encode('ascii', 'ignore')
31 return dict
UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 6: ordinal not in range(128)
我不确定该怎么做,但我仍然遇到相同的错误。这是我的代码:
# IMAGE RECOGNITION
# Tensorflow and numpy to create the neural network
import tensorflow as tf
import numpy as np
# Matplotlib to plot info to show our results
import matplotlib.pyplot as plt
# OS to load files and save checkpoints
import os
# LOADING THE DATA:
# LOADING CIFAR data from file:
# Load cifar data from file
# Load MNIST data from tf examples
# Load cifar data from file
image_height = 32
image_width = 32
color_channels = 3
model_name = "cifar"
def unpickle(file):
import pickle
with open(file, 'rb') as fo:
dict = pickle.load(fo)
return dict
train_data = np.array([])
train_labels = np.array([])
# Load all the data batches.
for i in range(5):
data_batch = unpickle( 'data_batch_' + str(i + 1))
train_data = np.append(train_data, data_batch[b'data'])
train_labels = np.append(train_labels, data_batch[b'labels'])
# Load the eval batch.
eval_batch = unpickle( 'test_batch')
eval_data = eval_batch[b'data']
eval_labels = eval_batch[b'labels']
# Load the english category names.
category_names_bytes = unpickle('batches.meta')[b'label_names']
category_names = list(map(lambda x: x.decode("utf-8"), category_names_bytes))
# TODO: Process Cifar data
def process_data(data):
float_data = np.array(data, dtype=float) / 255.0
reshaped_data = np.reshape(float_data, (-1, color_channels, image_height, image_width))
# The incorrect image
transposed_data = np.transpose(reshaped_data, [0, 2, 3, 1])
return transposed_data
train_data = process_data(train_data)
eval_data = process_data(eval_data)
# TODO: The Neural Network
# CONVOLUTIONAL NEURAL NETWORK CLASS:
谢谢!
答案 0 :(得分:3)
尝试:
pickle.load(fo, encoding='latin1')
这可能是Python 2/3兼容性问题。顺便说一句,您不应该使用dict
之类的变量名,因为它会覆盖Python内置函数。