weixin_45099845 2019-09-22 14:33 采纳率: 42.9%
浏览 1147
已结题

python里面的imageai模块,我想把里面的 默认模型 换成 我自己的模型,该怎么操作?

网上流传的十行代码就能目标检测,代码如下,并且我亲测有效:
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需要修改,可是这个该怎么修改?求助

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-09-22 16:29
    关注

    detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
    这里你加载了一个保存的模型,包括了训练的权重和偏置

    那么你的模型必须和保存的模型完全一致,包括你的层数、节点数,你的模型文件是107层,而你定义的模型是116层,不匹配。实在要做,只能丢弃这个模型文件,从头训练了。

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?