zoukankan      html  css  js  c++  java
  • Detectron2--(1)

    首先,是官方自己觉得很好的colab教程,我啥也运行不了,不说了,但是colab的教程告诉我一件事情,这个东西运行inference真的很简单。

    cfg = get_cfg() #有一个叫cfg的模型配置文件
    # add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
    # cfg 获取mask-rcnn的配置,我猜是这个意思
    cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
    # 妈的这个阈值我不知道是啥意思。。。。。。 cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST
    = 0.5 # set threshold for this model # Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as well
    # 给模型上参数,这个yaml文件里应该存了不少东西,存了模型结构,还存了网址?
    cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
    #这里就是个坑了,defaultpredictor似乎是cuda的,我cpu predictor
    = DefaultPredictor(cfg) outputs = predictor(im)


      outputs["instances"].pred_classes
      outputs["instances"].pred_boxes

    ok, 这个colab到此为止,我们回去看文档,1.怎么在cpu下运行,2.怎么本地加载模型参数,3.怎么输出我需要的信息,mask的结果,每个像素所属的类别

    https://detectron2.readthedocs.io/tutorials/models.html

    #这三个输出是比较有用的

    • “scores”: Tensor, a vector of N scores.
    • “pred_classes”: Tensor, a vector of N labels in range [0, num_categories).
    • “pred_masks”: a Tensor of shape (N, H, W), masks for each detected instance.

    Another small thing to remember: detectron2 models do not support model.to(device) or model.cpu(). The device is defined in cfg.MODEL.DEVICE and cannot be changed afterwards.#在cfg里面定义使用cpu

    同志们,我算是看懂了,想用它的推理模型一共三步,先配置一个cfgnode

    cfg = get_cfg() 
    cfg.merge_from_file("../configs/COCO-InstanceSegmentatio/mask_rcnn_R_50_FPN_3x.yaml")
    cfg.merge_from_list(["MODEL.WEIGHTS", "../pre_train_model/model_final_f10217.pkl","MODEL.DEVICE","cpu"])#cfg加配置,用cpu一定要在这里写cpu
    predictor = DefaultPredictor(cfg) #用predictor包装一下
    output = predictor(im) #输入图片,output就是推理结果了

    如果不想用predictor,要写成

    model = build_model(cfg)

    output = model(input)

    但是这种情况下,要按照API要求把input改成一个字典,我改成字典了还是运行不了,是不是一次输入的图片不足一个batch的原因呢?还不知道

    反正predictor这个方法是可行的吧。

    要看输出也很简单,

    output["instances"].pred_classes 打印为每一个instance预测的类别,一张图最后给了我48个instance,有点离谱.
    类别不为0的有17个,大概为0的都是背景了
     
    这篇先到这里,下一篇研究下,这个输出到底咋回事,还有模型的选取问题。
     
    真的要仔细,临走发现个bug,忘了设置阈值,
    cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
    加上了之后才和colab的结果一样。
  • 相关阅读:
    mysql索引类型 normal, unique, full text
    16.信号量互斥编程
    15.信号通信编程
    14.有名管道通信
    13.无名管道通讯编程
    12.多进程程序的操作
    11.进程控制理论
    10.时间编程
    9. 库函数方式文件编程
    8.Linux文件编程
  • 原文地址:https://www.cnblogs.com/zherlock/p/12514358.html
Copyright © 2011-2022 走看看