检查目标时出错:预期密集_3具有形状(4),但获得具有形状(10)的数组

2024-04-24 10:16:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python初学者 我使用Keras和Tensorflow创建ML模型。当我有4个类时,它工作得很好,但是添加了更多的类,它得到了这个错误。我们已经增加了10门课。 我使用python 3.7、Keras 2.3.1和Tensorflow 1.14。 我在这个链接中遵循教程 here。 我已经浪费了三天时间来解决这个问题,但我不能 谢谢

Classification.py文件

import os as os
from keras.models import load_model
from keras.utils import np_utils

import model as md
import preparation as prep
import visualization as vis

from keras.utils import to_categorical
from keras import metrics

# ----------- dataset settings -----------

# number of instances per class used for train and test in total:
# should be smaller or equal than generated subset
INSTANCES_PER_CLASS = 5000
NUM_CLASS_LIMIT = 345 # limit of classes
# path of the dataset seperated in train and test
DATA_PATH = os.path.join(os.getcwd(), "../Draw/dataset/train_test_20k/")
# path for all created files
MODEL_PATH = os.path.join(os.getcwd(), "models/" + str(NUM_CLASS_LIMIT) + "/" + str(INSTANCES_PER_CLASS) + "/")

# ----------- model settings -----------

MODEL_NAME = 'model.h5' # name for the freezed model
# input size
IMG_WIDTH = 28
IMG_HEIGHT = 28
IMG_SIZE = IMG_WIDTH * IMG_HEIGHT
IMG_DIM = 1

# training settings
EPOCHS = 10
BATCH_SIZE = 256




if __name__ == "__main__":

    # create new directories if required
    if not os.path.isdir(MODEL_PATH):
        os.makedirs(MODEL_PATH)

    # get the dataset
    num_classes, x_train, x_test, y_train, y_test, classes_dict = prep.collect_data(NUM_CLASS_LIMIT)

    print("trainingsset instances {}".format(x_train.shape))
    print("trainingsset labels {}".format(y_train.shape))

    # plot first test images
    #vis.plot_first_n_images(x_test, y_test, classes_dict, 100)


    # class representation as "binary" vector
    y_train = np_utils.to_categorical(y_train, num_classes=num_classes)
    y_test = np_utils.to_categorical(y_test, num_classes=num_classes)

    # create or load keras model
    if not os.path.isfile(MODEL_PATH + MODEL_NAME):
        print("create model...")
        model = md.build_model(input_shape=x_train.shape[1:], num_classes=num_classes)


    else:
        print("load existing model...")
        model = load_model(MODEL_PATH +  MODEL_NAME)

        # score trained model using validation set
        scores = model.evaluate(x_test, y_test, verbose=1)
        print('test loss:', scores[0])
        print('test accuracy:', scores[1])


    model.compile(loss='categorical_crossentropy',
            optimizer='adam',
            metrics=['acc'])

    # print model information if desired
    print(model.summary())

    # model training from scratch or retrain by existing model
    hist = model.fit(x_train, y_train, batch_size=BATCH_SIZE,
                    epochs=EPOCHS,
                    validation_data=[x_test, y_test],
                    shuffle=True)


    #from keras.utils import plot_model
    #plot_model(model, to_file=MODEL_PATH + 'model.png')

    # evaluation process
    print("evaluate model...")

    # summarize history during training phase
    # plot training and validation set accuracy
    vis.plot_training_history_accuracy(hist)

    # test set evaluation

    scores = model.evaluate(x_test, y_test, verbose=1)
    print(scores)
    print('test loss:', scores[0])
    print('test accuracy:', scores[1])

    # create and plot confusion matrix
    #y_pred = model.predict(x_test)
    #vis.plot_confusion_matrix(y_pred, y_test, classes=list(classes_dict.values()))


    # freeze the model (architecture and weights)
    model.save(os.path.join(MODEL_PATH, MODEL_NAME))
    print('saved trained model at  {}'.format(os.path.join(MODEL_PATH, MODEL_NAME)))

模型代码Model.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from keras.models import Model
from keras.layers import Input, Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D


def build_model(input_shape, num_classes):
    """
    Builds the model architecture based on MNIST CNN
    https://www.tensorflow.org/tutorials/estimators/cnn

    Args:
        input_spape: Input shape of the model
        num_classes: Number of classes

    Returns:
        keras.models.Model: The created model

    """



    inputs = Input(shape=input_shape)

    x = Conv2D(32, (5,5), activation='relu')(inputs)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Conv2D(128, (3, 3), activation='relu')(x)
    x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Dropout(0.2)(x)
    x = Flatten()(x)
    x = Dense(512, activation='relu')(x)
    x = Dense(256, activation='relu')(x)
    predictions = Dense(num_classes, activation='softmax')(x)
    print("SHOW DESEN", num_classes)

    return Model(inputs=inputs, outputs=predictions)

Tags: pathfromtestimportmodelplotostrain
1条回答
网友
1楼 · 发布于 2024-04-24 10:16:50

我认为错误来自这一步:

hist = model.fit(x_train, y_train, batch_size=BATCH_SIZE,
                    epochs=EPOCHS,
                    validation_data=[x_test, y_test],
                    shuffle=True)

对“target”的引用表示“y_train”数组。您可能希望通过在拟合之前检查y_train.shape来确认尝试拟合的y_列与实际model.summary()匹配。这可能是因为设计用于4个等级的旧型号正在使用,即使新的10级y_列车正在传递给model.fit()

诊断代码示例(已注释掉model.fit):

# This outputs the model design.  The final layer should be (None,num_classes)
print(model.summary())
# print shape of y_train/target data.  The second dimension should match the number of classes from above (?, num_classes)
print(y_train.shape)
#model.fit( x_train, y_train, epochs=1)

我希望这有帮助

相关问题 更多 >