1 Star 8 Fork 3

monkey_cici / mmsegmentation

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
add_transforms.md 1.28 KB
一键复制 编辑 原始数据 按行查看 历史

Adding New Data Transforms

Customization data transformation

The customized data transformation must inherited from BaseTransform and implement transform function. Here we use a simple flipping transformation as example:

import random
import mmcv
from mmcv.transforms import BaseTransform, TRANSFORMS

@TRANSFORMS.register_module()
class MyFlip(BaseTransform):
    def __init__(self, direction: str):
        super().__init__()
        self.direction = direction

    def transform(self, results: dict) -> dict:
        img = results['img']
        results['img'] = mmcv.imflip(img, direction=self.direction)
        return results

Moreover, import the new class.

from .my_pipeline import MyFlip

Thus, we can instantiate a MyFlip object and use it to process the data dict.

import numpy as np

transform = MyFlip(direction='horizontal')
data_dict = {'img': np.random.rand(224, 224, 3)}
data_dict = transform(data_dict)
processed_img = data_dict['img']

Or, we can use MyFlip transformation in data pipeline in our config file.

pipeline = [
    ...
    dict(type='MyFlip', direction='horizontal'),
    ...
]

Note that if you want to use MyFlip in config, you must ensure the file containing MyFlip is imported during runtime.

Python
1
https://gitee.com/monkeycc/mmsegmentation.git
git@gitee.com:monkeycc/mmsegmentation.git
monkeycc
mmsegmentation
mmsegmentation
main

搜索帮助

53164aa7 5694891 3bd8fe86 5694891