Hồ Chí Minh, 16-08-2023
Trần Nguyễn Chí Huy, Võ Duy Nguyên, UIT-Together Research Group

RTMDet

Mục Lục

Step 1. Cài đặt môi trường

Step 1.1. Tạo môi trường anaconda

Đặt tên theo cú pháp: Ten_viet tat cua ho va chu lot

VD: Tran Nguyen Chi Huy -> Huytnc

conda create --name Huytnc python=3.8 -y

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Step 1.2. Kích hoạt môi trường vừa tạo

conda activate Huytnc

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Step 1.3. Cài đặt PyTorch trên GPU platforms

conda install pytorch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1 cudatoolkit=11.3 -c pytorch

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Step 2. Cài đặt MMEngine và MMCV sử dụng MIM

pip install -U openmim mim install mmengine mim install "mmcv>=2.0.0,<2.1.0"

Hình ảnh cài đặt thành công openmim

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Hình ảnh cài đặt thành công MMEngine

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Hình ảnh cài đặt thành công MMCV

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Step 3. Thao tác với MMDetection

Truy cập vào thư mục luutru
VD: /home/u2301/luutru/

cd luutru/

Tạo thư mục tương ứng với tên môi trường bên trên

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

cd Huytnc/

Step 3.1. Cài đặt MMDetection

Tại thư mục này thực hiện clone và cài đặt mmdetection

git clone https://github.com/open-mmlab/mmdetection.git cd mmdetection pip install -v -e .

Hình ảnh clone thành công

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Step 3.2. Tạo thư mục checkpoints và tải file config

Tạo thư mục checkpoints

mkdir ./checkpoints

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Tải checkpoints của file config rtmdet_tiny_8xb32-300e_coco

mim download mmdet --config rtmdet_tiny_8xb32-300e_coco --dest ./checkpoints

Hình ảnh tải thành công file config và checkpoints

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Verify the installation

Chọn GPU
Chọn GPU bằng lệnh:

CUDA_VISIBLE_DEVICES=0

Verify the inference demo

python demo/image_demo.py demo/demo.jpg checkpoints/rtmdet_tiny_8xb32-300e_coco.py --weights checkpoints/rtmdet_tiny_8xb32-300e_coco_20220902_112414-78e30dcc.pth --device cuda:0

Kết quả được lưu trong thư mục outputs/vis
Vd: /home/u2301/luutru/Huytnc/mmdetection/outputs/vis/

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Tạo thư mục data trong thư mục mmdetection để chứa shortcut dẫn tới thư mục MS-COCO

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Sử dụng chung thư mục MS-COCO nên chúng ta sẽ tạo 1 shortcut dẫn tới thư mục MS-COCO dùng chung bằng lệnh:
ln -s /duong dan toi thu muc goc /duong dan toi thu muc luu shortcut

ln -s /home/u2301/luutru/coco/ /home/u2301/luutru/Huytnc/mmdetection/data/

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Test với dữ liệu MS-COCO

python tools/test.py \ configs/rtmdet/rtmdet_tiny_8xb32-300e_coco.py \ checkpoints/rtmdet_tiny_8xb32-300e_coco_20220902_112414-78e30dcc.pth \ --show

Nhấn tổ hợp Ctrl+C để dừng visualized. Màn hình hiện như bên dưới thì dừng thành công:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

python tools/test.py \ configs/rtmdet/rtmdet_tiny_8xb32-300e_coco.py \ checkpoints/rtmdet_tiny_8xb32-300e_coco_20220902_112414-78e30dcc.pth \ --show-dir faster_rcnn_r50_fpn_1x_results

Train with customized dataset

Chuẩn bị dataset

Ở đây mình dùng dataset visDrone đã được cài đặt sẵn, nên chỉ cần tạo 1 shortcut dẫn tới thư mục visDrone dùng chung bằng lệnh:

ln -s /home/cvpr2023/LuuTru/dataset/VisDrone/cocoVisdrone/ /home/u2301/luutru/Huytnc/mmdetection/data/

Convert sang COCO format

Vì visDrone đã có COCO format nên không cần convert.

Prepare a config

Tạo 1 file tên Prepare_Config.py


với nội dung như bên dưới:

config_drone = """ # Inherit and overwrite part of the config based on this config _base_ = './rtmdet_tiny_8xb32-300e_coco.py' data_root = '/home/cvpr2023/LuuTru/dataset/VisDrone/cocoVisdrone/' # dataset root train_batch_size_per_gpu = 4 train_num_workers = 2 max_epochs = 1 # so luong epoch can train stage2_num_epochs = 1 base_lr = 0.00008 metainfo = { 'classes': ( 'pedestrian','people','bicycle','car','van','truck','tricycle','awning-tricycle','bus','motor'), # so classes cua dataset 'palette': [ (220, 20, 60), (220, 0, 60), (220, 20, 0), (220, 20, 60), (220, 20, 60), (220, 20, 60), (220, 0, 60), (220, 20, 0), (220, 20, 60), (220, 20, 60), ] } train_dataloader = dict( batch_size=train_batch_size_per_gpu, num_workers=train_num_workers, dataset=dict( data_root=data_root, metainfo=metainfo, data_prefix=dict(img='train/'), ann_file='annotations/train.json')) val_dataloader = dict( dataset=dict( data_root=data_root, metainfo=metainfo, data_prefix=dict(img='val/'), ann_file='annotations/val.json')) test_dataloader = dict( dataset=dict( data_root=data_root, metainfo=metainfo, data_prefix=dict(img='test/'), ann_file='annotations/test.json')) val_evaluator = dict(ann_file=data_root + 'annotations/val.json') test_evaluator = dict(ann_file=data_root + 'annotations/test.json') model = dict(bbox_head=dict(num_classes=10)) # thay doi so classes phu hop # learning rate param_scheduler = [ dict( type='LinearLR', start_factor=1.0e-5, by_epoch=False, begin=0, end=10), dict( # use cosine lr from 10 to 20 epoch type='CosineAnnealingLR', eta_min=base_lr * 0.05, begin=max_epochs // 2, end=max_epochs, T_max=max_epochs // 2, by_epoch=True, convert_to_iter_based=True), ] train_pipeline_stage2 = [ dict(type='LoadImageFromFile', backend_args=None), dict(type='LoadAnnotations', with_bbox=True), dict( type='RandomResize', scale=(640, 640), ratio_range=(0.1, 2.0), keep_ratio=True), dict(type='RandomCrop', crop_size=(640, 640)), dict(type='YOLOXHSVRandomAug'), dict(type='RandomFlip', prob=0.5), dict(type='Pad', size=(640, 640), pad_val=dict(img=(114, 114, 114))), dict(type='PackDetInputs') ] # optimizer optim_wrapper = dict( _delete_=True, type='OptimWrapper', optimizer=dict(type='AdamW', lr=base_lr, weight_decay=0.05), paramwise_cfg=dict( norm_decay_mult=0, bias_decay_mult=0, bypass_duplicate=True)) default_hooks = dict( checkpoint=dict( interval=5, max_keep_ckpts=2, # only keep latest 2 checkpoints save_best='auto' ), logger=dict(type='LoggerHook', interval=5)) custom_hooks = [ dict( type='PipelineSwitchHook', switch_epoch=max_epochs - stage2_num_epochs, switch_pipeline=train_pipeline_stage2) ] # load COCO pre-trained weight load_from = './checkpoints/rtmdet_tiny_8xb32-300e_coco_20220902_112414-78e30dcc.pth' train_cfg = dict(type='EpochBasedTrainLoop', max_epochs=max_epochs, val_interval=1) """ with open('./configs/rtmdet/rtmdet_tiny_1xb4-1e_drone.py', 'w') as f: f.write(config_drone)

Sau đó chạy lệnh:

python "/home/u2301/luutru/Huytnc/mmdetection/Prepare_Config.py"

Bắt đầu train với lệnh:

python tools/train.py configs/rtmdet/rtmdet_tiny_1xb4-1e_drone.py

Sau khi train xong, chúng ta có thể tính toán điểm mAP từng class bằng cách chạy câu lệnh:

./tools/dist_test.sh \ configs/rtmdet/rtmdet_tiny_1xb4-1e_drone.py\ work_dirs/rtmdet_tiny_1xb4-1e_drone/best_coco_bbox_mAP_epoch_1.pth \ 1 \ --out results.pkl \ --cfg-options test_evaluator.classwise=True

Bên dưới là điểm mAP từng class.

Tiếp theo sẽ đến với phần visualize kết quả.

Visualize kết quả sau khi train

Visualize nhiều ảnh:

Chạy câu lệnh:

python tools/test.py \ configs/rtmdet/rtmdet_tiny_1xb4-1e_drone.py \ work_dirs/rtmdet_tiny_1xb4-1e_drone/best_coco_bbox_mAP_epoch_1.pth \ --show-dir rtmdet_tiny_1xb4-1e_drone_results

Sau khi chạy, thư mục các ảnh kết quả sẽ nằm trong thư mục work_dirs.

Visualize 1 ảnh:

Tạo file Demo_Detec.py

với nội dung:

from mmdet.apis import DetInferencer import glob # Change to use a config config = '/home/u2301/luutru/Huytnc/mmdetection/configs/rtmdet/rtmdet_tiny_1xb4-1e_drone.py' # Setup a checkpoint file to load checkpoint = glob.glob('/home/u2301/luutru/Huytnc/mmdetection/work_dirs/rtmdet_tiny_1xb4-1e_drone/best_coco_bbox_mAP_epoch_1.pth')[0] # Set the device to be used for evaluation device = 'cuda:0' # Initialize the DetInferencer inferencer = DetInferencer(config, checkpoint, device) # Co the dung img khac trong thu muc test img = '/home/cvpr2023/LuuTru/dataset/VisDrone/cocoVisdrone/test/0000074_08202_d_0000016.jpg' result = inferencer(img, out_dir='./outputs')

Chạy lệnh dưới để visualize:

python "/home/u2301/luutru/Huytnc/mmdetection/Demo_Detec.py"

Kết quả sau khi chạy sẽ được lưu trong thư mục outputs.

Tài liệu hướng dẫn dùng cho nhóm UIT-Together Research Group