| 782 | return img |
| 783 | |
| 784 | def _apply_basic(self, img, mixing_weights, m): |
| 785 | # This is a literal adaptation of the paper/official implementation without normalizations and |
| 786 | # PIL <-> Numpy conversions between every op. It is still quite CPU compute heavy compared to the |
| 787 | # typical augmentation transforms, could use a GPU / Kornia implementation. |
| 788 | img_shape = img.size[0], img.size[1], len(img.getbands()) |
| 789 | mixed = np.zeros(img_shape, dtype=np.float32) |
| 790 | for mw in mixing_weights: |
| 791 | depth = self.depth if self.depth > 0 else np.random.randint(1, 4) |
| 792 | ops = np.random.choice(self.ops, depth, replace=True) |
| 793 | img_aug = img # no ops are in-place, deep copy not necessary |
| 794 | for op in ops: |
| 795 | img_aug = op(img_aug) |
| 796 | mixed += mw * np.asarray(img_aug, dtype=np.float32) |
| 797 | np.clip(mixed, 0, 255., out=mixed) |
| 798 | mixed = Image.fromarray(mixed.astype(np.uint8)) |
| 799 | return Image.blend(img, mixed, m) |
| 800 | |
| 801 | def __call__(self, img): |
| 802 | mixing_weights = np.float32(np.random.dirichlet([self.alpha] * self.width)) |