Scheaven
2021-09-18 291deeb1fcf45dbf39a24aa72a213ff3fd6b3405
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
U
©r_dã@s”dZddddgZddlZddlZddlmZddlZddlm    Z    d    d
l
m Z m Z Gd d„de ƒZGd d„de ƒZGd d„de ƒZGdd„de ƒZdS)z9
@author:  liaoxingyu
@contact: sherlockliao01@gmail.com
ÚToTensorÚ RandomErasingÚ RandomPatchÚAugMixéN)Údeque)ÚImageé)Ú    to_tensorÚaugmentations_reidc@s eZdZdZdd„Zdd„ZdS)ra Convert a ``PIL Image`` or ``numpy.ndarray`` to tensor.
 
    Converts a PIL Image or numpy.ndarray (H x W x C) in the range
    [0, 255] to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 255.0]
    if the PIL Image belongs to one of the modes (L, LA, P, I, F, RGB, YCbCr, RGBA, CMYK, 1)
    or if the numpy.ndarray has dtype = np.uint8
 
    In the other cases, tensors are returned without scaling.
    cCst|ƒS)z
        Args:
            pic (PIL Image or numpy.ndarray): Image to be converted to tensor.
 
        Returns:
            Tensor: Converted image.
        )r    )ÚselfÚpic©r ú./data/transforms/transforms.pyÚ__call__szToTensor.__call__cCs |jjdS)Nz())Ú    __class__Ú__name__)r r r rÚ__repr__(szToTensor.__repr__N)rÚ
__module__Ú __qualname__Ú__doc__rrr r r rrs
 
c@s0eZdZdZddddddfdd    „Zd
d „Zd S) rað Randomly selects a rectangle region in an image and erases its pixels.
        'Random Erasing Data Augmentation' by Zhong et al.
        See https://arxiv.org/pdf/1708.04896.pdf
    Args:
        probability: The probability that the Random Erasing operation will be performed.
        sl: Minimum proportion of erased area against input image.
        sh: Maximum proportion of erased area against input image.
        r1: Minimum aspect ratio of erased area.
        mean: Erasing value.
    çà?g{®Gáz”?gš™™™™™Ù?g333333Ó?éÿ)g1™*•Ôß?g•eˆc]ÜÞ?gú~j¼t“Ü?cCs"||_||_||_||_||_dS©N)Ú probabilityÚmeanÚslÚshÚr1)r rrrrrr r rÚ__init__8s
zRandomErasing.__init__c
Cs˜tj|tjd ¡}t dd¡|jkr*|StdƒD]^}|jd|jd}t |j    |j
¡|}t |j d|j ¡}t t t ||¡ƒƒ}t t t ||¡ƒƒ}||jdkr2||jdkr2t d|jd|¡}t d|jd|¡}    |jddkrf|jd||||…|    |    |…df<|jd||||…|    |    |…df<|jd||||…|    |    |…df<n$|jd||||…|    |    |…df<|Sq2|S)N©Údtyperrédéé)ÚnpÚasarrayÚfloat32ÚcopyÚrandomÚuniformrÚrangeÚshaperrrÚintÚroundÚmathÚsqrtÚrandintr)
r ÚimgÚattemptÚareaÚ target_areaÚ aspect_ratioÚhÚwÚx1Úy1r r rr?s&$$&$
zRandomErasing.__call__N©rrrrrrr r r rr,s c@s2eZdZdZddd„Zd    d
„Zd d „Zd d„ZdS)raeRandom patch data augmentation.
    There is a patch pool that stores randomly extracted pathces from person images.
    For each input image, RandomPatch
        1) extracts a random patch and stores the patch in the patch pool;
        2) randomly selects a patch from the patch pool and pastes it on the
           input (at random position) to simulate occlusion.
    Reference:
        - Zhou et al. Omni-Scale Feature Learning for Person Re-Identification. ICCV, 2019.
        - Zhou et al. Learning Generalisable Omni-Scale Representations
          for Person Re-Identification. arXiv preprint, 2019.
    réPÃr!ç{®Gáz„?皙™™™™¹?c        Cs:||_||_||_||_||_||_t|d|_||_dS)N)Úmaxlen)    Ú prob_happenÚpatch_min_areaÚpatch_max_areaÚpatch_min_ratioÚ prob_rotateÚprob_flip_leftrightrÚ    patchpoolÚmin_sample_size)    r r?Z pool_capacityrFr@rArBrCrDr r rrfs zRandomPatch.__init__c    CsŠ||}tdƒD]t}t |j|j¡|}t |jd|j¡}ttt     ||¡ƒƒ}ttt     ||¡ƒƒ}||kr||kr||fSqdS)Nr!gð?)NN)
r*r(r)r@rArBr,r-r.r/)    r ÚWÚHr3r2r4r5r6r7r r rÚ generate_whvs zRandomPatch.generate_whcCsFt dd¡|jkr| tj¡}t dd¡|jkrB| t dd¡¡}|S)Nrriöÿÿÿé
)    r(r)rDÚ    transposerÚFLIP_LEFT_RIGHTrCÚrotater0)r Úpatchr r rÚtransform_patchs
 zRandomPatch.transform_patchc Cst|tjƒrt | tj¡¡}|j\}}| ||¡\}}|dk    rŽ|dk    rŽt     
d||¡}t     
d||¡}|  ||||||f¡}|j   |¡t|j ƒ|jkr¢|St     dd¡|jkr¸|St     |j d¡d}    |    j\}
} t     
d||
¡}t     
d|| ¡}| |    ¡}    | |    ||f¡|S)Nrr)Ú
isinstancer$ÚndarrayrÚ    fromarrayÚastypeÚuint8ÚsizerIr(r0ÚcroprEÚappendÚlenrFr)r?ÚsamplerOÚpaste) r r1rGrHr7r6r8r9Z    new_patchrNZpatchWZpatchHr r rrˆs( 
 
 
zRandomPatch.__call__N)rr;r!r<rr=rr)rrrrrrIrOrr r r rrYs þ
 c@s"eZdZdZd
dd„Zdd„Zd    S) ra Perform AugMix augmentation and compute mixture.
    Args:
        aug_prob_coeff: Probability distribution coefficients.
        mixture_width: Number of augmentation chains to mix per augmented example.
        mixture_depth: Depth of augmentation chains. -1 denotes stochastic depth in [1, 3]'
        severity: Severity of underlying augmentation operators (between 1 to 10).
    rr#éÿÿÿÿcCs"||_||_||_||_t|_dSr)Úaug_prob_coeffÚ mixture_widthÚ mixture_depthÚseverityr
Úaug_list)r r\r]r^r_r r rr°s
zAugMix.__init__c Cst tj |jg|j¡¡}t tj |j|j¡¡}tj|tjd ¡}t     |¡}|j
d|j
d}}t |jƒD]‚}t   | ¡ tj¡¡}|jdkrœ|jn tj dd¡}    t |    ƒD]$}
tj |j¡} | ||j||fƒ}q²|||tj|tjd7}qrd||||} | S)zwPerform AugMix augmentations and compute mixture.
        Returns:
          mixed: Augmented and mixed image.
        rrré)r$r&r(Ú    dirichletr\r]Úbetar%r'Ú
zeros_liker+r*rrRrSrTr^r0Úchoicer`r_) r ÚimageÚwsÚmZmixr6r7ÚiZ    image_augÚdepthÚ_ÚopÚmixedr r rr·s ÿ
 zAugMix.__call__N)rr#r[rr:r r r rr§s
)rÚ__all__r.r(Ú collectionsrÚnumpyr$ÚPILrÚ
functionalr    r
Úobjectrrrrr r r rÚ<module>s   -N