o
    Ï¯iO  ã                   @   sž  d dl Z d dlm  mZ d dlm  mZ d dlm	Z	 d dlm
Z
 d dlZd dlZd dlZd dlmZ d dlZd dlZd dlmZ dd„ Zdd	„ Zed
krÍeƒ ZejZdZde_de_g Zg Zg Zg Z g Z!g Z"e  #d¡Z#e	ej$ej%ejej&e#ej'ej(ej) *ej+¡dd	\Z,Z-e j.ee#dZ/de/v r·e/d Z0e/d Z1e2e3e1 4¡ ƒƒd   5d¡r±dd„ e1 4¡ D ƒZ1e, 6e1¡ ne, 6e/¡ e, 7e#¡ e, 8¡  e, 9¡ D ]Z:de:_;qÉe<e e› d¡ƒZ=g Z>g Z?g Z@g ZAg ZBg ZCe  D¡ ¯ ee=ƒD ]¢ZEeE Fdd¡ZGeHeGdƒZIe .eI¡ZJW d  ƒ n	1 sw   Y  ej.eEddd\ZKZLe  MeK¡ 7e#¡ZKeK Nd ¡eLdœZKeJd ZOej%dkr@d dlm
Z
 e
eOƒZOnd dlPmQZQ eQeOej%d ZOeeKeOe,e#ƒ\ZRZSZTZUZVZWe> XeR Y¡  Z¡ ¡ e? XeS Y¡  Z¡ ¡ e@ XeT Y¡  Z¡ ¡ eA XeU Y¡  Z¡ ¡ eB XeV Y¡  Z¡ ¡ eC XeW Y¡  Z¡ ¡ qîW d  ƒ n	1 sœw   Y  e  [e>¡ZRe  [e?¡ZSeBd  ZVeVeR eS \¡   Y¡  Z¡ Z]e] \¡  Y¡  Z¡ Z^ee^ƒZ_e`e_ƒ dS dS )!é    N)Úcreate_model)Útokenize)Útqdm)Ú
parse_argsc                 C   sf   || d |ƒ}t j|dd}|d ||d}t j|dd}| |¡}| |¡}|||||j ¡ |j ¡ fS )Néÿÿÿÿ)Údim)Údevice)ÚFÚ	normalizeÚaudio_transformÚtext_transformÚlogit_scale_aÚexpÚlogit_scale_t)ÚaudioÚtextÚmodelr   Úaudio_featuresÚtext_featuresÚaudio_features_mlpÚtext_features_mlp© r   úR/home/ubuntu/.local/lib/python3.10/site-packages/laion_clap/evaluate/eval_dcase.pyÚget_output_from_single_audio   s   

r   c                 C   sÈ   i }t  t  ttƒd ¡d¡ dd¡}t j| dd}t  ||k¡d }| ¡  	¡  
¡ }| ¡ d |d< t t |¡¡d |d< dD ]}t ||k ¡|d	|› < qAt t |d
k d|d  d¡¡|d< |S )Né   r   é   T)Ú
descendingÚ	mean_rankÚmedian_rank)r   r   é
   zR@r   g        zmAP@10)ÚtorchÚrepeat_interleaveÚarangeÚlenr   ÚviewÚargsortÚwhereÚdetachÚcpuÚnumpyÚmeanÚnpÚfloorÚmedian)Útext_to_audio_logitsÚmetricsÚground_truthÚrankingÚpredsÚkr   r   r   Úget_metrics!   s   "$r4   Ú__main__z /fsx/yusong/clotho_test_set/testTFÚcuda)Ú	precisionr   ÚjitÚforce_quick_geluÚopenai_model_cache_dirÚskip_params)Úmap_locationÚepochÚ
state_dictÚmodulec                 C   s"   i | ]\}}|t d ƒd… |“qS )zmodule.N)r#   )Ú.0r3   Úvr   r   r   Ú
<dictcomp>[   s   " rB   z/*.flacz.flacz.jsonÚri€»  )ÚsrÚmono)ÚwaveformÚsample_rater   Útransformer)Ú	tokenizer)Útmodel)ar    Útorch.nn.functionalÚnnÚ
functionalr	   Útorch.backends.cudnnÚbackendsÚcudnnÚ	open_clipr   r   ÚglobÚjsonÚlibrosar   r)   r+   ÚosÚlaion_clap.training.paramsr   r   r4   Ú__name__ÚargsÚ
pretrainedÚ
model_pathÚclotho_test_preprocessed_dirÚ	benchmarkÚdeterministicÚaudio_features_ensemble_allÚtext_features_ensemble_allÚaudio_features_mlp_ensemble_allÚtext_features_mlp_ensemble_allÚlogit_scale_a_ensemble_allÚlogit_scale_t_ensemble_allr   ÚamodelrJ   r7   Útorchscriptr9   ÚpathÚ
expanduserr:   r   Úclap_model_cfgÚloadÚ
checkpointÚstart_epochÚsdÚnextÚiterÚitemsÚ
startswithÚload_state_dictÚtoÚevalÚ
parametersÚparamÚrequires_gradÚsortedÚtest_file_listÚaudio_features_allÚtext_features_allÚaudio_features_mlp_allÚtext_features_mlp_allÚlogit_scale_a_allÚlogit_scale_t_allÚno_gradÚ	file_pathÚreplaceÚ	json_pathÚopenÚfÚ	json_datar   rD   Ú
from_numpyÚ	unsqueezer   Úlaion_clap.training.datarI   r   r   r   r   r   r   Úappendr'   r(   ÚcatÚtÚlogits_per_audioÚlogits_per_textr/   Úprintr   r   r   r   Ú<module>   s¼    


÷ÿ


ÿ
ÿèÿ

ÿ