在使用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.