重磅资源|Pytorch1.0版本的Mask R-CNN的Facebook的官方实现
【導讀】Facebook剛剛放出的基于Pytorch1.0版本的Faster R-CNN,Mask R-CNN的benchmark,比detectron更快,準確率更高。
項目地址:
https://github.com/facebookresearch/maskrcnn-benchmark
Faster R-CNN and Mask R-CNN in PyTorch 1.0
這個項目目的是為使用Pytorch1.0的用戶提供一個簡單的搭建檢測和分割模型的必要的底層模塊。
要點
網絡攝像頭和 Jupyter notebook的demo
我們提供了簡單的網絡攝像頭的demo,為你演示如何使用maskrcnn_benchmark進行推理:
cd?demo#?by?default,?it?runs?on?the?GPU#?for?best?results,?use?min-image-size?800python?webcam.py?--min-image-size?800#?can?also?run?it?on?the?CPUpython?webcam.py?--min-image-size?300?MODEL.DEVICE?cpu#?or?change?the?model?that?you?want?to?usepython?webcam.py?--config-file?../configs/caffe2/e2e_mask_rcnn_R_101_FPN_1x_caffe2.py?--min-image-size?300?MODEL.DEVICE?cpu#?in?order?to?see?the?probability?heatmaps,?pass?--show-mask-heatmapspython?webcam.py?--min-image-size?300?--show-mask-heatmaps?MODEL.DEVICE?cpu#?by?default,?it?runs?on?the?GPU #?for?best?results,?use?min-image-size?800 python?webcam.py?--min-image-size?800 #?can?also?run?it?on?the?CPU python?webcam.py?--min-image-size?300?MODEL.DEVICE?cpu #?or?change?the?model?that?you?want?to?use python?webcam.py?--config-file?../configs/caffe2/e2e_mask_rcnn_R_101_FPN_1x_caffe2.py?--min-image-size?300?MODEL.DEVICE?cpu #?in?order?to?see?the?probability?heatmaps,?pass?--show-mask-heatmaps python?webcam.py?--min-image-size?300?--show-mask-heatmaps?MODEL.DEVICE?cpu這個demo在 demo/Mask_R-CNN_demo.ipynb可以找到。
安裝
查看 INSTALL.md 的安裝指令。
模型庫和基線版本
預訓練模型,基線版本以及和Detectron和mmdetection的對比在MODEL_ZOO.md中。
幾行代碼實現推理
我們提供了一個幫助類來簡化使用預訓練模型實現推理的pipline。下面是如何實現,可以在?demo?文件夾中運行:
from?maskrcnn_benchmark.config?import?cfgfrom?predictor?import?COCODemoconfig_file?=?"../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml"#?update?the?config?options?with?the?config?filecfg.merge_from_file(config_file)#?manual?override?some?optionscfg.merge_from_list(["MODEL.DEVICE",?"cpu"])coco_demo?=?COCODemo(????cfg,????min_image_size=800,????confidence_threshold=0.7,)#?load?image?and?then?run?predictionimage?=?...predictions?=?coco_demo.run_on_opencv_image(image)config_file?=?"../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml"#?update?the?config?options?with?the?config?file cfg.merge_from_file(config_file) #?manual?override?some?options cfg.merge_from_list(["MODEL.DEVICE",?"cpu"])coco_demo?=?COCODemo(cfg,min_image_size=800,confidence_threshold=0.7, ) #?load?image?and?then?run?prediction image?=?... predictions?=?coco_demo.run_on_opencv_image(image)在COCO數據集上訓練的表現
需要先安裝?maskrcnn_benchmark,后面的例子才能運行。
需要先下載COCO數據集。我們推薦將coco數據集的軟鏈接放到datasets/里,如下:
我們使用 Detectron中的?minival?and?valminusminival設置
#?symlink?the?coco?datasetcd?~/github/maskrcnn-benchmarkmkdir?-p?datasets/cocoln?-s?/path_to_coco_dataset/annotations?datasets/coco/annotationsln?-s?/path_to_coco_dataset/train2014?datasets/coco/train2014ln?-s?/path_to_coco_dataset/test2014?datasets/coco/test2014ln?-s?/path_to_coco_dataset/val2014?datasets/coco/val2014 cd?~/github/maskrcnn-benchmark mkdir?-p?datasets/coco ln?-s?/path_to_coco_dataset/annotations?datasets/coco/annotations ln?-s?/path_to_coco_dataset/train2014?datasets/coco/train2014 ln?-s?/path_to_coco_dataset/test2014?datasets/coco/test2014 ln?-s?/path_to_coco_dataset/val2014?datasets/coco/val2014你也可以自己設置數據集的路徑。那樣的話,你需要修改maskrcnn_benchmark/config/paths_catalog.py?指向你自己的數據集. 你也可以創建一個新的?paths_catalog.py?實現同樣的兩個類,然后在訓練的時候作為一個配置參數傳到?PATHS_CATALOG?中。
單GPU訓練
python?/path_to_maskrnn_benchmark/tools/train_net.py?--config-file?"/path/to/config/file.yaml""/path/to/config/file.yaml"多GPU訓練
我們使用內置的?torch.distributed.launch?實現多GPU的訓練。.這個Pytorch中的函數會創建和我們需要使用的GPU數量一樣多的進程,每個Python進程使用一個GPU。
export?NGPUS=8python?-m?torch.distributed.launch?--nproc_per_node=$NGPUS?/path_to_maskrcnn_benchmark/tools/train_net.py?--config-file?"path/to/config/file.yaml" python?-m?torch.distributed.launch?--nproc_per_node=$NGPUS?/path_to_maskrcnn_benchmark/tools/train_net.py?--config-file?"path/to/config/file.yaml"摘要
更多的我們的實現相關的摘要信息,見 ABSTRACTIONS.md.
增加你自己的數據集
這個實現增加了對COCO風格的數據集的支持。在訓練時增加新的數據集的支持需要像下面這樣做:
from?maskrcnn_benchmark.structures.bounding_box?import?BoxListclass?MyDataset(object):????def?__init__(self,?...):????????#?as?you?would?do?normally????def?__getitem__(self,?idx):????????#?load?the?image?as?a?PIL?Image????????image?=?...????????#?load?the?bounding?boxes?as?a?list?of?list?of?boxes????????#?in?this?case,?for?illustrative?purposes,?we?use????????#?x1,?y1,?x2,?y2?order.????????boxes?=?[[0,?0,?10,?10],?[10,?20,?50,?50]]????????#?and?labels????????labels?=?torch.tensor([10,?20])????????#?create?a?BoxList?from?the?boxes????????boxlist?=?BoxList(boxes,?size=image.size,?mode="xyxy")????????#?add?the?labels?to?the?boxlist????????boxlist.add_field("labels",?labels)????????if?self.transforms:????????????image,?boxlist?=?self.transforms(image,?boxlist)????????#?return?the?image,?the?boxlist?and?the?idx?in?your?dataset????????return?image,?boxlist,?idx????def?get_img_info(self,?idx):????????#?get?img_height?and?img_width.?This?is?used?if????????#?we?want?to?split?the?batches?according?to?the?aspect?ratio????????#?of?the?image,?as?it?can?be?more?efficient?than?loading?the????????#?image?from?disk????????return?{"height":?img_height,?"width":?img_width} class?MyDataset(object):def?__init__(self,?...):#?as?you?would?do?normallydef?__getitem__(self,?idx):#?load?the?image?as?a?PIL?Imageimage?=?...#?load?the?bounding?boxes?as?a?list?of?list?of?boxes#?in?this?case,?for?illustrative?purposes,?we?use#?x1,?y1,?x2,?y2?order.boxes?=?[[0,?0,?10,?10],?[10,?20,?50,?50]]#?and?labelslabels?=?torch.tensor([10,?20])#?create?a?BoxList?from?the?boxesboxlist?=?BoxList(boxes,?size=image.size,?mode="xyxy")#?add?the?labels?to?the?boxlistboxlist.add_field("labels",?labels)if?self.transforms:image,?boxlist?=?self.transforms(image,?boxlist)#?return?the?image,?the?boxlist?and?the?idx?in?your?datasetreturn?image,?boxlist,?idxdef?get_img_info(self,?idx):#?get?img_height?and?img_width.?This?is?used?if#?we?want?to?split?the?batches?according?to?the?aspect?ratio#?of?the?image,?as?it?can?be?more?efficient?than?loading?the#?image?from?diskreturn?{"height":?img_height,?"width":?img_width}內容就是這些。你也可以在boxlist中增加額外的字段,例如分割的掩模(使用structures.segmentation_mask.SegmentationMask), 或者你自己的實例類型。
完整的如何實現?COCODataset?的例子,參見?maskrcnn_benchmark/data/datasets/coco.py.
注意:
上面提到的例子是訓練用的,我們還利用cocoApi來計算測試時的準確率,測試數據集目前需要按照cocoApi的要求。
License
maskrcnn-benchmark is released under the MIT license. See LICENSE for additional details.
總結
以上是生活随笔為你收集整理的重磅资源|Pytorch1.0版本的Mask R-CNN的Facebook的官方实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华硕笔记本电脑怎么重装系统 华硕笔记本如
- 下一篇: windows7状态栏不见了怎么办 Wi