关于#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 
    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.

根据错误提示,你使用 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**)

望采纳

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^