Keras ImageDataGenerator:标签形状有问题

时间:2020-07-15 05:25:24

标签: python arrays numpy tensorflow keras

我想解决此错误。 你能告诉我如何解决吗?

我尝试了这些,并得到了相同的X_train和Y_train形状。 打印(len(X_train)) 打印(len(Y_train)) 这两个输出都是82。

所以我无法理解为什么它说“ x(图像张量)和y(标签)应该具有相同的长度。” 即使,只要我检查了它们的长度,它们都是相同的长度,您能否教我哪些长度不同以及如何将它们更改为相同的长度?

谢谢您的时间。


from PIL import Image
import os
import glob
import numpy as np
from sklearn.model_selection import train_test_split
from keras.preprocessing.image import ImageDataGenerator

classes = ["ウソップ"]
num_classes = len(classes)
image_size = 50
num_testdata = 30

# 画像の読み込み
X_train = []
X_test = []
Y_train = []
Y_test = []

for index, classlabel in enumerate(classes):
    photos_dir = "./"+classlabel
    files = glob.glob(photos_dir+"/*.jpg")
    for i, file in enumerate(files):
        if i >= 200:
            break
        image = Image.open(file)
        image = image.convert("RGB")
        image = image.resize((image_size, image_size))
        data = np.asarray(image)
        X_train.append(data)  # リストの最後尾に追加する
        Y_train.append(index)  # ラベルを追加する

datagen = ImageDataGenerator(
    samplewise_center=True, samplewise_std_normalization=True)


g = datagen.flow(X_train, Y_train, shuffle=False)
X_batch, y_batch = g.next()

X_batch *= 127.0/max(abs(X_batch.min()), X_batch.max())
X_batch += 127.0
X_batch = X_batch.astype('unit8')

# 配列にしてテストとトレーニングデータに分けて入れる
X_train = np.array(X_train)
X_test = np.array(X_test)
y_train = np.array(Y_train)
y_test = np.array(Y_test)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-28-4b8de4d76d50> in <module>
----> 1 g = datagen.flow(X_train, Y_train, shuffle=False)
      2 X_batch, y_batch = g.next()
      3 
      4 X_batch *= 127.0/max(abs(X_batch.min()), X_batch.max())
      5 X_batch += 127.0

~\Documents\aidemy\environment\lib\site-packages\keras_preprocessing\image\image_data_generator.py in flow(self, x, y, batch_size, shuffle, sample_weight, seed, save_to_dir, save_prefix, save_format, subset)
    430             save_prefix=save_prefix,
    431             save_format=save_format,
--> 432             subset=subset
    433         )
    434 

~\Documents\aidemy\environment\lib\site-packages\keras_preprocessing\image\numpy_array_iterator.py in __init__(self, x, y, image_data_generator, batch_size, shuffle, sample_weight, seed, data_format, save_to_dir, save_prefix, save_format, subset, dtype)
     78                              'should have the same length. '
     79                              'Found: x.shape = %s, y.shape = %s' %
---> 80                              (np.asarray(x).shape, np.asarray(y).shape))
     81         if sample_weight is not None and len(x) != len(sample_weight):
     82             raise ValueError('`x` (images tensor) and `sample_weight` '

ValueError: `x` (images tensor) and `y` (labels) should have the same length. Found: x.shape = (50, 50, 3), y.shape = (82,)


0 个答案:

没有答案