DOTA数据集 | retinanet 实验记录(COCO数据格式)

源码链接

https://github.com/fizyr/keras-retinanet

运行环境

  • Ubuntu16.04
  • python3.6.5
  • CUDA 10.2 ( NVIDIA-SMI 440.33.01 Driver Version: 440.33.01 )

需要下载文件

训练步骤

  1. 下载该仓库: https://github.com/fizyr/keras-retinanet.git
  2. 数据格式是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)
### DOTA 数据集及其 COCO 格式版本 DOTA 是一个大规模的遥感图像目标检测数据集,包含了多种类别的物体标注。为了方便不同模型框架下的训练和评估工作,部分研究者已经将原始的 DOTA 数据集转换成了广泛使用的 COCO 标准格式。 #### 获取 DOTA 数据集并将其转为 COCO 格式 目前官方发布的 DOTA 数据集中并不直接提供 COCO 格式的文件[^3]。然而,社区内存在一些开源项目可以帮助完成这一转换过程: - **GitHub 上的相关资源**:可以找到多个 Python 脚本用于实现从原生 DOTA 格式COCO 的转换。这些脚本通常会处理类别映射、坐标变换等问题。 ```python import json from collections import defaultdict def dota_to_coco(dota_annotation_file, output_json_path): coco_data = { "images": [], "annotations": [], "categories": [] } category_id_mapping = {} # 建立类别ID映射表 with open(dota_annotation_file, 'r') as f: lines = f.readlines() image_info = {} annotation_id_counter = 1 for line in lines: parts = line.strip().split(' ') img_name = parts[0] bboxes = list(map(float, parts[1:])) if img_name not in image_info: image_entry = {"id": len(image_info)+1, "file_name":img_name} image_info[img_name] = len(image_info)+1 coco_data["images"].append(image_entry) obj_count_per_image = int(len(bboxes)/9) for i in range(obj_count_per_image): bbox = bboxes[i*9:(i+1)*9][:8] # 取前八个数值作为bbox x_coords = [bbox[j] for j in range(0,8,2)] y_coords = [bbox[j] for j in range(1,8,2)] min_x, max_x = min(x_coords), max(x_coords) min_y, max_y = min(y_coords), max(y_coords) width = max_x - min_x height = max_y - min_y area = width * height class_label = str(int(bbox[-1])) # 类别标签假设位于最后一个位置 if class_label not in category_id_mapping: new_cat_id = len(category_id_mapping) + 1 category_id_mapping[class_label] = new_cat_id cat_entry = {"supercategory":"none", "id":new_cat_id, "name":class_label} coco_data['categories'].append(cat_entry) ann_entry = { "segmentation":[], "area":area, "iscrowd":0, "image_id":image_info[img_name], "bbox":[min_x,min_y,width,height], "category_id":category_id_mapping[class_label], "id":annotation_id_counter } coco_data["annotations"].append(ann_entry) annotation_id_counter += 1 with open(output_json_path,'w') as out_f: json.dump(coco_data,out_f) if __name__ == "__main__": input_dota_ann = "./path/to/dota/label.txt" output_coco_format = "./output/coco_style_annotations.json" dota_to_coco(input_dota_ann,output_coco_format) ``` 此代码片段展示了如何读取 DOTA 风格的文本注解并将它们转化为 COCO JSON 文件结构的一部分。需要注意的是实际应用时可能还需要调整细节来适应具体的数据分布情况[^2]。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值