类型错误:图像数据的形状无效 (3072,) [英] TypeError: Invalid shape (3072,) for image data

查看:109
本文介绍了类型错误:图像数据的形状无效 (3072,)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的东西:

我想读取本地CIFAR10数据集而不是在colab上运行,而是使用colab.首先,我成功下载了 CIFAR10 数据集.然后我用下面的代码来读取它:

Instead of running on colab, I want to read a local CIFAR10 dataset do play CNN using code from colab. Some first of all, I successfully downloaded the CIFAR10 dataset. Then I used the following code to read it:

import tensorflow as tf
import pandas as pd
import numpy as np
import math
import timeit
import matplotlib.pyplot as plt
from six.moves import cPickle as pickle
import os
import platform
from subprocess import check_output
classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

# %matplotlib inline


img_rows, img_cols = 32, 32
input_shape = (img_rows, img_cols, 3)
def load_pickle(f):
    version = platform.python_version_tuple()
    if version[0] == '2':
        return  pickle.load(f)
    elif version[0] == '3':
        return  pickle.load(f, encoding='latin1')
    raise ValueError("invalid python version: {}".format(version))

def load_CIFAR_batch(filename):
    """ load single batch of cifar """
    with open(filename, 'rb') as f:
        datadict = load_pickle(f)
        X = datadict['data']
        Y = datadict['labels']
        X = X.reshape(10000,3072)
        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
def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=10000):
    # Load the raw CIFAR-10 data
    cifar10_dir = './cifar10/'
    X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)

    # Subsample the data
    mask = range(num_training, num_training + num_validation)
    X_val = X_train[mask]
    y_val = y_train[mask]
    mask = range(num_training)
    X_train = X_train[mask]
    y_train = y_train[mask]
    mask = range(num_test)
    X_test = X_test[mask]
    y_test = y_test[mask]

    x_train = X_train.astype('float32')
    x_test = X_test.astype('float32')

    x_train /= 255.0
    x_test /= 255.0

    return x_train, y_train, X_val, y_val, x_test, y_test


# Invoke the above function to get our data.
x_train, y_train, x_val, y_val, x_test, y_test = get_CIFAR10_data()enter code here

然后,为了显示数据集中的图像,我使用了我提到的链接中的原始代码:

Then, to display the images in the dataset, I used the original code from the link I mentioned:

plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(x_train[i], cmap=plt.cm.binary)
    # The CIFAR labels happen to be arrays, 
    # which is why you need the extra index
    plt.xlabel(classes[y_train[i][0]])
plt.show()

最后,出乎意料的是,它给出了一个错误:

At last, unexpectedly, it gave an error saying:

    runfile('F:/Google Drive/DCM_Image_AI/untitled1.py', wdir='F:/Google Drive/DCM_Image_AI')
Traceback (most recent call last):

  File "F:\Google Drive\DCM_Image_AI\untitled1.py", line 85, in <module>
    plt.imshow(x_train[i], cmap=plt.cm.binary)

  File "C:\Users\liuji\Anaconda3\envs\Face_ recognition\lib\site-packages\matplotlib\pyplot.py", line 2677, in imshow
    None else {}), **kwargs)

  File "C:\Users\liuji\Anaconda3\envs\Face_ recognition\lib\site-packages\matplotlib\__init__.py", line 1599, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)

  File "C:\Users\liuji\Anaconda3\envs\Face_ recognition\lib\site-packages\matplotlib\cbook\deprecation.py", line 369, in wrapper
    return func(*args, **kwargs)

  File "C:\Users\liuji\Anaconda3\envs\Face_ recognition\lib\site-packages\matplotlib\cbook\deprecation.py", line 369, in wrapper
    return func(*args, **kwargs)

  File "C:\Users\liuji\Anaconda3\envs\Face_ recognition\lib\site-packages\matplotlib\axes\_axes.py", line 5679, in imshow
    im.set_data(X)

  File "C:\Users\liuji\Anaconda3\envs\Face_ recognition\lib\site-packages\matplotlib\image.py", line 690, in set_data
    .format(self._A.shape))

TypeError: Invalid shape (3072,) for image data

任何人都可以帮助我解决这个问题.非常感谢.

 anyone can kindly help me out with this. many thanks.

推荐答案

第一件事,我意识到您正在将像素值除以255.注释这些行.

First thing I realize you are dividing your pixel values with 255. Comment these lines.

x_train /= 255.0
x_test /= 255.0

在那之后重塑你的形象

np.reshape(image, (32, 32, 3))

这应该有效.

这篇关于类型错误:图像数据的形状无效 (3072,)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆