MCPcopy Create free account
hub / github.com/MotrixLab/FineMoGen / RandomCrop

Class RandomCrop

mogen/datasets/pipelines/transforms.py:52–101  ·  view source on GitHub ↗

r"""Random crop motion sequences. Each sequence will be padded with zeros to the maximum length. Args: min_size (int or None): The minimum size of the cropped motion sequence (inclusive). max_size (int or None): The maximum size of the cropped motion

Source from the content-addressed store, hash-verified

50
51@PIPELINES.register_module()
52class RandomCrop(object):
53 r"""Random crop motion sequences. Each sequence will be padded with zeros
54 to the maximum length.
55
56 Args:
57 min_size (int or None): The minimum size of the cropped motion
58 sequence (inclusive).
59 max_size (int or None): The maximum size of the cropped motion
60 sequence (inclusive).
61 """
62
63 def __init__(self,
64 min_size: Optional[Union[int, None]] = None,
65 max_size: Optional[Union[int, None]] = None):
66 self.min_size = min_size
67 self.max_size = max_size
68 assert self.min_size is not None
69 assert self.max_size is not None
70
71 def __call__(self, results):
72 motion = results['motion']
73 length = len(motion)
74 crop_size = random.randint(self.min_size, self.max_size)
75 if length > crop_size:
76 idx = random.randint(0, length - crop_size)
77 motion = motion[idx:idx + crop_size]
78 results['motion_length'] = crop_size
79 else:
80 results['motion_length'] = length
81 padding_length = self.max_size - min(crop_size, length)
82 if padding_length > 0:
83 D = motion.shape[1:]
84 padding_zeros = np.zeros((padding_length, *D), dtype=np.float32)
85 motion = np.concatenate([motion, padding_zeros], axis=0)
86 results['motion'] = motion
87 results['motion_shape'] = motion.shape
88 if length >= self.max_size and crop_size == self.max_size:
89 results['motion_mask'] = torch.ones(self.max_size).numpy()
90 else:
91 results['motion_mask'] = torch.cat(
92 (torch.ones(min(length, crop_size)),
93 torch.zeros(self.max_size - min(length, crop_size))),
94 dim=0).numpy()
95 assert len(motion) == self.max_size
96 return results
97
98 def __repr__(self):
99 repr_str = self.__class__.__name__ + f'(min_size={self.min_size}'
100 repr_str += f', max_size={self.max_size})'
101 return repr_str
102
103
104@PIPELINES.register_module()

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected