一開始覺得找到的資料都是關於GPU下配置的,可能CPU會有很多坑
結果並沒有,就清華conda源坑了我一下
#創建conda環境
#20.3.16. conda清華源怎么也弄不出來,好像是main還是free哪個包出問題了
#一開始以為是配了ss,ip有問題,ping也ping不通,學到了,ping域名不能加http這些
#換了中科大源解決了,底下安裝pytorch還是可以用清華的鏡像
conda create -n detectron2 python=3.7
#安裝pytorch, cpu
#用清華源記得刪掉 -c pytorch
#速度就是一切
pip install opencv-python(清華源
pip install cython(清華源
#這里也要下很多包,你看到哪個包下的慢,就crtl+c,然后用pip或者conda結合國內源下載
pip install 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI'
# 編譯,同上,下包慢就手動來
#網上看到有的博客里編譯的語句是啥啥setup.py,然后還要手動下載fvcore,3.16這個版本已經改變了,就按照github最新的命令來
python -m pip install -e .
#接下來看一下人家給出的demo,我一句一句理一下
Inference Demo with Pre-trained Models
#選擇model zoo里面的一個模型,實際上也都保存在configs里面了吧
#不知道啊,.yaml文件是干嘛的,pkl文件可以是模型也可以是模型參數
#去model_zoo里面找到mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl這個文件,下載到本地,可以新建個什么文件夾叫model_zoo之類的,文件夾結構可以像他提供的這樣,但是如果你下載的不是特別多,也可以用一個文件夾
#不知道r_50_fpn_3x到底是啥意思,反正都和訓練相關吧,我就先用着了
- Pick a model and its config file from model zoo, for example,
mask_rcnn_R_50_FPN_3x.yaml. - We provide
demo.pythat is able to run builtin standard models. Run it with:
cd demo/
#--input input1.jpg input2.jpg \ 制定了輸入,可以自己找一張input1.jpg放在demo文件夾下。
# [--other-options] 別的選項的占位,沒有就把這句刪了
# --opts MODEL.WEIGHTS 你存儲model_final_f10217.pkl的路徑 MODEL.DEVICE cpu
# 這一句指明了model weights的地址,你用原來的路徑是會去網上下載的,model.device cpu,一定不能少,我們就cpu。
python demo.py --config-file ../configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml \
--input input1.jpg input2.jpg \
[--other-options]
--opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl
The configs are made for training, therefore we need to specify MODEL.WEIGHTS to a model from model zoo for evaluation. This command will run the inference and show visualizations in an OpenCV window.
For details of the command line arguments, see demo.py -h or look at its source code to understand its behavior. Some common arguments are:
- To run on your webcam, replace
--input fileswith--webcam. - To run on a video, replace
--input fileswith--video-input video.mp4. - To run on cpu, add
MODEL.DEVICE cpuafter--opts. - To save outputs to a directory (for images) or a file (for webcam or video), use
--output.

#demo運行后會有這種輸出,還有控制界面啥的,無所謂了,我奇怪的一點是,怎么都沒辦法退出運行,就一直卡在那里,ctrl+c也沒有反應,奇怪。
下一篇熟悉一下detectron2的API,因為我只需要使用它訓練好的模型,所以關於訓練的部分不會涉及
我寫的應該都是關於推理部分的代碼
