딥러닝

[Colab] MMdetection Custom Data 학습

승무_ 2022. 9. 10. 00:30

Install MMdetection 

from google.colab import drive
drive.mount('/content/drive')
# Check nvcc version
!nvcc -V
# Check GCC version
!gcc --version
# install dependencies: (use cu111 because colab has CUDA 11.1)
!pip install torch==1.9.0+cu111 torchvision==0.10.0+cu111 -f https://download.pytorch.org/whl/torch_stable.html

# install mmcv-full thus we could use CUDA operators
!pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu111/torch1.9.0/index.html

# Install mmdetection
!rm -rf mmdetection
!git clone https://github.com/open-mmlab/mmdetection.git
%cd mmdetection

!pip install -e .
from mmcv import collect_env
collect_env()
# Check Pytorch installation
import torch, torchvision
print(torch.__version__, torch.cuda.is_available())

# Check MMDetection installation
import mmdet
print(mmdet.__version__)

# Check mmcv installation
from mmcv.ops import get_compiling_cuda_version, get_compiler_version
print(get_compiling_cuda_version())
print(get_compiler_version())

1.mmdet->datasets->coco.py 변경

클래스 추가

 

2.cofing->_base_->default_runtime.py 변경

 

config->_base_->datasets->coco_detection.py내용을 default_runtime.py에 복사

config->faster_rcnn_r50_fpn.py내용을 default_runtime.py에 복사

cofing->_base_->schedule->schedule_1x.py내용을 default_runtime.py에 복사

 

3.cofing->_base_->default_runtime.py 수정

data_root 변경

바로밑에 CLASSES =() 추가

#이 부분 수정
data = dict(
    samples_per_gpu=2,
    workers_per_gpu=2,
    train=dict(
        type=dataset_type,
        ann_file=data_root + 'annotations/instances_train2017.json',
        img_prefix=data_root + 'train2017/',
        pipeline=train_pipeline),
    val=dict(
        type=dataset_type,
        ann_file=data_root + 'annotations/instances_val2017.json',
        img_prefix=data_root + 'val2017/',
        pipeline=test_pipeline),
    test=dict(
        type=dataset_type,
        ann_file=data_root + 'annotations/instances_val2017.json',
        img_prefix=data_root + 'val2017/',
        pipeline=test_pipeline))
evaluation = dict(interval=1, metric='bbox')
num_classes=  '클래스 수' 로 수정
load_from = "https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth"  추가

optimizer = dict(type='SGD', lr=0.02, momentum=0.9, weight_decay=0.0001)

->lr을 0.0025로 수정

 

runner = dict(type='EpochBasedRunner', max_epochs=12)

->epoch수정

 

4.train

!python tools/train.py configs/_base_/default_runtime.py

 

5.test

!python tools/test.py configs/_base_/default_runtime.py /content/mmdetection/work_dirs/default_runtime/epoch_12.pth --show-dir work_dirs/result/

 

6.결과 저장

!zip -r /content/sample_data/file.zip /content/mmdetection/work_dirs/result
from google.colab import files
files.download("/content/sample_data/file.zip")

 

'딥러닝' 카테고리의 다른 글

convert_to_coco  (0) 2022.09.14
[Colab] unzip  (0) 2022.09.14
COCO json file 병합  (0) 2022.09.08
YOLOv5 검출 결과 Crop  (0) 2022.09.08
DETR 코드  (1) 2022.09.02