本专栏目录:
核心内容篇:
- 目标检测——从简单的开始!进击的YOLO!
- 目标检测——Backbone与Detection head
- 目标检测——搭建更好更快的YOLO!
- 目标检测——制作GroundTruth!开始训练!
- 目标检测——YOLOv2!
- 目标检测——YOLOv3!
- 目标检测——YOLO轻量化初尝试-SlimYOLOv2
- 目标检测——小小的TinyYOLOv3
- 目标检测——anchor-free的回归之作-FCOS
- 目标检测——anchor-free之CenterNet的浅析与实现
番外篇:
- 番外一:darknet19与darknet53
- 番外二:ImageNet预训练
- 番外三:人脸检测实战-YOLAF
- 番外四:实时-轻量人脸检测
- 番外五:YOLOv3-Plus
- 番外六:YOLOv3-Slim
- 番外七:更好的backbone-CSPDarknet系列
1. 制作训练数据
本工作中,让我们来使用PASCAL VOC2007和VOC2012数据集作为训练集,VOC2007test作为测试集,之所以选择VOC,主要是因为它太经典了!而且VOC2007测试集还有真实标签,方便我们算mAP去评价模型的好坏。关于数据集的下载,大家可以执行下面的VOC2007.sh和VOC2012.sh两个批处理文件来下载数据:
VOC2007.sh:
#!/bin/bash
# Ellis Brown
start=`date +%s`
# handle optional download dir
if [ -z "$1" ]
then
# navigate to ~/data
echo "navigating to ~/data/ ..."
mkdir -p ~/data
cd ~/data/
else
# check if is valid directory
if [ ! -d $1 ]; then
echo $1 "is not a valid directory"
exit 0
fi
echo "navigating to" $1 "..."
cd $1
fi
echo "Downloading VOC2007 trainval ..."
# Download the data.
curl -LO http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
echo "Downloading VOC2007 test data ..."
curl -LO http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar
echo "Done downloading."
# Extract data
echo "Extracting trainval ..."
tar -xvf VOCtrainval_06-Nov-2007.tar
echo "Extracting test ..."
tar -xvf VOCtest_06-Nov-2007.tar
echo "removing tars ..."
rm VOCtrainval_06-Nov-2007.tar
rm VOCtest_06-Nov-2007.tar
end=`date +%s`
runtime=$((end-start))
echo "Completed in" $runtime "seconds"
VOC2012.sh:
#!/bin/bash
# Ellis Brown
start=`date +%s`
# handle optional download dir
if [ -z "$1" ]
then
# navigate to ~/data
echo "navigating to ~/data/ ..."
mkdir -p ~/data
cd ~/data/
else
# check if is valid directory
if [ ! -d $1 ]; then
echo $1 "is not a valid directory"
exit 0
fi
echo "navigating to" $1 "..."
cd $1
fi
echo "Downloading VOC2012 trainval ..."
# Download the data.
curl -LO http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar
echo "Done downloading."
# Extract data
echo "Extracting trainval ..."
tar -xvf VOCtrainval_11-May-2012.tar
echo "removing tar ..."
rm VOCtrainval_11-May-2012.tar
end=`date +%s`
runtime=$((end-start))
echo "Completed in" $runtime "seconds"
当然,COCO的下载文件我也提供了,显卡多的小伙伴可以考虑一下COCO呦~
假定,我们现在已经下好了数据集,VOC数据集中,图片是jpg格式的,而label都存在了xml文件中。如果读者在哪里看到了类似“VOC格式的label”,其实指的就是用xml文件来保存label数据。为了能够从xml文件读取出我们想要的bbox、class label等信息,我们首先定义一个类:
from .config import HOME
import os.path as osp
import sys
import torch
import torch.utils.data as data
import cv2
import numpy as np
if sys.version_info[0] == 2:
import xml.etree.cElementTree as ET
else:
import xml.etree.ElementTree as ET
VOC_CLASSES = ( # always index 0
'aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair',
'cow', 'diningtable', 'dog', 'horse',
'motorbike', 'person', 'pottedplant',
'sheep', 'sofa', 'train', 'tvmonitor')
# note: if you used our download scripts, this should be right
VOC_ROOT = "e:python_workODyolo-guidedataVOCdevkit"
class VOCAnnotationTransform(object):
"""Transforms a VOC annotation into a Tensor of bbox coords and label index
Initilized with a dictionary lookup of classnames to indexes
Arguments:
class_to_ind (dict, optional): dictionary lookup of classnames -> indexes
(default: alphabetic indexing of VOC's 20 classes)
keep_difficult (bool, optional): keep difficult instances or not
(default