网上流传的十行代码就能目标检测,代码如下,并且我亲测有效:
import PIL
import h5py
import imageai
import os
from imageai.Detection import ObjectDetection
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "02-c.tif"), output_image_path=os.path.join(execution_path , "02-cnew.tif"))
for eachObject in detections:
print(eachObject["name"] , " : " , eachObject["percentage_probability"] )
以上代码的确能把图片中的 person,car,bus,bird,bow,goat等物体检测出来,resnet50_coco_best_v2.0.1.h5这个模型是前辈的模型,第八行 detector.setModelTypeAsRetinaNet() 中RetinaNet是前辈默认模型的modeltype。
假如我训练好了一个识别各种鸟的模型,而前辈的模型并不能识别各种不同的鸟,我想把自己的模型加载进来,该怎么操作。如果只是把resnet50_coco_best_v2.0.1.h5替换成我的模型,会报错ValueError: You are trying to load a weight file containing 107 layers into a model with 116 layers.
意思是第八行中的RetinaNet需要修改,可是这个该怎么修改?求助