pyskl/datasets/pipelines/compose.py注意事项

打印流水线

# Copyright (c) OpenMMLab. All rights reserved.
from collections.abc import Sequence
from mmcv.utils import build_from_cfg

from pyskl.datasets.builder import PIPELINES

# @PIPELINES.register_module()
class Compose:
    """Compose a data pipeline with a sequence of transforms.

    Args:
        transforms (list[dict | callable]):
            Either config dicts of transforms or transform objects.
    """

    def __init__(self, transforms):
        assert isinstance(transforms, Sequence)
        print(isinstance(transforms, Sequence))
        self.transforms = []
        print(transforms)
        for transform in transforms:
            if isinstance(transform, dict):
                print(isinstance(transform, dict))
                transform = build_from_cfg(transform, PIPELINES)
                print(transform)
                self.transforms.append(transform)
            elif callable(transform):
                self.transforms.append(transform)
            else:
                raise TypeError(f'transform must be callable or a dict, '
                                f'but got {type(transform)}')

    def __call__(self, data):
        """Call function to apply transforms sequentially.

        Args:
            data (dict): A result dict contains the data to transform.

        Returns:
            dict: Transformed data.
        """

        for t in self.transforms:
            data = t(data)
            if data is None:
                return None
        return data

    def __repr__(self):
        format_string = self.__class__.__name__ + '('
        for t in self.transforms:
            format_string += '\n'
            format_string += '    {0}'.format(t)
        format_string += '\n)'
        return format_string

left_kp = [1, 3, 5, 7, 9, 11, 13, 15]
right_kp = [2, 4, 6, 8, 10, 12, 14, 16]    
train_pipeline = [
    dict(type='UniformSampleFrames', clip_len=32),
    dict(type='PoseDecode'),
    dict(type='PoseCompact', hw_ratio=1., allow_imgpad=True),
    dict(type='Resize', scale=(64, 64), keep_ratio=False),
    dict(type='RandomResizedCrop', area_range=(0.56, 1.0)),
    dict(type='Resize', scale=(56, 56), keep_ratio=False),
    dict(type='Flip', flip_ratio=0.5, left_kp=left_kp, right_kp=right_kp),
    dict(type='GeneratePoseTarget', with_kp=True, with_limb=False),
    dict(type='FormatShape', input_format='NCTHW_Heatmap'),
    dict(type='Collect', keys=['imgs', 'label'], meta_keys=[]),
    dict(type='ToTensor', keys=['imgs', 'label'])
]    
a = Compose(train_pipeline)
print(dir(a))

(pyskl) root@autodl-container-794d4f961c-8cb9245b:~/pyskl# python compose.py
True
[{'type': 'UniformSampleFrames', 'clip_len': 32}, {'type': 'PoseDecode'}, {'type': 'PoseCompact', 'hw_ratio': 1.0, 'allow_imgpad': True}, {'type': 'Resize', 'scale': (64, 64), 'keep_ratio': False}, {'type': 'RandomResizedCrop', 'area_range': (0.56, 1.0)}, {'type': 'Resize', 'scale': (56, 56), 'keep_ratio': False}, {'type': 'Flip', 'flip_ratio': 0.5, 'left_kp': [1, 3, 5, 7, 9, 11, 13, 15], 'right_kp': [2, 4, 6, 8, 10, 12, 14, 16]}, {'type': 'GeneratePoseTarget', 'with_kp': True, 'with_limb': False}, {'type': 'FormatShape', 'input_format': 'NCTHW_Heatmap'}, {'type': 'Collect', 'keys': ['imgs', 'label'], 'meta_keys': []}, {'type': 'ToTensor', 'keys': ['imgs', 'label']}]
True
UniformSampleFrames(clip_len=32, num_clips=1, seed=255)
True
PoseDecode()
True
PoseCompact(padding=0.25, threshold=10, hw_ratio=(1.0, 1.0), allow_imgpad=True)
True
Resize(scale=(64, 64), keep_ratio=False, interpolation=bilinear)
True
RandomResizedCrop(area_range=(0.56, 1.0), aspect_ratio_range=(0.75, 1.3333333333333333))
True
Resize(scale=(56, 56), keep_ratio=False, interpolation=bilinear)
True
Flip(flip_ratio=0.5, direction=horizontal, flip_label_map=None)
True
GeneratePoseTarget(sigma=0.6, use_score=True, with_kp=True, with_limb=False, skeletons=((0, 1), (0, 2), (1, 3), (2, 4), (0, 5), (5, 7), (7, 9), (0, 6), (6, 8), (8, 10), (5, 11), (11, 13), (13, 15), (6, 12), (12, 14), (14, 16), (11, 12)), double=False, left_kp=(1, 3, 5, 7, 9, 11, 13, 15), right_kp=(2, 4, 6, 8, 10, 12, 14, 16))
True
FormatShape(input_format='NCTHW_Heatmap')
True
Collect(keys=['imgs', 'label'], meta_keys=[], nested=False)
True
ToTensor(keys=['imgs', 'label'])
['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'transforms']
(pyskl) root@autodl-container-794d4f961c-8cb9245b:~/pyskl#

left_kp = [1, 3, 5, 7, 9, 11, 13, 15]
right_kp = [2, 4, 6, 8, 10, 12, 14, 16]    
train_pipeline = [
    dict(type='UniformSampleFrames', clip_len=32),
    dict(type='PoseDecode'),
    dict(type='PoseCompact', hw_ratio=1., allow_imgpad=True),
    dict(type='Resize', scale=(64, 64), keep_ratio=False),
    dict(type='RandomResizedCrop', area_range=(0.56, 1.0)),
    dict(type='Resize', scale=(56, 56), keep_ratio=False),
    dict(type='Flip', flip_ratio=0.5, left_kp=left_kp, right_kp=right_kp),
    dict(type='GeneratePoseTarget', with_kp=True, with_limb=False),
    dict(type='FormatShape', input_format='NCTHW_Heatmap'),
    dict(type='Collect', keys=['imgs', 'label'], meta_keys=[]),
    dict(type='ToTensor', keys=['imgs', 'label'])
]    
a = Compose(train_pipeline)
print(a.transforms)

[UniformSampleFrames(clip_len=32, num_clips=1, seed=255), PoseDecode(), PoseCompact(padding=0.25, threshold=10, hw_ratio=(1.0, 1.0), allow_imgpad=True), Resize(scale=(64, 64), keep_ratio=False, interpolation=bilinear), RandomResizedCrop(area_range=(0.56, 1.0), aspect_ratio_range=(0.75, 1.3333333333333333)), Resize(scale=(56, 56), keep_ratio=False, interpolation=bilinear), Flip(flip_ratio=0.5, direction=horizontal, flip_label_map=None), GeneratePoseTarget(sigma=0.6, use_score=True, with_kp=True, with_limb=False, skeletons=((0, 1), (0, 2), (1, 3), (2, 4), (0, 5), (5, 7), (7, 9), (0, 6), (6, 8), (8, 10), (5, 11), (11, 13), (13, 15), (6, 12), (12, 14), (14, 16), (11, 12)), double=False, left_kp=(1, 3, 5, 7, 9, 11, 13, 15), right_kp=(2, 4, 6, 8, 10, 12, 14, 16)), FormatShape(input_format='NCTHW_Heatmap'), Collect(keys=['imgs', 'label'], meta_keys=[], nested=False), ToTensor(keys=['imgs', 'label'])]

left_kp = [1, 3, 5, 7, 9, 11, 13, 15]
right_kp = [2, 4, 6, 8, 10, 12, 14, 16]    
train_pipeline = [
    dict(type='UniformSampleFrames', clip_len=32),
    dict(type='PoseDecode'),
    dict(type='PoseCompact', hw_ratio=1., allow_imgpad=True),
    dict(type='Resize', scale=(64, 64), keep_ratio=False),
    dict(type='RandomResizedCrop', area_range=(0.56, 1.0)),
    dict(type='Resize', scale=(56, 56), keep_ratio=False),
    dict(type='Flip', flip_ratio=0.5, left_kp=left_kp, right_kp=right_kp),
    dict(type='GeneratePoseTarget', with_kp=True, with_limb=False),
    dict(type='FormatShape', input_format='NCTHW_Heatmap'),
    dict(type='Collect', keys=['imgs', 'label'], meta_keys=[]),
    dict(type='ToTensor', keys=['imgs', 'label'])
]    
a = Compose(train_pipeline)
print(a.transforms)
print(a)

发现执行print(a)的时候,会自动调用 __repr__ 方法,输出下面的字符串:

Compose(
    UniformSampleFrames(clip_len=32, num_clips=1, seed=255)
    PoseDecode()
    PoseCompact(padding=0.25, threshold=10, hw_ratio=(1.0, 1.0), allow_imgpad=True)
    Resize(scale=(64, 64), keep_ratio=False, interpolation=bilinear)
    RandomResizedCrop(area_range=(0.56, 1.0), aspect_ratio_range=(0.75, 1.3333333333333333))
    Resize(scale=(56, 56), keep_ratio=False, interpolation=bilinear)
    Flip(flip_ratio=0.5, direction=horizontal, flip_label_map=None)
    GeneratePoseTarget(sigma=0.6, use_score=True, with_kp=True, with_limb=False, skeletons=((0, 1), (0, 2), (1, 3), (2, 4), (0, 5), (5, 7), (7, 9), (0, 6), (6, 8), (8, 10), (5, 11), (11, 13), (13, 15), (6, 12), (12, 14), (14, 16), (11, 12)), double=False, left_kp=(1, 3, 5, 7, 9, 11, 13, 15), right_kp=(2, 4, 6, 8, 10, 12, 14, 16))
    FormatShape(input_format='NCTHW_Heatmap')
    Collect(keys=['imgs', 'label'], meta_keys=[], nested=False)
    ToTensor(keys=['imgs', 'label'])
)

在 Compose 类中,这些变换操作是按照顺序依次执行的。也就是说,前一个操作的输出会作为后一个操作的输入。

这种将多个变换操作组合成一个管道的方式非常常见,它有以下几个好处:

  1. 可读性和可维护性:将所有的变换操作集中在一起,使得整个数据预处理流程一目了然,便于理解和修改。

  2. 灵活性:可以轻松地调整管道中的操作顺序或替换某些操作,而无需修改调用方的代码。

  3. 复用性:将常用的数据预处理步骤封装成一个可复用的 Compose 对象,可以在多个项目或模型中重复使用。

  4. 效率:将多个操作组合在一起,可以减少中间数据的存储和传输,提高整体的计算效率。

所以在处理复杂的数据预处理任务时,使用 Compose 这种管道式的设计模式是非常有效的。开发者可以根据需求灵活地组装和调整这些变换操作,以满足不同场景下的需求。

pyskl/pyskl/datasets/pipelines/compose.py at main · kennymckormick/pyskl · GitHub

  • 13
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值