o
    7wix                     @   s   d dl Zd dlZd dlmZ d dlm  mZ G dd dejZ	G dd dejZ
edkrZed  ejd ZejddZeeZe
d	d
dd
dZe  eeZeej dS dS )    Nc                       s,   e Zd Z fddZdd Zdd Z  ZS )DropStripesc                    s0   t t|   |dv sJ || _|| _|| _dS )zDrop stripes. 

        Args:
          dim: int, dimension along which to drop
          drop_width: int, maximum width of stripes to drop
          stripes_num: int, how many stripes to drop
        )      N)superr   __init__dim
drop_widthstripes_num)selfr   r   r	   	__class__ V/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchlibrosa/augmentation.pyr   	   s
   
zDropStripes.__init__c                 C   sV   |  dksJ | jdu r|S |jd }|j| j }t|D ]
}| || | q|S )z4input: (batch_size, channels, time_steps, freq_bins)   Fr   )
ndimensiontrainingshaper   rangetransform_slice)r
   input
batch_sizetotal_widthnr   r   r   forward   s   

zDropStripes.forwardc                 C   s   t | jD ]B}tjd| jddd }tjd|| ddd }| jdkr3d|dd||| ddf< q| jdkrGd|dddd||| f< qdS )z$e: (channels, time_steps, freq_bins)r   )   )lowhighsizer   Nr   )r   r	   torchrandintr   r   )r
   er   _distancebgnr   r   r   r   +   s   
 
zDropStripes.transform_slice)__name__
__module____qualname__r   r   r   __classcell__r   r   r   r   r      s    r   c                       s$   e Zd Z fddZdd Z  ZS )SpecAugmentationc                    s2   t t|   td||d| _td||d| _dS )a  Spec augmetation. 
        [ref] Park, D.S., Chan, W., Zhang, Y., Chiu, C.C., Zoph, B., Cubuk, E.D. 
        and Le, Q.V., 2019. Specaugment: A simple data augmentation method 
        for automatic speech recognition. arXiv preprint arXiv:1904.08779.

        Args:
          time_drop_width: int
          time_stripes_num: int
          freq_drop_width: int
          freq_stripes_num: int
        r   )r   r   r	   r   N)r   r(   r   r   time_dropperfreq_dropper)r
   time_drop_widthtime_stripes_numfreq_drop_widthfreq_stripes_numr   r   r   r   9   s   zSpecAugmentation.__init__c                 C   s   |  |}| |}|S )N)r)   r*   )r
   r   xr   r   r   r   O   s   

zSpecAugmentation.forward)r$   r%   r&   r   r   r'   r   r   r   r   r(   8   s    r(   __main__)
   r   i  @   )r   r2   r      )r+   r,   r-   r.   )numpynpr   torch.nnnntorch.nn.functional
functionalFModuler   r(   r$   manual_seedrandomRandomStaterandom_statenormalnp_dataTensorpt_dataspec_augmentertrainresultprintr   r   r   r   r   <module>   s$    0

