공부 기록
Object Detection(0929_day3) - kitti tiny dataset 본문
In [1]:
!pip install mmcv-full
Collecting mmcv-full Downloading mmcv-full-1.3.14.tar.gz (324 kB) |████████████████████████████████| 324 kB 5.1 MB/s Collecting addict Downloading addict-2.4.0-py3-none-any.whl (3.8 kB) Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (1.19.5) Requirement already satisfied: packaging in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (21.0) Requirement already satisfied: Pillow in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (7.1.2) Requirement already satisfied: pyyaml in /usr/local/lib/python3.7/dist-packages (from mmcv-full) (3.13) Collecting yapf Downloading yapf-0.31.0-py2.py3-none-any.whl (185 kB) |████████████████████████████████| 185 kB 46.4 MB/s Requirement already satisfied: pyparsing>=2.0.2 in /usr/local/lib/python3.7/dist-packages (from packaging->mmcv-full) (2.4.7) Building wheels for collected packages: mmcv-full Building wheel for mmcv-full (setup.py) ... done Created wheel for mmcv-full: filename=mmcv_full-1.3.14-cp37-cp37m-linux_x86_64.whl size=31467569 sha256=16b99f9261f0668705d262f46441989c774623b628c22eb8b69c8efdf7dfb13d Stored in directory: /root/.cache/pip/wheels/5e/54/62/69c99dc3c9937bca64126f81cbe315ae6c8e6e98c43fa7392d Successfully built mmcv-full Installing collected packages: yapf, addict, mmcv-full Successfully installed addict-2.4.0 mmcv-full-1.3.14 yapf-0.31.0
In [2]:
!git clone https://github.com/open-mmlab/mmdetection.git
Cloning into 'mmdetection'... remote: Enumerating objects: 21088, done. remote: Counting objects: 100% (1/1), done. remote: Total 21088 (delta 0), reused 0 (delta 0), pack-reused 21087 Receiving objects: 100% (21088/21088), 24.81 MiB | 23.11 MiB/s, done. Resolving deltas: 100% (14747/14747), done.
In [3]:
%cd mmdetection/
/content/mmdetection
In [ ]:
!python setup.py install
In [5]:
mkdir checkpoints
In [7]:
!wget -O /content/mmdetection/checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
--2021-09-29 06:02:35-- https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth Resolving download.openmmlab.com (download.openmmlab.com)... 47.252.96.35 Connecting to download.openmmlab.com (download.openmmlab.com)|47.252.96.35|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 167287506 (160M) [application/octet-stream] Saving to: ‘/content/mmdetection/checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth’ /content/mmdetectio 100%[===================>] 159.54M 8.44MB/s in 20s 2021-09-29 06:02:56 (7.86 MB/s) - ‘/content/mmdetection/checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth’ saved [167287506/167287506]
In [1]:
from mmdet.apis import init_detector, inference_detector, show_result_pyplot
In [2]:
config_file = "/content/mmdetection/configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py"
checkpoint_file = "/content/mmdetection/checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth"
In [3]:
# download, decompress the data
!wget https://download.openmmlab.com/mmdetection/data/kitti_tiny.zip
!unzip kitti_tiny.zip > /dev/null
--2021-09-29 06:03:56-- https://download.openmmlab.com/mmdetection/data/kitti_tiny.zip Resolving download.openmmlab.com (download.openmmlab.com)... 47.252.96.35 Connecting to download.openmmlab.com (download.openmmlab.com)|47.252.96.35|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 6918271 (6.6M) [application/zip] Saving to: ‘kitti_tiny.zip’ kitti_tiny.zip 100%[===================>] 6.60M 3.14MB/s in 2.1s 2021-09-29 06:03:59 (3.14 MB/s) - ‘kitti_tiny.zip’ saved [6918271/6918271]
In [ ]:
CLASSES = ('Car', 'Van', 'Truck', 'Pedestrian', 'Person_sitting', 'Cyclist', 'Tram', 'Misc', 'DontCare')
In [3]:
import copy
import os.path as osp
import mmcv
import numpy as np
from mmdet.datasets.builder import DATASETS
from mmdet.datasets.custom import CustomDataset
@DATASETS.register_module(force=True)
class KittiTinyDataset(CustomDataset):
CLASSES = ('Car', 'Pedestrian', 'Cyclist')
### self.ann_file : /content/kitti_tiny/train.txt
### self.img_prefix : /content/kitti_tiny/training/image_2
### ann_file : /content/kitti_tiny/train.txt
def load_annotations(self, ann_file):
cat2label = {k: i for i, k in enumerate(self.CLASSES)}
# load image list from file
image_list = mmcv.list_from_file(self.ann_file)
data_infos = []
# convert annotations to middle format
for image_id in image_list:
filename = f'{self.img_prefix}/{image_id}.jpeg'
image = mmcv.imread(filename)
height, width = image.shape[:2]
data_info = dict(filename=f'{image_id}.jpeg', width=width, height=height)
# load annotations
label_prefix = self.img_prefix.replace('image_2', 'label_2')
lines = mmcv.list_from_file(osp.join(label_prefix, f'{image_id}.txt'))
content = [line.strip().split(' ') for line in lines]
bbox_names = [x[0] for x in content]
bboxes = [[float(info) for info in x[4:8]] for x in content]
gt_bboxes = []
gt_labels = []
gt_bboxes_ignore = []
gt_labels_ignore = []
# filter 'DontCare'
for bbox_name, bbox in zip(bbox_names, bboxes):
if bbox_name in cat2label:
gt_labels.append(cat2label[bbox_name])
gt_bboxes.append(bbox)
else:
gt_labels_ignore.append(-1)
gt_bboxes_ignore.append(bbox)
data_anno = dict(
bboxes=np.array(gt_bboxes, dtype=np.float32).reshape(-1, 4),
labels=np.array(gt_labels, dtype=np.long),
bboxes_ignore=np.array(gt_bboxes_ignore,
dtype=np.float32).reshape(-1, 4),
labels_ignore=np.array(gt_labels_ignore, dtype=np.long))
data_info.update(ann=data_anno)
data_infos.append(data_info)
return data_infos
In [4]:
!ls -la /content/mmdetection/checkpoints
total 163376 drwxr-xr-x 2 root root 4096 Sep 29 06:02 . drwxr-xr-x 21 root root 4096 Sep 29 07:04 .. -rw-r--r-- 1 root root 167287506 Aug 28 2020 faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
In [5]:
from mmcv import Config
cfg = Config.fromfile(config_file)
In [6]:
print(cfg.pretty_text)
model = dict( type='FasterRCNN', backbone=dict( type='ResNet', depth=50, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch', init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')), neck=dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, num_outs=5), rpn_head=dict( type='RPNHead', in_channels=256, feat_channels=256, anchor_generator=dict( type='AnchorGenerator', scales=[8], ratios=[0.5, 1.0, 2.0], strides=[4, 8, 16, 32, 64]), bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[0.0, 0.0, 0.0, 0.0], target_stds=[1.0, 1.0, 1.0, 1.0]), loss_cls=dict( type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0), loss_bbox=dict(type='L1Loss', loss_weight=1.0)), roi_head=dict( type='StandardRoIHead', bbox_roi_extractor=dict( type='SingleRoIExtractor', roi_layer=dict(type='RoIAlign', output_size=7, sampling_ratio=0), out_channels=256, featmap_strides=[4, 8, 16, 32]), bbox_head=dict( type='Shared2FCBBoxHead', in_channels=256, fc_out_channels=1024, roi_feat_size=7, num_classes=80, bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[0.0, 0.0, 0.0, 0.0], target_stds=[0.1, 0.1, 0.2, 0.2]), reg_class_agnostic=False, loss_cls=dict( type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0), loss_bbox=dict(type='L1Loss', loss_weight=1.0))), train_cfg=dict( rpn=dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.7, neg_iou_thr=0.3, min_pos_iou=0.3, match_low_quality=True, ignore_iof_thr=-1), sampler=dict( type='RandomSampler', num=256, pos_fraction=0.5, neg_pos_ub=-1, add_gt_as_proposals=False), allowed_border=-1, pos_weight=-1, debug=False), rpn_proposal=dict( nms_pre=2000, max_per_img=1000, nms=dict(type='nms', iou_threshold=0.7), min_bbox_size=0), rcnn=dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.5, min_pos_iou=0.5, match_low_quality=False, ignore_iof_thr=-1), sampler=dict( type='RandomSampler', num=512, pos_fraction=0.25, neg_pos_ub=-1, add_gt_as_proposals=True), pos_weight=-1, debug=False)), test_cfg=dict( rpn=dict( nms_pre=1000, max_per_img=1000, nms=dict(type='nms', iou_threshold=0.7), min_bbox_size=0), rcnn=dict( score_thr=0.05, nms=dict(type='nms', iou_threshold=0.5), max_per_img=100))) dataset_type = 'CocoDataset' data_root = 'data/coco/' img_norm_cfg = dict( mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True), dict(type='Resize', img_scale=(1333, 800), keep_ratio=True), dict(type='RandomFlip', flip_ratio=0.5), dict( type='Normalize', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True), dict(type='Pad', size_divisor=32), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']) ] test_pipeline = [ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(1333, 800), flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict(type='RandomFlip'), dict( type='Normalize', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True), dict(type='Pad', size_divisor=32), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']) ]) ] data = dict( samples_per_gpu=2, workers_per_gpu=2, train=dict( type='CocoDataset', ann_file='data/coco/annotations/instances_train2017.json', img_prefix='data/coco/train2017/', pipeline=[ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True), dict(type='Resize', img_scale=(1333, 800), keep_ratio=True), dict(type='RandomFlip', flip_ratio=0.5), dict( type='Normalize', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True), dict(type='Pad', size_divisor=32), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']) ]), val=dict( type='CocoDataset', ann_file='data/coco/annotations/instances_val2017.json', img_prefix='data/coco/val2017/', pipeline=[ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(1333, 800), flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict(type='RandomFlip'), dict( type='Normalize', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True), dict(type='Pad', size_divisor=32), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']) ]) ]), test=dict( type='CocoDataset', ann_file='data/coco/annotations/instances_val2017.json', img_prefix='data/coco/val2017/', pipeline=[ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(1333, 800), flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict(type='RandomFlip'), dict( type='Normalize', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True), dict(type='Pad', size_divisor=32), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']) ]) ])) evaluation = dict(interval=1, metric='bbox') optimizer = dict(type='SGD', lr=0.02, momentum=0.9, weight_decay=0.0001) optimizer_config = dict(grad_clip=None) lr_config = dict( policy='step', warmup='linear', warmup_iters=500, warmup_ratio=0.001, step=[8, 11]) runner = dict(type='EpochBasedRunner', max_epochs=12) checkpoint_config = dict(interval=1) log_config = dict(interval=50, hooks=[dict(type='TextLoggerHook')]) custom_hooks = [dict(type='NumClassCheckHook')] dist_params = dict(backend='nccl') log_level = 'INFO' load_from = None resume_from = None workflow = [('train', 1)]
In [8]:
from mmdet.apis import set_random_seed
# Modify dataset type and path
cfg.dataset_type = 'KittiTinyDataset'
cfg.data_root = '/content/kitti_tiny/'
cfg.data.test.type = 'KittiTinyDataset'
cfg.data.test.data_root = '/content/kitti_tiny/'
cfg.data.test.ann_file = 'train.txt'
cfg.data.test.img_prefix = 'training/image_2'
cfg.data.train.type = 'KittiTinyDataset'
cfg.data.train.data_root = '/content/kitti_tiny/'
cfg.data.train.ann_file = 'train.txt'
cfg.data.train.img_prefix = 'training/image_2'
cfg.data.val.type = 'KittiTinyDataset'
cfg.data.val.data_root = '/content/kitti_tiny/'
cfg.data.val.ann_file = 'val.txt'
cfg.data.val.img_prefix = 'training/image_2'
# modify num classes of the model in box head
cfg.model.roi_head.bbox_head.num_classes = 3
# We can still use the pre-trained Mask RCNN model though we do not need to
# use the mask branch
cfg.load_from = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
# Set up working dir to save files and logs.
cfg.work_dir = './tutorial_exps'
# The original learning rate (LR) is set for 8-GPU training.
# We divide it by 8 since we only use one GPU.
cfg.optimizer.lr = 0.02 / 8
cfg.lr_config.warmup = None
cfg.log_config.interval = 10
cfg.lr_config.policy = 'step'
# Change the evaluation metric since we use customized dataset.
cfg.evaluation.metric = 'mAP'
# We can set the evaluation interval to reduce the evaluation times
cfg.evaluation.interval = 12
# We can set the checkpoint saving interval to reduce the storage cost
cfg.checkpoint_config.interval = 12
# Set seed thus the results are more reproducible
cfg.seed = 0
set_random_seed(0, deterministic=False)
cfg.gpu_ids = range(1)
# We can initialize the logger for training and have a look
# at the final config used for training
print(f'Config:\n{cfg.pretty_text}')
Config: model = dict( type='FasterRCNN', backbone=dict( type='ResNet', depth=50, num_stages=4, out_indices=(0, 1, 2, 3), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), norm_eval=True, style='pytorch', init_cfg=dict(type='Pretrained', checkpoint='torchvision://resnet50')), neck=dict( type='FPN', in_channels=[256, 512, 1024, 2048], out_channels=256, num_outs=5), rpn_head=dict( type='RPNHead', in_channels=256, feat_channels=256, anchor_generator=dict( type='AnchorGenerator', scales=[8], ratios=[0.5, 1.0, 2.0], strides=[4, 8, 16, 32, 64]), bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[0.0, 0.0, 0.0, 0.0], target_stds=[1.0, 1.0, 1.0, 1.0]), loss_cls=dict( type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0), loss_bbox=dict(type='L1Loss', loss_weight=1.0)), roi_head=dict( type='StandardRoIHead', bbox_roi_extractor=dict( type='SingleRoIExtractor', roi_layer=dict(type='RoIAlign', output_size=7, sampling_ratio=0), out_channels=256, featmap_strides=[4, 8, 16, 32]), bbox_head=dict( type='Shared2FCBBoxHead', in_channels=256, fc_out_channels=1024, roi_feat_size=7, num_classes=3, bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[0.0, 0.0, 0.0, 0.0], target_stds=[0.1, 0.1, 0.2, 0.2]), reg_class_agnostic=False, loss_cls=dict( type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0), loss_bbox=dict(type='L1Loss', loss_weight=1.0))), train_cfg=dict( rpn=dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.7, neg_iou_thr=0.3, min_pos_iou=0.3, match_low_quality=True, ignore_iof_thr=-1), sampler=dict( type='RandomSampler', num=256, pos_fraction=0.5, neg_pos_ub=-1, add_gt_as_proposals=False), allowed_border=-1, pos_weight=-1, debug=False), rpn_proposal=dict( nms_pre=2000, max_per_img=1000, nms=dict(type='nms', iou_threshold=0.7), min_bbox_size=0), rcnn=dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.5, min_pos_iou=0.5, match_low_quality=False, ignore_iof_thr=-1), sampler=dict( type='RandomSampler', num=512, pos_fraction=0.25, neg_pos_ub=-1, add_gt_as_proposals=True), pos_weight=-1, debug=False)), test_cfg=dict( rpn=dict( nms_pre=1000, max_per_img=1000, nms=dict(type='nms', iou_threshold=0.7), min_bbox_size=0), rcnn=dict( score_thr=0.05, nms=dict(type='nms', iou_threshold=0.5), max_per_img=100))) dataset_type = 'KittiTinyDataset' data_root = '/content/kitti_tiny/' img_norm_cfg = dict( mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True) train_pipeline = [ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True), dict(type='Resize', img_scale=(1333, 800), keep_ratio=True), dict(type='RandomFlip', flip_ratio=0.5), dict( type='Normalize', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True), dict(type='Pad', size_divisor=32), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']) ] test_pipeline = [ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(1333, 800), flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict(type='RandomFlip'), dict( type='Normalize', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True), dict(type='Pad', size_divisor=32), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']) ]) ] data = dict( samples_per_gpu=2, workers_per_gpu=2, train=dict( type='KittiTinyDataset', ann_file='train.txt', img_prefix='training/image_2', pipeline=[ dict(type='LoadImageFromFile'), dict(type='LoadAnnotations', with_bbox=True), dict(type='Resize', img_scale=(1333, 800), keep_ratio=True), dict(type='RandomFlip', flip_ratio=0.5), dict( type='Normalize', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True), dict(type='Pad', size_divisor=32), dict(type='DefaultFormatBundle'), dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels']) ], data_root='/content/kitti_tiny/'), val=dict( type='KittiTinyDataset', ann_file='val.txt', img_prefix='training/image_2', pipeline=[ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(1333, 800), flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict(type='RandomFlip'), dict( type='Normalize', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True), dict(type='Pad', size_divisor=32), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']) ]) ], data_root='/content/kitti_tiny/'), test=dict( type='KittiTinyDataset', ann_file='train.txt', img_prefix='training/image_2', pipeline=[ dict(type='LoadImageFromFile'), dict( type='MultiScaleFlipAug', img_scale=(1333, 800), flip=False, transforms=[ dict(type='Resize', keep_ratio=True), dict(type='RandomFlip'), dict( type='Normalize', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], to_rgb=True), dict(type='Pad', size_divisor=32), dict(type='ImageToTensor', keys=['img']), dict(type='Collect', keys=['img']) ]) ], data_root='/content/kitti_tiny/')) evaluation = dict(interval=12, metric='mAP') optimizer = dict(type='SGD', lr=0.0025, momentum=0.9, weight_decay=0.0001) optimizer_config = dict(grad_clip=None) lr_config = dict( policy='step', warmup=None, warmup_iters=500, warmup_ratio=0.001, step=[8, 11]) runner = dict(type='EpochBasedRunner', max_epochs=12) checkpoint_config = dict(interval=12) log_config = dict(interval=10, hooks=[dict(type='TextLoggerHook')]) custom_hooks = [dict(type='NumClassCheckHook')] dist_params = dict(backend='nccl') log_level = 'INFO' load_from = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth' resume_from = None workflow = [('train', 1)] work_dir = './tutorial_exps' seed = 0 gpu_ids = range(0, 1)
In [9]:
from mmdet.datasets import build_dataset
from mmdet.models import build_detector
from mmdet.apis import train_detector
In [10]:
# train용 데이터 셋 생성
datasets = [build_dataset(cfg.data.train)]
/usr/local/lib/python3.7/dist-packages/mmdet-2.17.0-py3.7.egg/mmdet/datasets/custom.py:157: UserWarning: CustomDataset does not support filtering empty gt images. 'CustomDataset does not support filtering empty gt images.')
In [11]:
datasets
Out[11]:
[ KittiTinyDataset Train dataset with number of images 50, and instance counts: +----------+-------+----------------+-------+-------------+-------+----------+-------+----------+-------+ | category | count | category | count | category | count | category | count | category | count | +----------+-------+----------------+-------+-------------+-------+----------+-------+----------+-------+ | | | | | | | | | | | | 0 [Car] | 147 | 1 [Pedestrian] | 23 | 2 [Cyclist] | 7 | | | | | +----------+-------+----------------+-------+-------------+-------+----------+-------+----------+-------+]
In [12]:
datasets[0].CLASSES
Out[12]:
('Car', 'Pedestrian', 'Cyclist')
In [13]:
model = build_detector(cfg.model, train_cfg=cfg.get('train_cfg'), test_cfg=cfg.get('test_cfg'))
model.CLASSES = datasets[0].CLASSES
/usr/local/lib/python3.7/dist-packages/mmdet-2.17.0-py3.7.egg/mmdet/core/anchor/builder.py:17: UserWarning: ``build_anchor_generator`` would be deprecated soon, please use ``build_prior_generator`` '``build_anchor_generator`` would be deprecated soon, please use '
In [14]:
!ls -la
total 6792 drwxr-xr-x 1 root root 4096 Sep 29 07:04 . drwxr-xr-x 1 root root 4096 Sep 29 05:36 .. drwxr-xr-x 4 root root 4096 Sep 16 13:39 .config drwxr-xr-x 2 root root 4096 Sep 29 06:02 .ipynb_checkpoints drwxrwxr-x 3 root root 4096 Jul 5 2020 kitti_tiny -rw-r--r-- 1 root root 6918271 Dec 24 2020 kitti_tiny.zip drwxr-xr-x 21 root root 4096 Sep 29 07:04 mmdetection drwxr-xr-x 1 root root 4096 Sep 16 13:40 sample_data drwxr-xr-x 2 root root 4096 Sep 29 06:58 tutorial_exps
In [15]:
%cd ./mmdetection
/content/mmdetection
In [16]:
mmcv.mkdir_or_exist(osp.abspath(cfg.work_dir))
train_detector(model, datasets, cfg, distributed=False, validate=True)
2021-09-29 07:21:51,935 - mmdet - INFO - load checkpoint from checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth 2021-09-29 07:21:51,937 - mmdet - INFO - Use load_from_local loader 2021-09-29 07:21:52,096 - mmdet - WARNING - The model and loaded state dict do not match exactly size mismatch for roi_head.bbox_head.fc_cls.weight: copying a param with shape torch.Size([81, 1024]) from checkpoint, the shape in current model is torch.Size([4, 1024]). size mismatch for roi_head.bbox_head.fc_cls.bias: copying a param with shape torch.Size([81]) from checkpoint, the shape in current model is torch.Size([4]). size mismatch for roi_head.bbox_head.fc_reg.weight: copying a param with shape torch.Size([320, 1024]) from checkpoint, the shape in current model is torch.Size([12, 1024]). size mismatch for roi_head.bbox_head.fc_reg.bias: copying a param with shape torch.Size([320]) from checkpoint, the shape in current model is torch.Size([12]). 2021-09-29 07:21:52,105 - mmdet - INFO - Start running, host: root@5569cea10591, work_dir: /content/mmdetection/tutorial_exps 2021-09-29 07:21:52,108 - mmdet - INFO - Hooks will be executed in the following order: before_run: (VERY_HIGH ) StepLrUpdaterHook (NORMAL ) CheckpointHook (LOW ) EvalHook (VERY_LOW ) TextLoggerHook -------------------- before_train_epoch: (VERY_HIGH ) StepLrUpdaterHook (NORMAL ) NumClassCheckHook (LOW ) IterTimerHook (LOW ) EvalHook (VERY_LOW ) TextLoggerHook -------------------- before_train_iter: (VERY_HIGH ) StepLrUpdaterHook (LOW ) IterTimerHook (LOW ) EvalHook -------------------- after_train_iter: (ABOVE_NORMAL) OptimizerHook (NORMAL ) CheckpointHook (LOW ) IterTimerHook (LOW ) EvalHook (VERY_LOW ) TextLoggerHook -------------------- after_train_epoch: (NORMAL ) CheckpointHook (LOW ) EvalHook (VERY_LOW ) TextLoggerHook -------------------- before_val_epoch: (NORMAL ) NumClassCheckHook (LOW ) IterTimerHook (VERY_LOW ) TextLoggerHook -------------------- before_val_iter: (LOW ) IterTimerHook -------------------- after_val_iter: (LOW ) IterTimerHook -------------------- after_val_epoch: (VERY_LOW ) TextLoggerHook -------------------- 2021-09-29 07:21:52,110 - mmdet - INFO - workflow: [('train', 1)], max: 12 epochs /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at /pytorch/c10/core/TensorImpl.h:1156.) return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode) /usr/local/lib/python3.7/dist-packages/mmdet-2.17.0-py3.7.egg/mmdet/core/anchor/anchor_generator.py:324: UserWarning: ``grid_anchors`` would be deprecated soon. Please use ``grid_priors`` warnings.warn('``grid_anchors`` would be deprecated soon. ' /usr/local/lib/python3.7/dist-packages/mmdet-2.17.0-py3.7.egg/mmdet/core/anchor/anchor_generator.py:361: UserWarning: ``single_level_grid_anchors`` would be deprecated soon. Please use ``single_level_grid_priors`` '``single_level_grid_anchors`` would be deprecated soon. ' 2021-09-29 07:22:04,607 - mmdet - INFO - Epoch [1][10/25] lr: 2.500e-03, eta: 0:05:54, time: 1.223, data_time: 0.231, memory: 2228, loss_rpn_cls: 0.0316, loss_rpn_bbox: 0.0180, loss_cls: 0.5672, acc: 84.0332, loss_bbox: 0.4016, loss: 1.0183 2021-09-29 07:22:14,691 - mmdet - INFO - Epoch [1][20/25] lr: 2.500e-03, eta: 0:05:12, time: 1.009, data_time: 0.035, memory: 2228, loss_rpn_cls: 0.0251, loss_rpn_bbox: 0.0127, loss_cls: 0.1958, acc: 93.8086, loss_bbox: 0.3179, loss: 0.5515 2021-09-29 07:22:31,830 - mmdet - INFO - Epoch [2][10/25] lr: 2.500e-03, eta: 0:04:19, time: 1.201, data_time: 0.226, memory: 2228, loss_rpn_cls: 0.0158, loss_rpn_bbox: 0.0154, loss_cls: 0.1680, acc: 94.5996, loss_bbox: 0.2792, loss: 0.4784 2021-09-29 07:22:41,932 - mmdet - INFO - Epoch [2][20/25] lr: 2.500e-03, eta: 0:04:11, time: 1.010, data_time: 0.034, memory: 2228, loss_rpn_cls: 0.0118, loss_rpn_bbox: 0.0123, loss_cls: 0.1445, acc: 94.5898, loss_bbox: 0.2191, loss: 0.3877 2021-09-29 07:22:58,963 - mmdet - INFO - Epoch [3][10/25] lr: 2.500e-03, eta: 0:03:45, time: 1.196, data_time: 0.229, memory: 2228, loss_rpn_cls: 0.0069, loss_rpn_bbox: 0.0113, loss_cls: 0.0948, acc: 96.6016, loss_bbox: 0.1626, loss: 0.2756 2021-09-29 07:23:09,108 - mmdet - INFO - Epoch [3][20/25] lr: 2.500e-03, eta: 0:03:38, time: 1.014, data_time: 0.035, memory: 2228, loss_rpn_cls: 0.0059, loss_rpn_bbox: 0.0120, loss_cls: 0.1547, acc: 94.0527, loss_bbox: 0.2603, loss: 0.4329 2021-09-29 07:23:26,416 - mmdet - INFO - Epoch [4][10/25] lr: 2.500e-03, eta: 0:03:18, time: 1.212, data_time: 0.228, memory: 2228, loss_rpn_cls: 0.0105, loss_rpn_bbox: 0.0148, loss_cls: 0.1285, acc: 94.8438, loss_bbox: 0.2240, loss: 0.3778 2021-09-29 07:23:36,557 - mmdet - INFO - Epoch [4][20/25] lr: 2.500e-03, eta: 0:03:11, time: 1.014, data_time: 0.034, memory: 2228, loss_rpn_cls: 0.0047, loss_rpn_bbox: 0.0125, loss_cls: 0.1225, acc: 95.3418, loss_bbox: 0.1966, loss: 0.3362 2021-09-29 07:23:53,716 - mmdet - INFO - Epoch [5][10/25] lr: 2.500e-03, eta: 0:02:54, time: 1.210, data_time: 0.227, memory: 2228, loss_rpn_cls: 0.0050, loss_rpn_bbox: 0.0098, loss_cls: 0.1009, acc: 96.0938, loss_bbox: 0.1891, loss: 0.3048 2021-09-29 07:24:03,829 - mmdet - INFO - Epoch [5][20/25] lr: 2.500e-03, eta: 0:02:46, time: 1.011, data_time: 0.033, memory: 2228, loss_rpn_cls: 0.0068, loss_rpn_bbox: 0.0115, loss_cls: 0.1086, acc: 95.9766, loss_bbox: 0.1743, loss: 0.3012 2021-09-29 07:24:21,021 - mmdet - INFO - Epoch [6][10/25] lr: 2.500e-03, eta: 0:02:30, time: 1.205, data_time: 0.228, memory: 2228, loss_rpn_cls: 0.0036, loss_rpn_bbox: 0.0094, loss_cls: 0.0829, acc: 96.8457, loss_bbox: 0.1575, loss: 0.2534 2021-09-29 07:24:31,216 - mmdet - INFO - Epoch [6][20/25] lr: 2.500e-03, eta: 0:02:22, time: 1.020, data_time: 0.035, memory: 2228, loss_rpn_cls: 0.0040, loss_rpn_bbox: 0.0101, loss_cls: 0.0916, acc: 96.4844, loss_bbox: 0.1720, loss: 0.2777 2021-09-29 07:24:48,396 - mmdet - INFO - Epoch [7][10/25] lr: 2.500e-03, eta: 0:02:07, time: 1.210, data_time: 0.229, memory: 2228, loss_rpn_cls: 0.0034, loss_rpn_bbox: 0.0098, loss_cls: 0.0832, acc: 96.9824, loss_bbox: 0.1548, loss: 0.2513 2021-09-29 07:24:58,576 - mmdet - INFO - Epoch [7][20/25] lr: 2.500e-03, eta: 0:01:58, time: 1.018, data_time: 0.035, memory: 2228, loss_rpn_cls: 0.0027, loss_rpn_bbox: 0.0121, loss_cls: 0.0942, acc: 96.1914, loss_bbox: 0.1750, loss: 0.2840 2021-09-29 07:25:15,862 - mmdet - INFO - Epoch [8][10/25] lr: 2.500e-03, eta: 0:01:44, time: 1.209, data_time: 0.229, memory: 2228, loss_rpn_cls: 0.0014, loss_rpn_bbox: 0.0087, loss_cls: 0.0725, acc: 97.0410, loss_bbox: 0.1421, loss: 0.2247 2021-09-29 07:25:26,097 - mmdet - INFO - Epoch [8][20/25] lr: 2.500e-03, eta: 0:01:35, time: 1.023, data_time: 0.035, memory: 2228, loss_rpn_cls: 0.0022, loss_rpn_bbox: 0.0090, loss_cls: 0.0842, acc: 96.8555, loss_bbox: 0.1704, loss: 0.2658 2021-09-29 07:25:43,363 - mmdet - INFO - Epoch [9][10/25] lr: 2.500e-04, eta: 0:01:21, time: 1.208, data_time: 0.226, memory: 2228, loss_rpn_cls: 0.0015, loss_rpn_bbox: 0.0089, loss_cls: 0.0659, acc: 97.3535, loss_bbox: 0.1330, loss: 0.2092 2021-09-29 07:25:53,666 - mmdet - INFO - Epoch [9][20/25] lr: 2.500e-04, eta: 0:01:12, time: 1.031, data_time: 0.034, memory: 2228, loss_rpn_cls: 0.0014, loss_rpn_bbox: 0.0076, loss_cls: 0.0649, acc: 97.5293, loss_bbox: 0.1215, loss: 0.1954 2021-09-29 07:26:11,020 - mmdet - INFO - Epoch [10][10/25] lr: 2.500e-04, eta: 0:00:58, time: 1.220, data_time: 0.229, memory: 2228, loss_rpn_cls: 0.0036, loss_rpn_bbox: 0.0090, loss_cls: 0.0747, acc: 97.0312, loss_bbox: 0.1390, loss: 0.2263 2021-09-29 07:26:21,294 - mmdet - INFO - Epoch [10][20/25] lr: 2.500e-04, eta: 0:00:50, time: 1.028, data_time: 0.035, memory: 2228, loss_rpn_cls: 0.0012, loss_rpn_bbox: 0.0065, loss_cls: 0.0680, acc: 97.2852, loss_bbox: 0.1286, loss: 0.2042 2021-09-29 07:26:38,623 - mmdet - INFO - Epoch [11][10/25] lr: 2.500e-04, eta: 0:00:36, time: 1.216, data_time: 0.231, memory: 2228, loss_rpn_cls: 0.0034, loss_rpn_bbox: 0.0080, loss_cls: 0.0764, acc: 97.0605, loss_bbox: 0.1319, loss: 0.2196 2021-09-29 07:26:48,891 - mmdet - INFO - Epoch [11][20/25] lr: 2.500e-04, eta: 0:00:27, time: 1.026, data_time: 0.034, memory: 2228, loss_rpn_cls: 0.0014, loss_rpn_bbox: 0.0090, loss_cls: 0.0691, acc: 97.0898, loss_bbox: 0.1357, loss: 0.2151 2021-09-29 07:27:06,293 - mmdet - INFO - Epoch [12][10/25] lr: 2.500e-05, eta: 0:00:13, time: 1.224, data_time: 0.228, memory: 2228, loss_rpn_cls: 0.0013, loss_rpn_bbox: 0.0063, loss_cls: 0.0620, acc: 97.6074, loss_bbox: 0.1166, loss: 0.1862 2021-09-29 07:27:16,560 - mmdet - INFO - Epoch [12][20/25] lr: 2.500e-05, eta: 0:00:04, time: 1.026, data_time: 0.034, memory: 2228, loss_rpn_cls: 0.0022, loss_rpn_bbox: 0.0064, loss_cls: 0.0599, acc: 97.6367, loss_bbox: 0.1006, loss: 0.1691 2021-09-29 07:27:21,485 - mmdet - INFO - Saving checkpoint at 12 epochs
[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>] 25/25, 3.8 task/s, elapsed: 7s, ETA: 0s ---------------iou_thr: 0.5---------------
2021-09-29 07:27:29,304 - mmdet - INFO - +------------+-----+------+--------+-------+ | class | gts | dets | recall | ap | +------------+-----+------+--------+-------+ | Car | 62 | 155 | 0.887 | 0.809 | | Pedestrian | 13 | 56 | 0.846 | 0.679 | | Cyclist | 7 | 65 | 0.571 | 0.100 | +------------+-----+------+--------+-------+ | mAP | | | | 0.529 | +------------+-----+------+--------+-------+ 2021-09-29 07:27:29,308 - mmdet - INFO - Epoch(val) [12][25] AP50: 0.5290, mAP: 0.5292
In [23]:
pwd
Out[23]:
'/content/mmdetection'
In [ ]:
import cv2
import mmcv
model.cfg = cfg
video_reader = mmcv.VideoReader("/content/data/songdo_car.mp4")
video_writer = None
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video_writer = cv2.VideoWriter("/content/data/songdo_car_out.mp4", fourcc,
video_reader.fps, (video_reader.width, video_reader.height))
for frame in mmcv.track_iter_progress(video_reader):
result = inference_detector(model, frame)
frame = model.show_result(frame, result, score_thr=0.4)
video_writer.write(frame)
if video_writer:
video_writer.release()
[ ] 0/12751, elapsed: 0s, ETA:
/usr/local/lib/python3.7/dist-packages/mmdet-2.17.0-py3.7.egg/mmdet/datasets/utils.py:69: UserWarning: "ImageToTensor" pipeline is replaced by "DefaultFormatBundle" for batch inference. It is recommended to manually replace it in the test data pipeline in your config file. 'data pipeline in your config file.', UserWarning) /usr/local/lib/python3.7/dist-packages/mmdet-2.17.0-py3.7.egg/mmdet/core/anchor/anchor_generator.py:324: UserWarning: ``grid_anchors`` would be deprecated soon. Please use ``grid_priors`` warnings.warn('``grid_anchors`` would be deprecated soon. ' /usr/local/lib/python3.7/dist-packages/mmdet-2.17.0-py3.7.egg/mmdet/core/anchor/anchor_generator.py:361: UserWarning: ``single_level_grid_anchors`` would be deprecated soon. Please use ``single_level_grid_priors`` '``single_level_grid_anchors`` would be deprecated soon. '
[>>> ] 1533/12751, 1.5 task/s, elapsed: 1039s, ETA: 7603s
In [ ]:
'playdata' 카테고리의 다른 글
Object Detection(1001_day5) - yolov3 (0) | 2021.10.01 |
---|---|
Object Detection(0930_day4) - Oxford pet dataset (0) | 2021.09.30 |
Object Detection(0929_day3) - demo (0) | 2021.09.29 |
Object Detection(0929_day3) - Pascal & COCO dataset (0) | 2021.09.29 |
Object Detection(0927_day1) (0) | 2021.09.27 |
Comments