检查目标时发生错误:预期density_3的形状为(4,),但数组的形状为(10,)

问题描述 投票:0回答:1

我是Python的初学者我使用Keras和Tensorflow创建ML模型。当我有4个课程时,它可以很好地工作,但是添加更多的课程,则会出现此错误。添加添加了10个类。我使用python 3.7,Keras 2.3.1,Tensorflow 1.14。我正在此链接中关注教程here。我已经失去了3天的时间来解决此问题,但我无法谢谢

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)
python tensorflow keras
1个回答
0
投票

我相信该错误来自此步骤:

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

对“目标”的引用表示“ y_train”数组。您可能希望通过检查拟合之前的y_train.shape来确认要拟合的y_train与实际的model.summary()相匹配。即使将新的10类y_train传递给model.fit(),也有可能正在使用为4个类设计的旧模型。

诊断代码示例(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)

我希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.