ValueError:检查输入时出错:预期input_1具有4维,但数组的形状为(32,28,28,1,1)

时间:2020-06-14 23:37:47

标签: python numpy tensorflow keras generative-adversarial-network

我正在尝试实施https://github.com/eriklindernoren/Keras-GAN/tree/master/acgan中的“辅助分类器生成对抗网络的实现”。该代码正在训练MNIST数据集,我想在自定义数据集上对其进行训练。我从本地目录读取了数据,但出现此错误。

ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (32, 28, 28, 1, 1)

这是我用本地目录中的数据集替换加载MNIST数据的方式。

        data = []
        labels = []
        imagePaths = sorted(list(paths.list_images((("/home/path/to/images/")))))
        random.seed(42)
        random.shuffle(imagePaths)
        IMG_SIZE= (28,28)
# loop over the input images
        for imagePath in imagePaths:
            image_pixels = image.load_img(imagePath, target_size=(28,28,1), grayscale=True)
            image_pixels = image.img_to_array(image_pixels)
            image_pixels = image_pixels/255
            data.append(image_pixels)
            label = imagePath.split(os.path.sep)[-2]
            labels.append(label)

        data = np.array(data)
        labels = np.array(labels)
        print ("shape of train images is :", data.shape)
        print ("shape of labels is : ", labels.shape)

# partition the data into training and testing splits using 80% of
# the data for training and the remaining 20% for testing
        (X_train, x_test, y_train, y_test) = train_test_split(data,labels, test_size=0.20, random_state=42)
        ntrain = len(X_train)
        ntest = len(x_test)
        print("There are {} train images and {} test images.".format(np.asarray(X_train).shape[0], np.asarray(x_test).shape[0]))
        print('There are {} unique classes to predict.'.format(np.unique(y_train).shape[0]))

我也尝试按模型期望的那样扩展输入图像的尺寸。我添加了这个

X_train = np.expand_dims(X_train, axis=1)

这次又收到此错误

ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (32, 1, 28, 28, 1)

是否有解决此问题的建议? 如何将火车图像的形状从(1200,28,28,1)更改为(1200,28,28)

0 个答案:

没有答案