retinanet 实验记录
源码链接
https://github.com/fizyr/keras-retinanet
运行环境
- Ubuntu16.04
- python3.6.5
- CUDA 10.2 ( NVIDIA-SMI 440.33.01 Driver Version: 440.33.01 )
需要下载文件
训练步骤
- 下载该仓库:
https://github.com/fizyr/keras-retinanet.git
- 数据格式是coco,按照标准的coco格式放置数据即可。
训练命令:
# Running directly from the repository:
keras_retinanet/bin/train.py coco /path/to/MS/COCO
# Using the installed script:
retinanet-train coco /path/to/MS/COCO
python train.py coco /home/xxx/DOTA/DOTA_clip_coco_600/DOTA_clip_coco_600
/path/to/MS/COCO:输入你的coco数据存放的位置。下面的命令没有试过,有需要的朋友可以自己实践一下。
PS:得到训练模型,想要用模型来评估就需要把模型转换一下。
转换模型:
python convert_model.py ./snapshots/resnet50_coco_06.h5 ./models/resnet50_coco_06.h5
评估命令:
python evaluate.py --image-min-side=600 --image-max-side=600 coco /home/xxx/DOTA/DOTA_clip_coco_600/DOTA_clip_coco_600/ ./models/resnet50_coco_06.h5
运行作者给出的预训练模型命令:
首先把从release下载模型,把模型放在snapshots里,运行下面的代码就🆗了。
python train.py --weights ~/keras-retinanet/snapshots/resnet50_coco_best_v2.0.1.h5 coco /home/xxx/DOTA/DOTA_clip_coco_600/DOTA_clip_coco_600
实验输出(以下贴出部分map)
预测各个类别的map
在./keras_retinanet/callbacks/coco.py 中加入下面这个函数即可。参考博客
def _print_detection_eval_metrics(self, coco_eval):
IoU_lo_thresh = 0.5
IoU_hi_thresh = 0.95
def _get_thr_ind(coco_eval, thr):
ind = np.where((coco_eval.params.iouThrs > thr - 1e-5) &
(coco_eval.params.iouThrs < thr + 1e-5))[0][0]
iou_thr = coco_eval.params.iouThrs[ind]
assert np.isclose(iou_thr, thr)
return ind
ind_lo = _get_thr_ind(coco_eval, IoU_lo_thresh)
ind_hi = _get_thr_ind(coco_eval, IoU_hi_thresh)
# precision has dims (iou, recall, cls, area range, max dets)
# area range index 0: all area ranges
# max dets index 2: 100 per image
precision = \
coco_eval.eval['precision'][ind_lo:(ind_hi + 1), :, :, 0, 2]
ap_default = np.mean(precision[precision > -1])
print(('~~~~ Mean and per-category AP @ IoU=[{:.2f},{:.2f}] '
'~~~~').format(IoU_lo_thresh, IoU_hi_thresh))
# print("")
print('MAP:{:.1f}'.format(100 * ap_default))
for cls_ind, cls in enumerate(self._classes):
if cls == '__background__':
continue
# minus 1 because of __background__
# cat_name = db.class_name(cls_ind)
# print(cat_name)
cat_name = self.class_name(cls)
# print(cat_name+":")
precision = coco_eval.eval['precision'][ind_lo:(ind_hi + 1), :, cls_ind, 0, 2]
ap = np.mean(precision[precision > -1])
print(cat_name+':{:.1f}'.format(100 * ap))
并在./keras-retinanet/keras_retinanet/utils/coco_eval.py中coco_eval.accumulate()下面加入:
coco_eval._print_detection_eval_metrics(coco_eval)