Python 预期ndim=4发现ndim=5和其他错误-Keras-GTSRB数据集

Python 预期ndim=4发现ndim=5和其他错误-Keras-GTSRB数据集,python,arrays,tensorflow,keras,training-data,Python,Arrays,Tensorflow,Keras,Training Data,我试图基于GTSRB数据集(下面给出的链接)制作一个CNN模型,但我面临以下错误: 当我设置input\u shape=input\u shape=(3,IMG\u SIZE,IMG\u SIZE)时,我得到以下错误: ValueError:检查输入时出错:预期conv2d_34_输入为 有4个维度,但得到了具有形状的数组(9030,1) 当我研究这个问题时,我发现一个解决方案可能是将批次大小作为参数传递,当我尝试这样做时,我得到以下错误: ValueError:输入0与层conv2d_40不兼

我试图基于GTSRB数据集(下面给出的链接)制作一个CNN模型,但我面临以下错误:

当我设置input\u shape=input\u shape=(3,IMG\u SIZE,IMG\u SIZE)时,我得到以下错误:

ValueError:检查输入时出错:预期conv2d_34_输入为 有4个维度,但得到了具有形状的数组(9030,1)

当我研究这个问题时,我发现一个解决方案可能是将批次大小作为参数传递,当我尝试这样做时,我得到以下错误:

ValueError:输入0与层conv2d_40不兼容:应为 ndim=4,发现ndim=5

当我尝试重新塑造训练图像时,会出现以下错误:

ValueError:无法将大小为9030的数组重塑为形状(48,48,3)

代码片段: 加载培训数据集:

import csv

# Training dataset
def readTrafficSignsTrain(rootpath):
    '''Reads traffic sign data for German Traffic Sign Recognition Benchmark.

    Arguments: path to the traffic sign data, for example './GTSRB/Training'
    Returns:   list of images, list of corresponding labels'''
    images = [] # images
    labels = [] # corresponding labels

    # loop over all 42 classes
    for c in range(0,43):
#         prefix = rootpath + '/' + format(c, '05d') + '/' # subdirectory for class
#         annFile = open(prefix + 'GT-'+ format(c, '05d') + '.csv') # annotations file
        prefix = rootpath + '/00000' + '/'
        annFile = open(prefix + 'GT-00000' + '.csv')
        annReader = csv.reader(annFile, delimiter=';') # csv parser for annotations file
        next(annReader, None) # skip header

        # loop over all images in current annotations file
        for row in annReader:
            images.append(plt.imread(prefix + row[0])) # the 1st column is the filename
            labels.append(row[7]) # the 8th column is the label

        annFile.close()
    return images, labels

training_images, training_labels = readTrafficSignsTrain('./GTSRB/Training')
这里有一个问题,例如,图像形状不一样

print(len(training_images))
print(len(training_labels))
print()
print(training_images[0].shape)
print(training_images[20].shape)
print(training_images[200].shape)
print(training_images[2000].shape)
输出

9030 9030

(30,29,3)(54,57,3)(69,63,3)(52,51,3)

图层设置(从下面链接的Keras文档复制和粘贴):

培训模型(只是模型。暂时适合)

import numpy

trim = numpy.array(training_images)
trlb = numpy.array(training_labels)

print(training_images[0].shape)
print(trim.shape)

trim - trim.reshape(48, 48, 3)

model.fit(trim, trlb, epochs = 30, batch_size = 32)
输出

> ValueError: Error when checking model input: the list of Numpy arrays
> that you are passing to your model is not the size the model expected.
> Expected to see 1 array(s), but instead got the following list of 9030
> arrays: [array([[[ 75,  78,  80],
>             [ 74,  76,  78],
>             [ 86,  87,  84],
>             ...,
>             [ 68,  75,  75],
>             [ 65,  69,  68],
>             [ 66,  67,  66]],
>     
>            [[ 83,  84,  86],
>             [...
ValueError:无法将大小为9030的数组重塑为形状(48,48,3)

当我删除重塑

ValueError:检查输入时出错:预期conv2d\u 41\u输入为 有4个维度,但得到了具有形状的数组(9030,1)

当我用这个代替的时候

model.fit(training_images, training_labels, epochs = 30, batch_size = 32)
输出

> ValueError: Error when checking model input: the list of Numpy arrays
> that you are passing to your model is not the size the model expected.
> Expected to see 1 array(s), but instead got the following list of 9030
> arrays: [array([[[ 75,  78,  80],
>             [ 74,  76,  78],
>             [ 86,  87,  84],
>             ...,
>             [ 68,  75,  75],
>             [ 65,  69,  68],
>             [ 66,  67,  66]],
>     
>            [[ 83,  84,  86],
>             [...
因此,如果我这样做(不确定为什么)

我明白了

ValueError:检查输入时出错:预期conv2d\u 41\u输入为 有4个维度,但得到了形状为(30,29,3)的数组

这就是我的想法

input_shape=(3, IMG_SIZE, IMG_SIZE)
如果我

input_shape=(batch_size, 3, IMG_SIZE, IMG_SIZE)
我明白了

ValueError:输入0与层conv2d_47不兼容:应为 ndim=4,发现ndim=5

model.summary()的输出

如果有人能帮忙,我将不胜感激

链接 GTSRB: Keras文档我从以下地方获得模型:

github上完整项目的链接:


谢谢!

您无法将np.array重新塑造为尺寸不允许的形状。以下是您可以做的

import numpy as np 
img_arr = np.array([np.ones((30, 29, 3)), 
                    np.ones((54, 57, 3)), 
                    np.ones((69, 63, 3)), 
                    np.ones((52, 51, 3))])

print(img_arr.shape)

import cv2
img_arr_conv = np.array([cv2.resize(img, dsize=(48, 48)) for img in img_arr])
print(img_arr_conv.shape)

>>>(4,)
>>>(4, 48, 48, 3)
您得到的是
ValueError:无法将大小为9030的数组重塑为形状(48,48,3)
因为如果元素的大小都不同,numpy无法推断数组的尺寸,并且无法重塑尺寸不允许的数组。
ValueError:检查输入时出错:预期conv2d\u 41\u输入有4个尺寸,但得到了具有形状的数组(9030,1)
.Numpy只知道数组中有9030个元素。它不能做更多的事情,因为元素的所有维度都不同。
范例


希望这有帮助

您不能将np.array重塑为尺寸不允许的形状。以下是您可以做的

import numpy as np 
img_arr = np.array([np.ones((30, 29, 3)), 
                    np.ones((54, 57, 3)), 
                    np.ones((69, 63, 3)), 
                    np.ones((52, 51, 3))])

print(img_arr.shape)

import cv2
img_arr_conv = np.array([cv2.resize(img, dsize=(48, 48)) for img in img_arr])
print(img_arr_conv.shape)

>>>(4,)
>>>(4, 48, 48, 3)
您得到的是
ValueError:无法将大小为9030的数组重塑为形状(48,48,3)
因为如果元素的大小都不同,numpy无法推断数组的尺寸,并且无法重塑尺寸不允许的数组。
ValueError:检查输入时出错:预期conv2d\u 41\u输入有4个尺寸,但得到了具有形状的数组(9030,1)
.Numpy只知道数组中有9030个元素。它不能做更多的事情,因为元素的所有维度都不同。
范例


希望这有帮助

所以,一个可能的解决方案是将img_宽度和img_高度传递给输入_形状?例如输入_形状(3,img_宽度,img_高度)不,我是说调整图像大小使其与
img_arr_conv=np.array相同([cv2.resize(img,dsize=(48,48))用于训练_图像中的img])
我会试试的。谢谢。非常感谢,好先生。你不知道我在这个错误上浪费了多少时间。我想我只是在装傻。再次感谢你!所以,一个可能的解决办法是将img_宽度和img_高度传递到输入_形状?例如输入_形状(3,img_宽度,img_高度)不,我是说将你的图像调整为相同的
img\u arr\u conv=np。数组([cv2.resize(img,dsize=(48,48))用于img in training\u images])
将尝试。谢谢。非常感谢你,好先生。你不知道我在这个错误上浪费了多少时间。我想我只是在装傻。再次感谢你!
import numpy as np 
img_arr = np.array([np.ones((30, 29, 3)), 
                    np.ones((54, 57, 3)), 
                    np.ones((69, 63, 3)), 
                    np.ones((52, 51, 3))])

print(img_arr.shape)

import cv2
img_arr_conv = np.array([cv2.resize(img, dsize=(48, 48)) for img in img_arr])
print(img_arr_conv.shape)

>>>(4,)
>>>(4, 48, 48, 3)
img_arr_bad = np.array([np.ones((30, 29, 3)), 
                        np.ones((54, 57, 3)), 
                        np.ones((69, 63, 3)), 
                        np.ones((52, 51, 3))])

img_arr_good = np.array([np.ones((48, 48, 3)), 
                         np.ones((48, 48, 3)), 
                         np.ones((48, 48, 3)), 
                         np.ones((48, 48, 3))])

print(img_arr_bad.shape)
print(img_arr_good.shape)

>>>(4,)
>>>(4, 48, 48, 3)