# MMDetection windows install [官方github](https://github.com/open-mmlab/mmdetection) 官方沒有提供windwos的安裝方法,這邊紀錄我在windows下安裝的過程 ## 安裝、套件版本 windows 10 CUDA 10.1 pytorch 1.4.0 torchvision 0.4.1 mmcv 0.5.9 MSVC 14.16 (visual studio 2019) ~~MMDetection v2.1.0~~ MMDetection v2.2.1(終於成功了) ## 安裝步驟 1. **裝好CUDA** 2. **安装visual studio** 我是安裝2019的版本,記得安裝C++環境 將MSVC加入環境變數,我的路徑是: ``` C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.16.27023\bin\HostX64\x64 ``` 這步很重要... 注意CUDA版本與MSVC的版本是否有支援,如果版本不一樣的話 3. **創建新的python虛擬環境** ``` python -m venv venv .\venv\Scripts\activate.bat ``` 4. **安裝pytorch** 可以到這裡找到需要的版本 https://download.pytorch.org/whl/torch_stable.html 5. **安装mmcv** ``` pip install mmcv==0.6.2 ``` 不確定最新版可不可以,但mmcv-full在windows上是不行的 6. **安裝pycocotools:** ``` pip install Cython pip install "git+https://github.com/open-mmlab/cocoapi.git#subdirectory=pycocotools" ``` 7. **安裝mmdetection** 最後最重要的一步了 ``` git clone https://github.com/open-mmlab/mmdetection.git cd mmdetection git checkout v2.2.1 # 或是v2.1.0 pip install -r requirements.txt ``` 修改`setup.py` 在 def make_cuda_ext(...)裡面 ``` extra_compile_args = {'cxx': []} 改成 extra_compile_args = {'cxx': ["-DMS_WIN64","-MD"]} ``` **V2.2.1版需要修正** ./mmdetection/mmdet/ops/sigmoid_focal_loss/src/cuda/sigmoid_focal_loss_cuda.cu ``` 106行 AT_ASSERTM(targets.max().item<long>() <= (long)num_classes, 改成 AT_ASSERTM(targets.max().item<int64_t>() <= (int64_t)num_classes, ``` 開始編譯 ``` python setup.py build_ext --inplace pip install -v -e . ``` ## 測試 最好以工作管理員身分執行 我們將test.py放在mmdetection的目錄下(剛剛git clone出來的資料夾) ``` from mmdet.apis import init_detector, inference_detector import mmcv config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py' checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth' # build the model from a config file and a checkpoint file model = init_detector(config_file, checkpoint_file, device='cuda:0') # test a single image and show the results img = 'demo/demo.jpg' # or img = mmcv.imread(img), which will only load it once result = inference_detector(model, img) # visualize the results in a new window model.show_result(img, result, show=True) # or save the visualization results to image files model.show_result(img, result, out_file='result.jpg') ``` 我們要到github上下載訓練好的模型 記得在github上把master切換成v2.1.0的版本再搜尋避免出錯 https://github.com/open-mmlab/mmdetection/blob/v2.1.0/docs/model_zoo.md 這邊我是用R-50-FPN來做測試 | R-50-FPN | pytorch | 1x | 4.0 | 21.4 | 37.4 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth) | | -------- | ------- | --- | --- | ---- | ---- |:---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | 下載好後接著新增一個資料夾叫做checkpoints然後把載好的東西丟到裡面,然後執行`test.py` 然後就出錯了... ``` OSError: [WinError 145] 目錄不是空的。 ``` 到github上發現解法 https://github.com/open-mmlab/mmfashion/issues/60 修改路徑下的檔案./venv/lib/site-packages/mmcv/utils/config.py ``` with tempfile.TemporaryDirectory() as temp_config_dir: temp_config_file = tempfile.NamedTemporaryFile( dir=temp_config_dir, suffix='.py') temp_config_file.close() **新增這行** temp_config_name = osp.basename(temp_config_file.name) shutil.copyfile(filename, osp.join(temp_config_dir, temp_config_name)) ``` 再執行一次應該就可以了 我是安裝成功後才打這篇記錄的,可能有哪些地方漏掉 ## 訓練 [官方有說明](https://github.com/open-mmlab/mmdetection/blob/v2.1.0/docs/getting_started.md) ### 注意 **\*Important\***: The default learning rate in config files is for 8 GPUs and 2 img/gpu (batch size = 8*2 = 16). According to the Linear Scaling Rule, you need to set the learning rate proportional to the batch size if you use different GPUs or images per GPU, e.g., lr=0.01 for 4 GPUs * 2 img/gpu and lr=0.08 for 16 GPUs * 4 img/gpu. config 檢查要改的東西 1. _base_ = 預設模型 2. data augmentation 3. dataset settings 圖片路徑、標記路徑等等 4. num_class 5. lr_config 6. load_from 預先訓練權重