我已经使用自己的图像数据集训练了一个自定义模型来识别图像中的对象,但似乎无法识别所有对象。我只有两种对象(一个人以不同方式书写特定字母的图像)。例如,字母“a”和字母“o”如下所示。
当我在手写文本样本上运行测试代码时,它在一定程度上确实显示了准确率的百分比,但没有边界框。这是手写文本的图像:
这是我得到的输出:
我使用imageai来训练自定义模型。我想知道是否可以使用这个训练好的模型在手写图像上进行多对象检测,并可能显示边界框?
以防提供额外帮助,这是我的工作目录的外观:
这是我用于训练模型的代码(custom_detector.py):
from imageai.Prediction.Custom import ModelTraining# Instanciating the modelmodel_trainer = ModelTraining()model_trainer.setModelTypeAsResNet()# Setting our dataset directyorymodel_trainer.setDataDirectory("characters")# training the modelmodel_trainer.trainModel(num_objects=2, num_experiments=100, enhance_data=True, batch_size=10)
这是我用于测试训练模型的代码(test.py):
from imageai.Prediction.Custom import CustomImagePredictionimport os# get the working directoryexecution_path = os.getcwd()print(execution_path)# instanciate predictionprediction = CustomImagePrediction()prediction.setModelTypeAsResNet()# Set model pathprediction.setModelPath(os.path.join(execution_path, "characters", "models", "myModel.h5"))# Set JSON path# This is how the JSON file looks like:#{# "0" : "A",# "1" : "O"#}prediction.setJsonPath(os.path.join(execution_path, "characters", "json", "model_class.json"))# Initialize the number of objects you have retrainedprediction.loadModel(num_objects=2)# run predictionpredictions, probabilities = prediction.predictImage(os.path.join(execution_path, "HandTextTest.jpg"), result_count=2)# Print each predictionfor eachPrediction, eachProbability in zip(predictions, probabilities): print(eachPrediction, " : ", eachProbability)
任何帮助或建议将不胜感激。
回答:
# run predictionpredictions, probabilities = prediction.predictImage(os.path.join(execution_path, "HandTextTest.jpg"), result_count=2)
predictImage函数负责预测相应图像的边界框。但此函数不会在图像上绘制边界框,它只显示不同类别的概率。参见predictImage代码。
而detectCustomObjectsFromVideo函数则在结果视频帧上绘制边界框,并将结果保存到文件中。参见detectCustomObjectsFromVideo代码。
因此,这不是模型能做什么的问题,因为predict函数不支持在图像上绘制边界框。你可以修改代码以在图像上绘制边界框,或者使用其他能够为你提供带边界框图像的包或框架。
如果你有任何问题,请随时评论。