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层,不匹配。实在要做,只能丢弃这个模型文件,从头训练了。

    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的