o
    xiq                     @  sn   d dl mZ d dlmZ d dlZd dlmZ d dlm	Z	 d dl
mZ d dlZ		ddddZ	dd ddZdS )!    )annotations)AnyN)tqdm)Results)ClassificationPredictorresultr   
model_namestrtablewandb.Table | Noneoriginal_imagenp.array | Nonec              	     s   |  d} | j}|j  dd | j D  |dur"t|nt| j	}|| t
|j |j fddt|jD fddt|jD  fddtD | jg}|dura|j|  |S  |fS )	zkPlot classification prediction results to a `wandb.Table` if the table is passed otherwise return the data.cpuc                 S  s   i | ]\}}t |t|qS  )intr	   ).0kvr   r   f/home/ubuntu/.local/lib/python3.10/site-packages/wandb/integration/ultralytics/classification_utils.py
<dictcomp>   s    z3plot_classification_predictions.<locals>.<dictcomp>Nc                      g | ]} t | qS r   r   r   	class_idxclass_id_to_labelr   r   
<listcomp>"       z3plot_classification_predictions.<locals>.<listcomp>c                   r   r   r   r   )probabilities_listr   r   r   #   r   c                   s   i | ]\}} t | |qS r   r   )r   r   probabilityr   r   r   r   $   s    )toprobsdatanumpytolistnamesitemswandbImageorig_imgr   top1top1conflisttop5	enumeratespeedadd_data)r   r   r
   r   probabilities	table_rowr   )r   r   r   plot_classification_predictions   s.   




r3   
dataloaderr   	predictorr   wandb.Tablemax_validation_batchesr   epoch
int | Nonereturnc              
   C  s4  d}t | j| j }t||}t| D ]\}}	dd t|	d  dD }
|	d   }t	t
|d|d  d	| d
}|D ]M}zB||
| d }t|||
| d\}}||g|dd  }|d|||   |durs|g| n|}|g| }|j|  |d7 }W q@ ty   Y q@w |d |kr |S q|S )z/Plot classification results to a `wandb.Table`.r   c                 S  s   g | ]}|qS r   r   )r   imager   r   r   r   =   s    z:plot_classification_validation_results.<locals>.<listcomp>img)r            clsz$Generating Visualizations for batch-r?   /)desc)r   Nr>   )lendataset
batch_sizeminr.   np	transposer#   r$   r   ranger3   insertr0   	Exception)r4   r   r5   r
   r7   r8   data_idxnum_dataloader_batches	batch_idxbatchimage_batchground_truthprogress_bar_result_iterableimg_idxprediction_resultr   r2   r   r   r   &plot_classification_validation_results0   s@   	




rU   )NN)r   r   r   r	   r
   r   r   r   )N)r4   r   r   r	   r5   r   r
   r6   r7   r   r8   r9   r:   r6   )
__future__r   typingr   r#   rG   	tqdm.autor   ultralytics.engine.resultsr    ultralytics.models.yolo.classifyr   r'   r3   rU   r   r   r   r   <module>   s    )