我在使用 imageai 包进行自定义产品检测时出错

i have a error when im using imageai package to do custom product detection

我正在参加关于产品检测的比赛。我正在使用 imageai 包并且我很好地遵循了说明

我的代码:

from imageai.Prediction.Custom import ModelTraining
model_trainer = ModelTraining()
model_trainer.setModelTypeAsResNet()
model_trainer.setDataDirectory(r"C:\Users\User\Desktop\directory\Copy of shopee-product-detection-dataset")
model_trainer.trainModel(num_objects=41, num_experiments=200, enhance_data=True, batch_size=32, show_network_summary=True)

但是当我运行时,出现了这个错误:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\directory\shopeetrainingcode.py", line 9, in <module>
    model_trainer.trainModel(num_objects=41, num_experiments=200, enhance_data=True, batch_size=32, show_network_summary=True)
  File "C:\Program Files\Python36\lib\site-packages\imageai\Prediction\Custom\__init__.py", line 342, in trainModel
    validation_steps=int(num_test / batch_size), callbacks=[checkpoint, lr_scheduler, tensorboard])
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\_impl\keras\engine\training.py", line 1598, in fit_generator
    initial_epoch=initial_epoch)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\_impl\keras\engine\training_generator.py", line 191, in fit_generator
    x, y, sample_weight=sample_weight, class_weight=class_weight)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\_impl\keras\engine\training.py", line 1378, in train_on_batch
    class_weight=class_weight)
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\_impl\keras\engine\training.py", line 804, in _standardize_user_data
    exception_prefix='target')
  File "C:\Program Files\Python36\lib\site-packages\tensorflow\python\keras\_impl\keras\engine\training_utils.py", line 192, in standardize_input_data
    ' but got array with shape ' + str(data_shape))
ValueError: Error when checking target: expected activation_50 to have shape (41,) but got array with shape (42,)

有人知道问题出在哪里吗?

这可能是因为您在设置 num_objects=41 时试图传递 42 个对象。像这样用 num_objects=42 试试:

from imageai.Prediction.Custom import ModelTraining
    model_trainer = ModelTraining()
    model_trainer.setModelTypeAsResNet()
    model_trainer.setDataDirectory(r"C:\Users\User\Desktop\directory\Copy of shopee-product-detection-dataset")
    model_trainer.trainModel(num_objects=42, num_experiments=200, enhance_data=True, batch_size=32, show_network_summary=True)