FCOS訓練自己數據集
FCOS測試:
論文參考博主另一篇博客:論文閱讀【FCOS】
YOLO V3測試:
一、環境配置:
2種方案,源碼更清晰一點,mm框架的訓練方便。
- 直接項目源碼:https://github.com/tianzhi0549/FCOS ,截止目前 pytorch每天都在更新nightly(測試)版本,建議安裝最新穩定(stable)版pytorch=1.3.1, torchvision=0.4.2, cudatoolkit=10.0匹配cuda10.0,使用命令nvcc -V 查下你當前的cuda版本,最後執行命令:
# first, make sure that your conda is setup properly with the right environment
# for that, check that `which conda`, `which pip` and `which python` points to the
# right path. From a clean conda env, this is what you need to do
conda create --name FCOS
conda activate FCOS
# this installs the right pip and dependencies for the fresh python
conda install ipython
# FCOS and coco api dependencies
pip install ninja yacs cython matplotlib tqdm
# follow PyTorch installation in https://pytorch.org/get-started/locally/
# we give the instructions for CUDA 9.0
conda install -c pytorch==1.3.1 torchvision=0.4.2 cudatoolkit=10.0
# install pycocotools. Please make sure you have installed cython.
cd到home/xxx下
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
python setup.py build_ext install
# install PyTorch Detection
cd到home/xxx下
git clone https://github.com/tianzhi0549/FCOS.git
cd FCOS
# the following will install the lib with
# symbolic links, so that you can modify
# the files if you want and won't need to
# re-build it
激活fcos環境再編譯 source activate FCOS
python setup.py build develop --no-deps
2.搭建mmdetection:
參考:https://github.com/open-mmlab/mmdetection
mmdetection安裝文檔詳見:https://github.com/open-mmlab/mmdetection/blob/master/docs/INSTALL.md
mmcv安裝文檔詳見: https://github.com/open-mmlab/mmcv
要求
- Linux (Windows is not officially supported)
- Python 3.5+ (Python 2 is not supported)
- PyTorch 1.1 or higher
- CUDA 9.0 or higher
- NCCL 2
- GCC(G++) 4.9 or higher
- mmcv
注:這裏安裝前一般把 NCCL 和 MMCV提前安裝好
安裝虛擬環境
conda還是好一點,裝局部的話,安裝過成中 將 set conda python那個選 no,就不會改變系統python 裝局部啓動環境時
執行:
cd anacond3/bin
source activate
source activate open-mmlab
不要忙着啓動了,還沒搭環境呢,anaconda安裝好之後執行:
a. Create a conda virtual environment and activate it.
conda create -n open-mmlab python=3.7 -y
conda activate open-mmlab
b. Install PyTorch stable or nightly and torchvision following the official instructions, e.g.,
conda install pytorch torchvision -c pytorch
c. Clone the mmdetection repository.
git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
d. Install mmdetection (other dependencies will be installed automatically).
python setup.py develop
# or "pip install -v -e ."
二、製作VOC格式數據集:
這裏沒有用coco格式,原作者代碼解析coco官方數據集沒問題,博主自己轉換的就有問題,這裏我看了coco API藉口也沒發現自己製作的問題,算了趕時間我就換VOC格式,VOC相比COCO能簡單,COCO數據集無用的信息太多了。
參考:https://blog.csdn.net/u011574296/article/details/78953681
這個網上很多,如果不想製作看博主一篇文章:我們直接提取VOC,或者COCO的某幾類作爲自己的數據集,博主提取COCO的person一類哈 :
提取VOC或COCO某幾類數據集:https://blog.csdn.net/weixin_38632246/article/details/97141364:
三、實驗細節:
修改配置文件~ /mmdetection/config/fcos/fcos_r50_caffe_fpn_gn_1x_4gpu.py
# model settings
model = dict(
type='FCOS',
pretrained='/home/rock/mmdetection/configs/fcos/resnet50_caffe-788b5fa3.pth', #這個可以不改,默認直接下載
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=False),
style='caffe'),
neck=dict(
type='FPN',
in_channels=[256, 512, 1024, 2048],
out_channels=256,
start_level=1,
add_extra_convs=True,
extra_convs_on_inputs=False, # use P5
num_outs=5,
relu_before_extra_convs=True),
bbox_head=dict(
type='FCOSHead',
num_classes=2, #改下類別爲 class+1
in_channels=256,
stacked_convs=4,
feat_channels=256,
strides=[8, 16, 32, 64, 128],
loss_cls=dict(
type='FocalLoss',
use_sigmoid=True,
gamma=2.0,
alpha=0.25,
loss_weight=1.0),
loss_bbox=dict(type='IoULoss', loss_weight=1.0),
loss_centerness=dict(
type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0)))
# training and testing settings
train_cfg = dict(
assigner=dict(
type='MaxIoUAssigner',
pos_iou_thr=0.5,
neg_iou_thr=0.4,
min_pos_iou=0,
ignore_iof_thr=-1),
allowed_border=-1,
pos_weight=-1,
debug=False)
test_cfg = dict(
nms_pre=1000,
min_bbox_size=0,
score_thr=0.05,
nms=dict(type='nms', iou_thr=0.5),
max_per_img=100)
# dataset settings
dataset_type = 'VOCDataset' #改成 VOCDataset
data_root = '/home/rock/dataset/pd/'# 博主在~/pd/下存放的是轉換後的VOC2012數據集,建議名字VOC2012,不要亂改,不然程序要多改/home/rock/mmdetection/mmdet/datasets/voc.py 文件,後面再說嘍
img_norm_cfg = dict(
mean=[102.9801, 115.9465, 122.7717], std=[1.0, 1.0, 1.0], to_rgb=False)
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', **img_norm_cfg),
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', **img_norm_cfg),
dict(type='Pad', size_divisor=32),
dict(type='ImageToTensor', keys=['img']),
dict(type='Collect', keys=['img']),
])
]
data = dict(
imgs_per_gpu=4,
workers_per_gpu=4,
train=dict(
type=dataset_type,
ann_file=data_root + 'VOC2012/ImageSets/Main/trainval.txt', #以下數據集路徑都要改
img_prefix=data_root+'VOC2012/',
pipeline=train_pipeline),
val=dict(
type=dataset_type,
ann_file=data_root + 'VOC2012/ImageSets/Main/test.txt',
img_prefix=data_root+'VOC2012/',
pipeline=test_pipeline),
test=dict(
type=dataset_type,
ann_file=data_root + 'VOC2012/ImageSets/Main/test.txt',
img_prefix=data_root+'VOC2012/',
pipeline=test_pipeline))
# optimizer
optimizer = dict(
type='SGD',
lr=0.002,
momentum=0.9,
weight_decay=0.0001,
paramwise_options=dict(bias_lr_mult=2., bias_decay_mult=0.))
optimizer_config = dict(grad_clip=None)
# learning policy
lr_config = dict(
policy='step',
warmup='constant',
warmup_iters=500,
warmup_ratio=1.0 / 3,
step=[8, 11])
checkpoint_config = dict(interval=1)
# yapf:disable
log_config = dict(
interval=50,
hooks=[
dict(type='TextLoggerHook'),
dict(type='TensorboardLoggerHook') #把這項打開,tensorboard 可以查看日誌
])
# yapf:enable
# runtime settings
total_epochs = 12
device_ids = range(4)
dist_params = dict(backend='nccl')
log_level = 'INFO'
work_dir = './work_dirs/fcos_r50_caffe_fpn_gn_1x_4gpu_pd'
load_from = None
resume_from = None
workflow = [('train', 1)]
修改配置文件 /mmdetection/mmdet/datasets/voc.py
from .registry import DATASETS
from .xml_style import XMLDataset
@DATASETS.register_module
class VOCDataset(XMLDataset):
# CLASSES = ('aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car',
# 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse',
# 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train',
# 'tvmonitor')
CLASSES = ('person',) #幾類你寫幾類,一類把最後的逗號加上
def __init__(self, **kwargs):
super(VOCDataset, self).__init__(**kwargs)
if 'VOC2007' in self.img_prefix:
self.year = 2007
elif 'VOC2012' in self.img_prefix:
self.year = 2012 # 如果你是VOC2017把數字改過來就行
else:
raise ValueError('Cannot infer dataset year from img_prefix')
訓練命令
python tools/test.py configs/fcos/fcos_r50_caffe_fpn_gn_1x_4gpu.py
測試命令
python tools/test.py configs/fcos/fcos_r50_caffe_fpn_gn_1x_4gpu.py /home/rock/mmdetection/work_dirs/fcos_r50_caffe_fpn_gn_1x_4gpu_pd/epoch_5.pth --show
注:按任意健切換測試圖像,ctrl+Z 退出
四、訓練結果
網絡輸入大小 img_height<800, img_width<1333, 實驗用了2萬7千多張訓練,1萬5千張測試:
主要觀察參數 loss_cls下降到0.3以下,loss_bbox 大概在0.4-0.3以下, 總體loss在1.0 左右說明訓練正常
類別置信度損失:
總體損失函數
中心點損失:
邊框迴歸損失:
五、FCOS在該數據集的MAP測量
博主測了下迭代5個epoch的model在測試集上的表現,最終map爲66.38,這個和yolov3 608尺寸迭代40000次的結果爲66.50,況且博主測試的是最差的FCOS主幹網絡(resnet-50),博主估計其骨架網絡最好的可以比 YOLO v3 map肯定高出不少,YOLO v3-608的速度與FCOS(resnet-50)兩者速度持平
配置(單GPU,2080Ti), 博主測試一段1分鐘視頻,YOLO v3-608 總耗時2分08秒,FCOS-resnet50 耗時2分48秒,兩者fps比較如下圖,YOLO v3爲16fps左右,FCOS爲14fps左右。
FCOS論文閱讀 見博主這篇文章https://blog.csdn.net/weixin_38632246/article/details/100542184
分享下幾點總結:
1、FCOS網絡主要去除Anchor ,解決了遮擋問題,是一種點對點的檢測
2、其缺點是cls_score 的打分有點低,yolo一般在1.0-0.5, fcos在0.7-0.3
3、FCOS CenterNet將目標檢測推向新的方法領域,Anchor-Free is better !
================== 更新11.28 -2019==========================
剛訓練下fcos-mobilenetv2:模型大小30MB,一臺2080ti訓練完大概9個小時,比fcos要快很多,原fcos的BFLOPS爲33左右,fcos-mobilenetv2的BFLOPS爲22,減小了大約三分之一的計算量,博主測試了下在26000張picture的map爲75.6,fps 爲50左右,accuracy幾乎沒有降低,厲害了。
博主最近剛擼完cornernet-lite系列的code,後面會寫系列的源碼解讀哈,目標檢測的paper更新的實在太快了,讀paper要勤奮呀!
fcos有很多改進的地方,比如以下幾點:
1.基於主幹網絡(backbone)的更新,我列舉幾個:mobilenetv1-v3,shufflenet v1-v2, vovnet, hrnet, efficientDet
2.優化loss模塊 nms,heatmap-gaussian 等
3.分佈式訓練,GroupNorm層,Leak_relu, attention機制。
-------------------------12.31.2019-----------------------------------
更新一下一張富有哲理的圖,這個是fcos的 center 預生成的location 位置,暫且不說,看完源碼詳細補充。。。。
-------------------------13.3.2020-----------------------------------
更新一下map更高的paper Bridging the Gap Between Anchor-based and Anchor-free Detection via Adaptive Training Sample Selection
同FCOS,將anchor的方式加入FCOS,取消原來的Center預測,同基準測試行人檢測,ATSS-mobilenet比FCOS-mobilenet 高25%的map,下圖右三的黑衣服女孩檢測出來了,後面的2個人頭其實也檢測出來了,不過我提高了檢測閾值。
同時記錄一下新的trick:
1).apex的FP16技術可以加速網絡訓練,提升anchor-free模型單GPU大概8-10倍
2). mixup 的訓練方式可以提高map,大約1%-5%,詳細請參看論文,附代碼
3) label smooth 技術,參看論文