o
    &j8                     @   s   d Z ddlmZmZmZ ddlZddlmZ	 ddlm
Z ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ G dd dejZdS )zxAugmenters that are collections of other augmenters.

List of augmenters:

    * :class:`RandAugment`

Added in 0.4.0.

    )print_functiondivisionabsolute_importN   )
parameters)random   )meta)
arithmetic)flip)pillike)sizec                       sL   e Zd ZdZdZ			d fdd		Zed
d Zedd Zdd Z	  Z
S )RandAugmenta  Apply RandAugment to inputs as described in the corresponding paper.

    See paper::

        Cubuk et al.

        RandAugment: Practical automated data augmentation with a reduced
        search space

    .. note::

        The paper contains essentially no hyperparameters for the individual
        augmentation techniques. The hyperparameters used here come mostly
        from the official code repository, which however seems to only contain
        code for CIFAR10 and SVHN, not for ImageNet. So some guesswork was
        involved and a few of the hyperparameters were also taken from
        https://github.com/ildoonet/pytorch-randaugment/blob/master/RandAugment/augmentations.py .

        This implementation deviates from the code repository for all PIL
        enhance operations. In the repository these use a factor of
        ``0.1 + M*1.8/M_max``, which would lead to a factor of ``0.1`` for the
        weakest ``M`` of ``M=0``. For e.g. ``Brightness`` that would result in
        a basically black image. This definition is fine for AutoAugment (from
        where the code and hyperparameters are copied), which optimizes
        each transformation's ``M`` individually, but not for RandAugment,
        which uses a single fixed ``M``. We hence redefine these
        hyperparameters to ``1.0 + S * M * 0.9/M_max``, where ``S`` is
        randomly either ``1`` or ``-1``.

        We also note that it is not entirely clear which transformations
        were used in the ImageNet experiments. The paper lists some
        transformations in Figure 2, but names others in the text too (e.g.
        crops, flips, cutout). While Figure 2 lists the Identity function,
        this transformation seems to not appear in the repository (and in fact,
        the function ``randaugment(N, M)`` doesn't seem to exist in the
        repository either). So we also make a best guess here about what
        transformations might have been used.

    .. warning::

        This augmenter only works with image data, not e.g. bounding boxes.
        The used PIL-based affine transformations are not yet able to
        process non-image data. (This augmenter uses PIL-based affine
        transformations to ensure that outputs are as similar as possible
        to the paper's implementation.)

    Added in 0.4.0.

    **Supported dtypes**:

    minimum of (
        :class:`~imgaug.augmenters.flip.Fliplr`,
        :class:`~imgaug.augmenters.size.KeepSizeByResize`,
        :class:`~imgaug.augmenters.size.Crop`,
        :class:`~imgaug.augmenters.meta.Sequential`,
        :class:`~imgaug.augmenters.meta.SomeOf`,
        :class:`~imgaug.augmenters.meta.Identity`,
        :class:`~imgaug.augmenters.pillike.Autocontrast`,
        :class:`~imgaug.augmenters.pillike.Equalize`,
        :class:`~imgaug.augmenters.arithmetic.Invert`,
        :class:`~imgaug.augmenters.pillike.Affine`,
        :class:`~imgaug.augmenters.pillike.Posterize`,
        :class:`~imgaug.augmenters.pillike.Solarize`,
        :class:`~imgaug.augmenters.pillike.EnhanceColor`,
        :class:`~imgaug.augmenters.pillike.EnhanceContrast`,
        :class:`~imgaug.augmenters.pillike.EnhanceBrightness`,
        :class:`~imgaug.augmenters.pillike.EnhanceSharpness`,
        :class:`~imgaug.augmenters.arithmetic.Cutout`,
        :class:`~imgaug.augmenters.pillike.FilterBlur`,
        :class:`~imgaug.augmenters.pillike.FilterSmooth`
    )

    Parameters
    ----------
    n : int or tuple of int or list of int or imgaug.parameters.StochasticParameter or None, optional
        Parameter ``N`` in the paper, i.e. number of transformations to apply.
        The paper suggests ``N=2`` for ImageNet.
        See also parameter ``n`` in :class:`~imgaug.augmenters.meta.SomeOf`
        for more details.

        Note that horizontal flips (p=50%) and crops are always applied. This
        parameter only determines how many of the other transformations
        are applied per image.


    m : int or tuple of int or list of int or imgaug.parameters.StochasticParameter or None, optional
        Parameter ``M`` in the paper, i.e. magnitude/severity/strength of the
        applied transformations in interval ``[0 .. 30]`` with ``M=0`` being
        the weakest. The paper suggests for ImageNet ``M=9`` in case of
        ResNet-50 and ``M=28`` in case of EfficientNet-B7.
        This implementation uses a default value of ``(6, 12)``, i.e. the
        value is uniformly sampled per image from the interval ``[6 .. 12]``.
        This ensures greater diversity of transformations than using a single
        fixed value.

        * If ``int``: That value will always be used.
        * If ``tuple`` ``(a, b)``: A random value will be uniformly sampled per
          image from the discrete interval ``[a .. b]``.
        * If ``list``: A random value will be picked from the list per image.
        * If ``StochasticParameter``: For ``B`` images in a batch, ``B`` values
          will be sampled per augmenter (provided the augmenter is dependent
          on the magnitude).

    cval : number or tuple of number or list of number or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        The constant value to use when filling in newly created pixels.
        See parameter `fillcolor` in
        :class:`~imgaug.augmenters.pillike.Affine` for details.

        The paper's repository uses an RGB value of ``125, 122, 113``.
        This implementation uses a single intensity value of ``128``, which
        should work better for cases where input images don't have exactly
        ``3`` channels or come from a different dataset than used by the
        paper.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.RandAugment(n=2, m=9)

    Create a RandAugment augmenter similar to the suggested hyperparameters
    in the paper.

    >>> aug = iaa.RandAugment(m=30)

    Create a RandAugment augmenter with maximum magnitude/strength.

    >>> aug = iaa.RandAugment(m=(0, 9))

    Create a RandAugment augmenter that applies its transformations with a
    random magnitude between ``0`` (very weak) and ``9`` (recommended for
    ImageNet and ResNet-50). ``m`` is sampled per transformation.

    >>> aug = iaa.RandAugment(n=(0, 3))

    Create a RandAugment augmenter that applies ``0`` to ``3`` of its
    child transformations to images. Horizontal flips (p=50%) and crops are
    always applied.

       r            N
deprecatedc                    s   |dkr|n|}t |}tj|dddddd}|| _|| _| |}	| ||}
|	|
fD ]
}|D ]}||_q1q-t	t
| jtj|	| dtj||
d| dg||||d	 d S )
Nr   m)r   NTF)Zvalue_rangeZtuple_to_uniformZlist_to_choiceZallow_floats)seed)Zrandom_orderr   )r   namerandom_statedeterministic)iarandomZRNGiapZhandle_discrete_param_m_cval_create_initial_augmenters_list_create_main_augmenters_listr   superr   __init__r	   
SequentialZderive_rng_ZSomeOf)selfnr   cvalr   r   r   r   rngZinitial_augsZ	main_augslstZ	augmenter	__class__ ^/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/imgaug/augmenters/collections.pyr!      s4   




zRandAugment.__init__c                 C   s8   t dtjtjtjtd|dddddddd	gS )
N      ?r      TZelementwiseF)percentZsample_independentlyZ	keep_sizeZlinear)interpolation)r   ZFliplrsizelibZKeepSizeByResizeZCropr   ZDivideZUniform)clsr   r*   r*   r+   r      s   
z+RandAugment._create_initial_augmenters_listc           	         s~  | j fdd  fdd} fdd}dd }fd	d
}tj}td}t tjddt	 t
jdd|| |dddtj|dt||ddddtjdt|d||ddddt||t||t||t|||d| ||id|d| ||id|d| |did|d| |didt
jdt |ddddd d!t t gS )"Nc                    s   |  }t j| |ddS NTr.   )r   ZMultiply)levelmaxvalZmaxval_norm)m_maxr*   r+   _float_parameter   s   zBRandAugment._create_main_augmenters_list.<locals>._float_parameterc                    s   t j | |ddS )NF)round)r   Z
Discretize)r4   r5   r7   r*   r+   _int_parameter  s   z@RandAugment._create_main_augmenters_list.<locals>._int_parameterc                    s*    | d}t t jdt |ddddS )Ng?      ?Tr.   g?gffffff?)r   ClipAdd
RandomSign)r4   Zfparamr9   r*   r+   _enhance_parameter  s
   
zDRandAugment._create_main_augmenters_list.<locals>._enhance_parameterc                 S   s   t j| |ddS r3   )r   ZSubtract)abr*   r*   r+   	_subtract  s   z;RandAugment._create_main_augmenters_list.<locals>._subtractc                     s(    |d< d|vrd|d< t j| i |S )NZ	fillcolorcenter)        rD   )r   ZAffine)argskwargs)r%   r*   r+   _affine  s   z9RandAugment._create_main_augmenters_list.<locals>._affineg333333?r   )cutoffr;   )pr   )r,   r,   )rotaterC      r   )Znb_bits   )rI   	thresholdx)ZshearygQ?)Ztranslate_percentr   g      ?TZconstant)r   ZsquaredZ	fill_moder%   )_M_MAXr   r>   npZrad2degr	   ZIdentityr   ZAutocontrastZEqualizer
   InvertZ	Posterizer<   ZSolarizeZEnhanceColorZEnhanceContrastZEnhanceBrightnessZEnhanceSharpnessZCutoutZ
FilterBlurZFilterSmooth)	r2   r   r%   r:   r?   rB   rG   Z_rnd_sZ	shear_maxr*   )r7   r%   r6   r+   r      s^   


z(RandAugment._create_main_augmenters_listc                 C   s   | d }|j | j| jgS )z=See :func:`~imgaug.augmenters.meta.Augmenter.get_parameters`.r   )r$   r   r   )r#   Zsomeofr*   r*   r+   get_parametersR  s   zRandAugment.get_parameters)r   r   r   NNr   r   )__name__
__module____qualname____doc__rP   r!   classmethodr   r   rS   __classcell__r*   r*   r(   r+   r      s     *

Vr   )rW   
__future__r   r   r   numpyrQ    r   r   r   r   r	   r
   r   r   r   r1   r"   r   r*   r*   r*   r+   <module>   s    	