| 
 | 1 | +from augmentor import *  | 
 | 2 | + | 
 | 3 | + | 
 | 4 | +def get_augmentation(  | 
 | 5 | +    is_train,  | 
 | 6 | +    box=None,  | 
 | 7 | +    missing=7,  | 
 | 8 | +    blur=7,  | 
 | 9 | +    lost=True,  | 
 | 10 | +    random=False,  | 
 | 11 | +    recompute=False,  | 
 | 12 | +    border=False,  | 
 | 13 | +    **kwargs  | 
 | 14 | +):  | 
 | 15 | +    augs = list()  | 
 | 16 | + | 
 | 17 | +    # Box  | 
 | 18 | +    if is_train:  | 
 | 19 | +        if box == 'noise':  | 
 | 20 | +            augs.append(  | 
 | 21 | +                NoiseBox(sigma=(1,3), dims=(3,13), margin=(1,3,3),  | 
 | 22 | +                         density=0.3, skip=0.1)  | 
 | 23 | +            )  | 
 | 24 | +        elif box == 'fill':  | 
 | 25 | +            augs.append(  | 
 | 26 | +                FillBox(dims=(3,13), margin=(1,3,3),  | 
 | 27 | +                        density=0.3, skip=0.1)  | 
 | 28 | +            )  | 
 | 29 | + | 
 | 30 | +    # Brightness & contrast purterbation  | 
 | 31 | +    augs.append(  | 
 | 32 | +        MixedGrayscale2D(  | 
 | 33 | +            contrast_factor=0.5,  | 
 | 34 | +            brightness_factor=0.5,  | 
 | 35 | +            prob=1, skip=0.3))  | 
 | 36 | + | 
 | 37 | +    # Missing section & misalignment  | 
 | 38 | +    to_blend = list()  | 
 | 39 | +    # Misalingments  | 
 | 40 | +    trans = Compose([Misalign((0, 3), margin=1),  | 
 | 41 | +                     Misalign((0, 8), margin=1),  | 
 | 42 | +                     Misalign((0,13), margin=1)])  | 
 | 43 | + | 
 | 44 | +    # Out-of-alignments  | 
 | 45 | +    slip = Compose([SlipMisalign((0, 3), interp=True, margin=1),  | 
 | 46 | +                    SlipMisalign((0, 8), interp=True, margin=1),  | 
 | 47 | +                    SlipMisalign((0,13), interp=True, margin=1)])  | 
 | 48 | +    to_blend.append(Blend([trans,slip], props=[0.7,0.3]))  | 
 | 49 | +    if is_train:  | 
 | 50 | +        to_blend.append(Blend([  | 
 | 51 | +            MisalignPlusMissing((2,8), value=0, random=random),  | 
 | 52 | +            MisalignPlusMissing((2,8), value=0, random=False)  | 
 | 53 | +        ]))  | 
 | 54 | +    else:  | 
 | 55 | +        to_blend.append(MisalignPlusMissing((2,8), value=0, random=False))  | 
 | 56 | +    if missing > 0:  | 
 | 57 | +        if is_train:  | 
 | 58 | +            to_blend.append(Blend([  | 
 | 59 | +                MixedMissingSection(maxsec=missing, individual=True, value=0, random=False),  | 
 | 60 | +                MixedMissingSection(maxsec=missing, individual=True, value=0, random=random),  | 
 | 61 | +                MissingSection(maxsec=missing, individual=False, value=0, random=random),  | 
 | 62 | +            ]))  | 
 | 63 | +        else:  | 
 | 64 | +            to_blend.append(  | 
 | 65 | +                MixedMissingSection(maxsec=missing, individual=True, value=0, random=False)  | 
 | 66 | +            )  | 
 | 67 | +    if lost:  | 
 | 68 | +        if is_train:  | 
 | 69 | +            to_blend.append(Blend([  | 
 | 70 | +                LostSection(1),  | 
 | 71 | +                LostPlusMissing(value=0, random=random),  | 
 | 72 | +                LostPlusMissing(value=0, random=False)  | 
 | 73 | +            ]))  | 
 | 74 | +    augs.append(Blend(to_blend))  | 
 | 75 | + | 
 | 76 | +    # Out-of-focus  | 
 | 77 | +    if blur > 0:  | 
 | 78 | +        augs.append(MixedBlurrySection(maxsec=blur))  | 
 | 79 | + | 
 | 80 | +    # Warping  | 
 | 81 | +    if is_train:  | 
 | 82 | +        augs.append(Warp(skip=0.3, do_twist=False, rot_max=45.0, scale_max=1.1))  | 
 | 83 | + | 
 | 84 | +    # Flip & rotate  | 
 | 85 | +    augs.append(FlipRotate())  | 
 | 86 | + | 
 | 87 | +    # Create border  | 
 | 88 | +    if border:  | 
 | 89 | +        augs.append(Border())  | 
 | 90 | + | 
 | 91 | +    # Recompute connected components  | 
 | 92 | +    if recompute:  | 
 | 93 | +        augs.append(Label())  | 
 | 94 | + | 
 | 95 | +    return Compose(augs)  | 
0 commit comments