mmdetection里的数据增强


前言

记录下mmdetection配置里的简单的数据增强,里面的代码都是截取的部分便于理解的代码。

LoadImageFromFile

这个就是加载图片的信息放入results的字典里,文件mmdet/datasets/pipelines/loading.py。
刚开始的results字典文件
在这里插入图片描述

results['filename'] = filename
results['ori_filename'] = results['img_info']['filename']
results['img'] = img
results['img_shape'] = img.shape
results['ori_shape'] = img.shape
results['img_fields'] = ['img']
return results

执行之后的
在这里插入图片描述
在这里插入图片描述
后面都一样,往字典里添加和对字典里的数据进行处理,就不放图了。

LoadAnnotations

加载图片的一些标签信息,文件mmdet/datasets/pipelines/loading.py。

if self.with_bbox:
     results = self._load_bboxes(results)
     if results is None:
         return None
 if self.with_label:
     results = self._load_labels(results)
 if self.with_mask:
     results = self._load_masks(results)
 if self.with_seg:
     results = self._load_semantic_seg(results)
 return results

1、Resize

数据增强的文件代码大部分都在mmdet/datasets/pipelines/transforms.py里,自己增加需要的数据增强方法最好也在这里增加。

self._resize_img(results)
self._resize_bboxes(results)    #这里先根据缩放因子进行相应的缩放。然后范围clip限制去除超出图像范围的部分。
self._resize_masks(results)
self._resize_seg(results)

2、RandomFlip

cur_dir = np.random.choice(direction_list, p=flip_ratio_list)   # [flip,None]随机选择是否翻转
results['flip'] = cur_dir is not None       # 
if 'flip_direction' not in results:
   results['flip_direction'] = cur_dir
if results['flip']:
   # flip image
   for key in results.get('img_fields', ['img']):
       results[key] = mmcv.imflip(
           results[key], direction=results['flip_direction'])
   # flip bboxes
   for key in results.get('bbox_fields', []):
       results[key] = self.bbox_flip(results[key],
                                     results['img_shape'],
                                     results['flip_direction'])
   # flip masks
   for key in results.get('mask_fields', []):
       results[key] = results[key].flip(results['flip_direction'])

   # flip segs
   for key in results.get('seg_fields', []):
       results[key] = mmcv.imflip(
           results[key], direction=results['flip_direction'])
return results

3.Normalize

代码如下(示例):

 for key in results.get('img_fields', ['img']):	# 是对img进行归一化
     results[key] = mmcv.imnormalize(results[key], self.mean, self.std,
                                     self.to_rgb)
 results['img_norm_cfg'] = dict(
     mean=self.mean, std=self.std, to_rgb=self.to_rgb)
 return results

4. Pad

    def _pad_img(self, results):
        """Pad images according to ``self.size``."""
        pad_val = self.pad_val.get('img', 0)
        for key in results.get('img_fields', ['img']):	# 是对img进行pad
            if self.pad_to_square:
                max_size = max(results[key].shape[:2])
                self.size = (max_size, max_size)
            if self.size is not None:
                padded_img = mmcv.impad(
                    results[key], shape=self.size, pad_val=pad_val)
            elif self.size_divisor is not None:
                padded_img = mmcv.impad_to_multiple(
                    results[key], self.size_divisor, pad_val=pad_val)
            results[key] = padded_img
        results['pad_shape'] = padded_img.shape
        results['pad_fixed_size'] = self.size
        results['pad_size_divisor'] = self.size_divisor

5.DefaultFormatBundle

   results = self._add_default_meta_keys(results)
   if len(img.shape) < 3:
       img = np.expand_dims(img, -1)
   img = np.ascontiguousarray(img.transpose(2, 0, 1))
   results['img'] = DC(to_tensor(img), stack=True)
for key in ['proposals', 'gt_bboxes', 'gt_bboxes_ignore', 'gt_labels']:
   if key not in results:
       continue
   results[key] = DC(to_tensor(results[key]))	#将格式to_tensor

6.collect

data = {}
img_meta = {}
for key in self.meta_keys:
    img_meta[key] = results[key]	# 最后输入需要什么就将一些结果字典里的字段复制过来
data['img_metas'] = DC(img_meta, cpu_only=True)
for key in self.keys:
    data[key] = results[key]
return data		# 最后返回需要的数据

最后字典里的数据

在这里插入图片描述

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
mmdetection中,有多种方法可以进行数据集增强。一种方法是在送入机器学习之前,对数据集进行转换。这可以通过执行一系列的转换操作来实现,这些转换操作可以增强数据集的大小。另一种方法是在小批量(mini-batch)上执行这些转换。这样可以减少内存的占用,并且可以提高训练的效率。 在mmdetection中,有一些内置的方法可以用来进行数据集增强。其中包括ClassBalancedDataset、ConcatDataset和RepeatDataset等方法。这些方法可以根据需要进行选择,以实现数据集的增强效果。 如果需要自定义新的数据增强方法,可以在mmdetection的代码中新建一个augment.py文件,然后在其中编写自己的数据增强类。在初始化的声明中添加这个新的数据增强类,然后就可以在训练管道中使用它了。例如,在train_pipeline中添加一个dict(type='augment'),即可使用新的数据增强方法。 在mmdetection中,还有许多其他的数据增强方法可以使用,包括Compose、to_tensor、ToTensor、ImageToTensor、ToDataContainer、Transpose、Collect、DefaultFormatBundle、LoadAnnotations、LoadImageFromFile等。此外,还有Resize、RandomFlip、Pad、RandomCrop、Normalize、SegRescale、MinIoURandomCrop、Expand、PhotoMetricDistortion、Albu、InstaBoost、RandomCenterCropPad、AutoAugment、CutOut、Shear、Rotate、ColorTransform、EqualizeTransform、BrightnessTransform、ContrastTransform、Translate、RandomShift等方法。这些方法可以根据不同的需求选择使用,以实现不同的数据增强效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [mmdetection中的数据增强方法(慢慢写, 会很长)](https://blog.csdn.net/liuqiangaliuv/article/details/119682448)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI、明察秋毫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值