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

时间:2018-10-24 20:03:22

标签: python arrays tensorflow keras training-data

我正在尝试基于GTSRB数据集(以下给出的链接)创建CNN模型,但遇到以下错误:

当我设置input_shape = input_shape =(3,IMG_SIZE,IMG_SIZE)时,出现此错误:

  

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

研究问题时,我发现一个解决方案可能是将batch_size作为参数传递,当我尝试这样做时,出现此错误:

  

ValueError:输入0与层conv2d_40不兼容:预期   ndim = 4,找到的ndim = 5

当我尝试重塑training_images时,出现此错误:

  

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文档复制和粘贴):

IMG_SIZE = 48
NUM_CLASSES = 43
K.set_image_data_format('channels_first')

batch_size = 32

def cnn_model():
    model = Sequential()

    model.add(Conv2D(32, (3, 3), padding='same',
                     input_shape=(3, IMG_SIZE, IMG_SIZE),
                     activation='relu',
                     data_format="channels_first"))
    model.add(Conv2D(32, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2), data_format="channels_first"))
    model.add(Dropout(0.2))

    model.add(Conv2D(64, (3, 3), padding='same',
                     activation='relu'))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))

    model.add(Conv2D(128, (3, 3), padding='same',
                     activation='relu'))
    model.add(Conv2D(128, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))

    model.add(Flatten())
    model.add(Dense(512, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(NUM_CLASSES, activation='softmax'))
    return model

model = cnn_model()

训练模型(暂时暂时为model.fit

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:无法将大小为9030的数组重塑为形状(48,48,3)

当我移除重塑形状

  

ValueError:检查输入时出错:预期的conv2d_41_input为   有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],
>             [...

所以,如果我这样做(不确定原因)

for i in range(len(training_images)):
    model.fit(training_images[i], training_labels[i], epochs = 30, batch_size = 32)

我明白了

  

ValueError:检查输入时出错:预期的conv2d_41_input为   有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()的输出

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_34 (Conv2D)           (None, 32, 48, 48)        896       
_________________________________________________________________
conv2d_35 (Conv2D)           (None, 32, 46, 46)        9248      
_________________________________________________________________
max_pooling2d_14 (MaxPooling (None, 32, 23, 23)        0         
_________________________________________________________________
dropout_14 (Dropout)         (None, 32, 23, 23)        0         
_________________________________________________________________
conv2d_36 (Conv2D)           (None, 64, 23, 23)        18496     
_________________________________________________________________
conv2d_37 (Conv2D)           (None, 64, 21, 21)        36928     
_________________________________________________________________
max_pooling2d_15 (MaxPooling (None, 64, 10, 10)        0         
_________________________________________________________________
dropout_15 (Dropout)         (None, 64, 10, 10)        0         
_________________________________________________________________
conv2d_38 (Conv2D)           (None, 128, 10, 10)       73856     
_________________________________________________________________
conv2d_39 (Conv2D)           (None, 128, 8, 8)         147584    
_________________________________________________________________
max_pooling2d_16 (MaxPooling (None, 128, 4, 4)         0         
_________________________________________________________________
dropout_16 (Dropout)         (None, 128, 4, 4)         0         
_________________________________________________________________
flatten_4 (Flatten)          (None, 2048)              0         
_________________________________________________________________
dense_10 (Dense)             (None, 512)               1049088   
_________________________________________________________________
dropout_17 (Dropout)         (None, 512)               0         
_________________________________________________________________
dense_11 (Dense)             (None, 43)                22059     
=================================================================
Total params: 1,358,155
Trainable params: 1,358,155
Non-trainable params: 0
_________________________________________________________________
None

如果任何人都可以提供帮助,那将不胜感激。

链接 GTSRB:http://benchmark.ini.rub.de/?section=gtsrb&subsection=news 我从以下网址获得了Keras文档:https://chsasank.github.io/keras-tutorial.html

有关github上完整项目的链接:https://github.com/PavlySz/TSR-Project

谢谢!

1 个答案:

答案 0 :(得分:0)

您不能将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: cannot reshape array of size 9030 into shape (48,48,3),是因为如果元素的大小都不同,则numpy无法推断数组的尺寸,并且无法重新调整尺寸不允许的数组形状。 ValueError: Error when checking input: expected conv2d_41_input to have 4 dimensions, but got array with shape (9030, 1)的情况也是如此。 Numpy只知道数组中有9030个元素。它只能做些什么,因为元素的所有尺寸都不同。
例子

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)

希望这会有所帮助