Haicaji 2023-04-03 09:40 采纳率: 25%
浏览 37
已结题

关于#python#的问题:在使用ImagAI训练模型时遇到问题

在使用ImagAI训练模型时遇到问题,应该如何解决
代码如下

from imageai.Prediction.Custom import ModelTraining
model_trainer = ModelTraining()
model_trainer.setModelTypeAsResNet()
model_trainer.setDataDirectory("ClashOfClans")
model_trainer.trainModel(
    num_objects=1,
    num_experiments=100,
    enhance_data=True,
    batch_size=2,
    show_network_summary=True)

遇到以下报错:

You are passing a target array of shape (2, 1) while using as loss `categorical_crossentropy`. `categorical_crossentropy` expects targets to be binary matrices (1s and 0s) of shape (samples, classes). If your targets are integer classes, you can convert them to the expected format via:

from keras.utils import to_categorical
y_binary = to_categorical(y_int)


Alternatively, you can use the loss function `sparse_categorical_crossentropy` instead, which does expect integer targets.
  File "E:\python file\AI Program\3.imageAI\8.TrainModel.py", line 10, in <module>
    show_network_summary=True)
ValueError: You are passing a target array of shape (2, 1) while using as loss `categorical_crossentropy`. `categorical_crossentropy` expects targets to be binary matrices (1s and 0s) of shape (samples, classes). If your targets are integer classes, you can convert them to the expected format via:

from keras.utils import to_categorical
y_binary = to_categorical(y_int)


Alternatively, you can use the loss function `sparse_categorical_crossentropy` instead, which does expect integer targets.

  • 写回答

2条回答 默认 最新

  • 沐阳gg 2023-04-03 09:48
    关注

    根据错误提示,你使用 categorical_crossentropy 作为损失函数,但是你的目标数组的形状为 (2,1),而 categorical_crossentropy 函数期望的目标数组形状应该是 (samples, classes),即二进制矩阵 (1s 和 0s)。
    解决办法:
    1、将目标数组转换为二进制矩阵形式:可以使用 Keras 工具包中的 to_categorical 函数将目标数组转换为期望的二进制矩阵形式。具体可以使用以下代码将 y_int 转换为二进制矩阵形式并将结果存储在 y_binary 中:

     from keras.utils import to_categorical
        y_binary = to_categorical(y_int)
    

    然后,你可以将 y_binary 作为目标数组传递给 trainModel 函数:

    model_trainer.trainModel(
        num_objects=1,
        num_experiments=100,
        enhance_data=True,
        batch_size=2,
        show_network_summary=True,
        train_from_pretrained_model="pretrained.h5",
        continue_from_epoch=10,
        initial_learning_rate=1e-4,
        learning_rate_schedule="cosine_decay",
        learning_rate_decay_epochs=10,
        learning_rate_drop=0.1,
        train_with_frozen_layers="last_layers",
        save_transfer_learning_weights=True,
        verbose=True,
        tensorboard_verbose=0,
        tensorboard_dir="logs/",
        validation_split=0.1,
        random_transform=True,
        **y_binary**)
    

    望采纳

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 5月28日
  • 创建了问题 4月3日