MixUp as described in: Hongyi Zhang, Moustapha Cisse, Yann N. Dauphin, David Lopez-Paz. mixup: Beyond Empirical Risk Minimization, ICLR 2018 Class derived from :py:class:`monai.transforms.Mixer`. See corresponding documentation for details on the constructor parameters.
| 63 | |
| 64 | |
| 65 | class MixUp(Mixer): |
| 66 | """MixUp as described in: |
| 67 | Hongyi Zhang, Moustapha Cisse, Yann N. Dauphin, David Lopez-Paz. |
| 68 | mixup: Beyond Empirical Risk Minimization, ICLR 2018 |
| 69 | |
| 70 | Class derived from :py:class:`monai.transforms.Mixer`. See corresponding |
| 71 | documentation for details on the constructor parameters. |
| 72 | """ |
| 73 | |
| 74 | def apply(self, data: torch.Tensor): |
| 75 | weight, perm, _ = self._params |
| 76 | nsamples, *dims = data.shape |
| 77 | if len(weight) != nsamples: |
| 78 | raise ValueError(f"Expected batch of size: {len(weight)}, but got {nsamples}") |
| 79 | |
| 80 | if len(dims) not in [3, 4]: |
| 81 | raise ValueError("Unexpected number of dimensions") |
| 82 | |
| 83 | mixweight = weight[(Ellipsis,) + (None,) * len(dims)] |
| 84 | return mixweight * data + (1 - mixweight) * data[perm, ...] |
| 85 | |
| 86 | def __call__(self, data: torch.Tensor, labels: torch.Tensor | None = None, randomize=True): |
| 87 | data_t = convert_to_tensor(data, track_meta=get_track_meta()) |
| 88 | labels_t = data_t # will not stay this value, needed to satisfy pylint/mypy |
| 89 | if labels is not None: |
| 90 | labels_t = convert_to_tensor(labels, track_meta=get_track_meta()) |
| 91 | if randomize: |
| 92 | self.randomize() |
| 93 | if labels is None: |
| 94 | return convert_to_dst_type(self.apply(data_t), dst=data)[0] |
| 95 | |
| 96 | return ( |
| 97 | convert_to_dst_type(self.apply(data_t), dst=data)[0], |
| 98 | convert_to_dst_type(self.apply(labels_t), dst=labels)[0], |
| 99 | ) |
| 100 | |
| 101 | |
| 102 | class CutMix(Mixer): |
no outgoing calls
searching dependent graphs…