简体   繁体   English

ValueError:检查输入时出错:预期dense_1_input 具有形状(9,) 但得到形状为(1,) 的数组

[英]ValueError: Error when checking input: expected dense_1_input to have shape (9,) but got array with shape (1,)

hi so I build a DNN network to classify some objects in an image using the features of the object, like bellow :嗨,所以我构建了一个 DNN 网络来使用对象的特征对图像中的某些对象进行分类,如下所示:

contours, _ = cv2.findContours(imgthresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

for contour in contours:
    features = np.array([])
    (x_start, y_start, character_width, character_height) = cv2.boundingRect(contour)
    x_end = x_start + character_width
    y_end = y_start + character_height
    character_area = character_width * character_height
    features = np.append(features, [character_width, character_height, character_area, x_start,
                                    y_start, x_end, y_end, image_width, image_height])

    print(features)
    print(features.shape)
    cv2.rectangle(image, (x_start, y_start), (x_end, y_end), (0, 255, 0), thickness=1)

print(features) output is: print(features)输出是:

[  5.   1.   5. 105.  99. 110. 100. 100. 117.]

and print(features.shape) is:print(features.shape)是:

(9,)

I build and trained a DNN using the following code :我使用以下代码构建并训练了一个 DNN:

model = Sequential()

model.add(Dense(50, input_dim=9, activation='relu'))

model.add(Dropout(0.5))

model.add(Dense(40, activation='relu'))

model.add(Dropout(0.5))

model.add(Dense(30,activation='relu'))

model.add(Dense(2, activation='softmax'))

The input layer has 9 input features.输入层有 9 个输入特征。 So I tried to get the prediction of the model using:所以我尝试使用以下方法获得模型的预测:

model.predict_classes(features)

The training data, a CSV file, contains 10 columns (9 features and 1 for the output)训练数据,一个CSV文件,包含 10 列(9 个特征和 1 个用于输出)

I got the following error :我收到以下错误:

ValueError: Error when checking input: expected dense_1_input to have shape (9,) but got array with shape (1,)

I tried to reshape the features array by using :我尝试使用以下方法重塑 features 数组:

np.reshape(features,(1,9)

but that didn't work either.但这也不起作用。 I am still new at this field我还是这个领域的新手

Here is a minimal working example.这是一个最小的工作示例。

import numpy as np
import tensorflow as tf

def main():
    features = np.array([5, 1, 5, 105, 99, 110, 100, 100, 117])
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.Dense(50, input_dim=9, activation="relu"))

    print(tf.expand_dims(features, 0))
    print(np.reshape(features, (1, 9)))

    print(model.predict_classes(np.reshape(features, (1, 9))))


if __name__ == '__main__':
    main()

As you can see, the np.reshape call make it works.如您所见, np.reshape调用使它起作用。 It is roughly equivalent to the tf.expand_dims .它大致相当于tf.expand_dims

Your current error comes from the fact that your model expect a batch dimension.您当前的错误来自您的模型需要批量维度这一事实。 So, if you pass it an array of shape (9,) it infers that it's a batch of scalars, and not a single array of size 9.所以,如果你传递给它一个形状为(9,)的数组(9,)它会推断它是一批标量,而不是一个大小为 9 的数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:检查输入时出错:预期density_1_input的形状为(24,),但数组的形状为(1,) - ValueError: Error when checking input: expected dense_1_input to have shape (24,) but got array with shape (1,) ValueError:检查输入时出错:预期dense_1_input 具有形状(8,) 但得到形状为(1,) 的数组 - ValueError: Error when checking input: expected dense_1_input to have shape (8,) but got array with shape (1,) ValueError:检查输入时出错:预期dense_1_input的形状为(1,)但得到的数组形状为(5000,) - ValueError: Error when checking input: expected dense_1_input to have shape (1,) but got array with shape (5000,) ValueError:检查输入时出错:预期dense_1_input具有形状(180,)但得到形状为(1,)的数组 - ValueError: Error when checking input: expected dense_1_input to have shape (180,) but got array with shape (1,) ValueError:检查时出错:预期dense_1_input具有形状(3,)但得到形状为(1,)的数组 - ValueError: Error when checking : expected dense_1_input to have shape (3,) but got array with shape (1,) ValueError:检查时出错:预期density_1_input具有2维,但数组的形状为(1,16,16,512) - ValueError: Error when checking : expected dense_1_input to have 2 dimensions, but got array with shape (1, 16, 16, 512) 如何修复'ValueError:检查输入时出错:期望dense_1_input有形状(4,)但是在Python中有错误的形状(1,)数组? - How to fix ‘ValueError: Error when checking input: expected dense_1_input to have shape (4,) but got array with shape (1,)’ error in Python? ValueError:检查输入时出错:预期density_1_input具有形状(1,224,224),但数组形状为(224,224,1) - ValueError: Error when checking input: expected dense_1_input to have shape (1, 224, 224) but got array with shape (224, 224, 1) 检查输入时出错:预期density_1_input具有形状(3773),但数组的形状为(111,) - Error when checking input: expected dense_1_input to have shape (3773,) but got array with shape (111,) 检查输入时出错:预期dense_1_input 具有形状(784,) 但得到形状为(10,) 的数组 - Error when checking input: expected dense_1_input to have shape (784,) but got array with shape (10,)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM