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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题