环境配置
#数据集在谷歌云盘上,挂载谷歌云盘
#colab中已经有pytorch环境,无需专门配置pytorch
from google.colab import drive
drive.mount('/content/drive')
%cd /content/drive/MyDrive
# 下载MMDetection,下载后注释掉
!git clone https://github.com/open-mmlab/mmdetection.git
# 查看torch的版本
import torch
print(torch.__version__)
# 卸载原有的pytorch
!pip uninstall torch torchvision -y
# 查看cuda版本,colab会更新,得根据当前的版本,安装上相匹配的pytorch版本
!nvcc --version
# 线上安装新的pytorch
!pip install torch torchvision
!pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu118/torch1.11.0/index.html
!pip install ipykernel
!pip install mmpycocotools
%cd /
!cp -a /content/gdrive/MyDrive/mmdetection /
%cd /content/drive/MyDrive/mmdetection
!pip install -r requirements/build.txt
!pip install -v -e . # or "python setup.py develop"
# 先删除原有的数据集,再解压柑橘数据集到本地路径
!rm -rf /orange_resized
!unzip /content/drive/MyDrive/datasets.zip -d /
# Cascade R-CNN下载权重,权重在mmdetection中去找
# 在coco数据集上已经训练的很好了,加载预训练权重能够帮助我们更快的训练模型
!wget -P checkpoints https://download.openmmlab.com/mmdetection/v2.0/cascade_rcnn/cascade_rcnn_r101_fpn_1x_coco/cascade_rcnn_r101_fpn_1x_coco_20200317-0b6a2fbf.pth
# Cascade R-CNN模型训练
!python tools/train.py configs/self_model/cascade_rcnn_r101_fpn_1x_orange.py
训练结果图
# Spares R-CNN和Cascade R-CNN对比
#!python tools/analysis_tools/analyze_logs.py plot_curve work_dirs/sparse_rcnn_r101_fpn_mstrain_480-800_3x_orange/20230111_040201.log.json work_dirs/cascade_rcnn_r101_fpn_1x_orange/20230113_024503 (1).log.json --key bbox_mAP --legend Cascade_RCNN Sparse_RCNN --title mAP --out mAP.pdf
# cascade_rcnn和tood的对比图
!python /content/drive/MyDrive/mmdetection/tools/analysis_tools/analyze_logs.py plot_curve /content/drive/MyDrive/mmdetection/work_dirs/cascade_rcnn_r101_fpn_1x_orange/20230113_024503.log.json /content/drive/MyDrive/mmdetection/work_dirs/tood_r50_fpn_anchor_based_1x_orange/20230116_030156.log.json --keys bbox_mAP --legend Cascade_RCNN TOOD --title mAP --out mAP.pdf