ó
    Ã ÖjÍ  ã                   óv   • S r SSKrSSKrSSKJr  SSKJrJr  SrSr	 " S S\R                  5      rS rS
S	 jrg)z0SpecAugment transforms for HFTT/BEATs mel input.é    N)ÚFrequencyMaskingÚTimeMasking)é   é   )é   r   c                   óh   ^ • \ rS rSrSrSr  SU 4S jjr\S 5       rSS jr	S r
SS jrS	 rS
rU =r$ )ÚStructuredSpecAugé   a3  Per-sample SpecAugment on [B, 1, F, T] with mean fill.

Masks are drawn independently per sample and filled with that sample's mean, so they never
create loud horizontal bands (wheeze-like) or vertical stripes (crackle-like).
protect_all=True: freq masks never overlap the wheeze protect band, for every sample.
sound_aware=True with labels (label-conditional, leaks labels through the mask pattern):
  - wheeze samples: freq masks are narrower and never overlap the wheeze protect band
  - crackle samples: time masks are capped near the crackle transient width
Tc
                 ój  >• [         T
U ]  5         [        U5      [        U5      sU l        U l        [        U5      [        U5      sU l        U l        [        U5      U l        [        U	5      U l	        [        US   5      [        US   5      4U l
        [        U5      [        U5      sU l        U l        g )Nr   r   )ÚsuperÚ__init__ÚintÚfreq_mask_paramÚtime_mask_paramÚn_freqÚn_timeÚboolÚsound_awareÚprotect_allÚprotect_bandÚwheeze_freq_maskÚcrackle_time_mask)Úselfr   r   r   r   r   r   r   r   r   Ú	__class__s             €Ú-/root/disk_3tb/ICBHI_2017_F12/util/specaug.pyr   ÚStructuredSpecAug.__init__   s™   ø€ ä‰ÑÔÜ58¸Ó5IÌ3ÈÓK_Ð2ˆÔ˜dÔ2Ü#& v£;´°F³Ð ˆŒ�T”[Ü Ó,ˆÔÜ Ó,ˆÔÜ  ¨a¡Ó1´3°|ÀA±Ó3GÐHˆÔÜ8;Ð<LÓ8MÌsÐSdÓOeÐ5ˆÔ˜tÕ5ó    c                 óv   • U S:”  a2  [        [        R                  " SU S-   S5      R                  5       5      $ S$ )zUniform integer in [0, high].r   r   ©r   )r   ÚtorchÚrandintÚitem)Úhighs    r   Ú	_rand_intÚStructuredSpecAug._rand_int#   s6   € ð @DÀa»xŒs”5—=’=  D¨1¡H¨dÓ3×8Ñ8Ó:Ó;ÐNÈQÐNr   c                 ó\  • U R                  [        X!5      5      nUS:X  a  gUc  U R                  X-
  5      U4$ Uu  pV[        XT-
  S-   S5      n[        X-
  U-
  S5      nXx-   S:X  a  g[        [        R
                  " SXx-   S5      R                  5       5      n	X—:  a  X”4$ US-   X—-
  -   U4$ )zQSample (start, width); with avoid=(lo, hi) the span stays fully outside [lo, hi].r   )r   r   r   r   )r$   ÚminÚmaxr   r    r!   r"   )
r   ÚsizeÚ	max_widthÚavoidÚwidthÚloÚhiÚn_belowÚn_aboveÚks
             r   Ú_spanÚStructuredSpecAug._span(   s¸   € à—‘œs 9Ó3Ó4ˆØ�A‹:ØØ‰=Ø—>‘> $¡,Ó/°Ð6Ð6Ø‰ˆÜ�b‘j 1‘n aÓ(ˆÜ�d‘l RÑ'¨Ó+ˆØÑ Ó!ØÜ”—’˜a Ñ!2°DÓ9×>Ñ>Ó@ÓAˆØ›[�ˆzÐM¨r°A©v¸¹Ñ/EÀuÐ.MÐMr   c                 ó&   • U R                  US 5      $ ©N)Úforward_with_labels)r   Úxs     r   ÚforwardÚStructuredSpecAug.forward7   s   € Ø×'Ñ'¨¨4Ó0Ð0r   c           
      ó`  • UR                  5       S:w  a-  [        SR                  [        UR                  5      5      5      eUR                  u  p4pVU R
                  =(       a    US LnU(       a,  UR                  5       R                  5       R                  5       OS /U-  nUR                  5       n[        U5       GHm  u  pšU=(       a    [        U
5      [        ;   nU=(       a    [        U
5      [        ;   nX   R                  5       nU(       a   [        U R                   U R"                  5      OU R                   nU(       a   [        U R$                  U R&                  5      OU R$                  n[)        U R*                  5       HV  nU R-                  X^U(       d  U R.                  (       a  U R0                  OS 5      u  nnU(       d  ME  XØU	S S 2UUU-   2S S 24'   MX     [)        U R2                  5       H1  nU R-                  Xo5      u  nnU(       d  M   XØU	S S 2S S 2UUU-   24'   M3     GMp     U$ )Né   z.StructuredSpecAug expects [B, 1, F, T], got {})ÚdimÚ
ValueErrorÚformatÚtupleÚshaper   ÚdetachÚcpuÚtolistÚcloneÚ	enumerater   ÚWHEEZE_LABELSÚCRACKLE_LABELSÚmeanr'   r   r   r   r   Úranger   r2   r   r   r   )r   r7   ÚlabelsÚBÚ_ÚFÚTÚ
use_labelsÚoutÚbÚlabÚwheezeÚcrackleÚfillÚf_maxÚt_maxÚsÚws                     r   r6   Ú%StructuredSpecAug.forward_with_labels:   sµ  € Ø�5‰5‹7�a‹<ÜÐM×TÑTÔUZÐ[\×[bÑ[bÓUcÓdÓeÐeØ—W‘W‰
ˆˆaØ×%Ñ%×<¨&¸Ð*<ˆ
Þ3=�—‘“×$Ñ$Ó&×-Ñ-Ô/ÀDÀ6ÈAÁ:ˆØ�g‰g‹iˆÜ ×'‰FˆAØ×=¤C¨£H´Ñ$=ˆFØ ×?¤S¨£X´Ñ%?ˆGØ‘4—9‘9“;ˆDÞHN”C˜×,Ñ,¨d×.CÑ.CÔDÐTX×ThÑThˆEÞIP”C˜×,Ñ,¨d×.DÑ.DÔEÐVZ×VjÑVjˆEÜ˜4Ÿ;™;Ö'�Ø—z‘z !Æ&ÈD×L\×L\¨D×,=Ò,=ÐcgÓh‘��1ß�1Ø,0˜š1˜a  A¡˜g¢qÐ(Ó)ñ (ô ˜4Ÿ;™;Ö'�Ø—z‘z !Ó+‘��1ß�1Ø,0˜š1ša  1 q¡5 Ð(Ó)ô (ñ (ð ˆ
r   c                 óè   • SR                  U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  U R                  5	      $ )Nzrfreq={}x{}, time={}x{}, sound_aware={}, protect_all={}, protect_band={}, wheeze_freq_mask={}, crackle_time_mask={})
r>   r   r   r   r   r   r   r   r   r   )r   s    r   Ú
extra_reprÚStructuredSpecAug.extra_reprQ   sc   € ð'ß(.©¨t¯{©{¸D×<PÑ<PÐRV×R]ÑR]Ð_c×_sÑ_sØ/3×/?Ñ/?À×AQÑAQÐSW×SdÑSdØ/3×/DÑ/DÀd×F\ÑF\ó)^ð	^r   )	r   r   r   r   r   r   r   r   r   )	é   é#   r   r   F©é   é<   é   rc   Fr5   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Úexpects_freq_timer   Ústaticmethodr$   r2   r8   r6   r\   Ú__static_attributes__Ú__classcell__)r   s   @r   r	   r	      sN   ø† ñð Ðà_dØ\a÷fð ñOó ðOôNò1ô÷.^ð ^r   r	   c                 óŒ   • U (       d  0 $ [        U 5       n[        R                  " U5      sS S S 5        $ ! , (       d  f       g = fr5   )ÚopenÚjsonÚload)ÚpathÚfs     r   Ú_load_statsrs   X   s(   € ÞØˆ	Ü	ˆdŒ�qÜ�yŠy˜‹|÷ 
��ús	   •5µ
Ac           	      ó<  • AU S:X  a5  [         R                  " [        SS9[        SS9[        SS9[        SS95      $ U S:X  a
  [	        5       $ U S;   a=  [        U5      n[	        U S:H  U S	:H  UR                  S
S5      UR                  SS5      S9$ [        SU  35      e)a  Build a SpecAugment module.

icbhi_ast_sup: legacy torchaudio masks (zero fill, one mask per batch).
structured:    StructuredSpecAug, per-sample masks with mean fill.
sound_aware:   structured + freq masks kept out of the wheeze protect band for every sample.
sound_aware_labeled: label-conditional masks (wheeze protect band, short crackle time masks).
    The mask pattern reveals the label, so the model learns "unmasked = abnormal" and
    specificity collapses at test time; kept only to reproduce that failure.
Úicbhi_ast_supr^   )r   r_   )r   Ú
structured)r   Úsound_aware_labeledrw   r   Úwheeze_protect_bandr`   r   rc   )r   r   r   r   zUnknown specaug policy: )ÚnnÚ
Sequentialr   r   r	   rs   Úgetr=   )ÚpolicyÚmaskÚ
stats_pathÚstatss       r   Úbuild_specaug_transformr€   _   s·   € ð 	Ø�Ó Ü�}Š}Ü¨RÑ0Ü¨RÑ0Ü¨Ñ+Ü¨Ñ+ó	
ð 	
ð �ÓÜ Ó"Ð"ØÐ7Ó7Ü˜JÓ'ˆÜ ØÐ"7Ñ7Ø -Ñ/ØŸ™Ð#8¸'ÓBØ#Ÿi™iÐ(;¸QÓ?ñ	
ð 	
ô Ð/°¨xÐ8Ó
9Ð9r   )ru   rH   N)rh   ro   r    Útorch.nnry   Útorchaudio.transformsr   r   rG   rF   ÚModuler	   rs   r€   © r   r   Ú<module>r…      s=   ðÙ 6Û ã Ý ß ?à€Ø€ôI^˜Ÿ	™	ô I^òXõ:r   