“ascii”编解码器无法解码位置 6 中的字节 0x8b:

我正在尝试使用 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


MYYA
浏览 368回答 1
1回答

郎朗坤

尝试:pickle.load(fo,&nbsp;encoding='latin1')这可能是 Python 2/3 兼容性问题。顺便说一句,您应该尽量不要使用诸如dict变量名之类的东西,因为它会覆盖 Python 内置函数。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python