【问题标题】:error when checking target expected dense_2 to have shape (13 ) but got array with shape ( 40)检查目标预期的 dense_2 具有形状(13)但得到具有形状(40)的数组时出错
【发布时间】:2018-05-23 11:24:09
【问题描述】:

您好,我正在编写一个神经元来确定数字计数

def get_image_size():
    img = cv2.imread('gestures/0/100.jpg', 0)
    return img.shape // 50*50

def get_num_of_classes():
    return len(os.listdir('gestures/')) //13classes

image_x, image_y = get_image_size()

CNN 模型

def cnn_model():
    num_of_classes = get_num_of_classes()
    model = Sequential()
    model.add(Conv2D(32, (5,5), input_shape=(image_x, image_y, 1), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same'))
    model.add(Conv2D(64, (5,5), activation='relu'))
    model.add(MaxPooling2D(pool_size=(5, 5), strides=(5, 5), padding='same'))
    model.add(Flatten())
    model.add(Dense(1024, activation='relu'))
    model.add(Dropout(0.4))
    model.add(Dense(num_of_classes, activation='softmax'))
    sgd = optimizers.SGD(lr=1e-4)
    model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
    filepath="cnn_model_keras2.h5"
    checkpoint1 = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
    #checkpoint2 = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=True, mode='min')
    callbacks_list = [checkpoint1]
    return model, callbacks_list

培训

def train():
    with open("train_images", "rb") as f:
        train_images = np.array(pickle.load(f))
    with open("train_labels", "rb") as f:
        train_labels = np.array(pickle.load(f), dtype=np.int32)

    with open("test_images", "rb") as f:
        test_images = np.array(pickle.load(f))
    with open("test_labels", "rb") as f:
        test_labels = np.array(pickle.load(f), dtype=np.int32)

    train_images = np.reshape(train_images, (train_images.shape[0], image_x, image_y, 1))
    test_images = np.reshape(test_images, (test_images.shape[0], image_x, image_y, 1))
    train_labels = np_utils.to_categorical(train_labels)
    test_labels = np_utils.to_categorical(test_labels)

    model, callbacks_list = cnn_model()
    model.fit(train_images, train_labels, validation_data=(test_images, test_labels), epochs=50, batch_size=100, callbacks=callbacks_list)
    scores = model.evaluate(test_images, test_labels, verbose=0)
    print("CNN Error: %.2f%%" % (100-scores[1]*100))

但我收到此错误:ValueError: Error when checking target: expected dense_1 to have shape (13,) but got array with shape (40,) 我搜索了一些解决方案但没有任何效果,如果有人知道如何解决它,请

【问题讨论】:

  • 您的网络配置为输出 13 个类,但您的标签似乎有 40 个类。由您来找出为什么存在差异,因为这里没有生成这些数组的代码。这也可以通过查看 train_labels 的形状来看出
  • 我正在尝试破解验证码,甚至我也面临同样的错误。有办法解决吗?

标签: python neural-network keras anaconda artificial-intelligence


【解决方案1】:

错误表明您用于训练的标签数量与您预测的不同。看代码,好像是num_of_classes != train_labels.shape[1]。如果您只有一个类标签向量,例如[3, 7, 1],那么您可以使用loss='sparse_categorical_crossentropy',它将在为您训练时对目标进行编码。

【讨论】:

  • 感谢您的回答,我已经尝试过这样做:loss='sparse_categorical_crossentropy' 但我收到此错误error when checking target expected dense_2 to have shape (1 ) but got array with shape ( 40)
  • 您的train_labels 是什么样的?这与模型的输出之间本质上存在形状不匹配。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-18
相关资源
最近更新 更多