CoolFace
Apppublic

robometer/rewardeval_ui

sourceHugging Faceupdated 7mo agoView on Hugging Face
4likes
progress_policy_ranking.cpython-310.pyc72 linesDownload Raw Back to __pycache__
1o

2��ei+(�@svddlmZmZmZmZddlmZddlZddl	m3Z4ddlmZddl
mZddlmZe�ZGdd	�d	e�ZdS)5�)�Dict�List�Any�Optional)�cycleN)�defaultdict)�ProgressSample)�RFMBaseSampler)�6get_loggercs�eZdZdZ					ddedeeded	ed7eef8�fdd�
Zd
ee	e9effdd�Zdede	e10efd
ee	e11effdd�Z
ded
efdd�Zdd�Zdd�Z�ZS)�ProgressPolicyRankingSamplerz�Dataset that generates progress samples for policy ranking by selecting N trajectories per quality label for tasks with multiple quality labels.�N�T�num_examples_per_quality_pr�num_partial_successes�12frame_step�use_frame_steps�	max_taskscsnt�jdi|��||_||_||_||_||_t�dt	|j13��d��|��|_t�dt	|j��d��dS)Nz.ProgressPolicyRankingSampler initialized with z
 trajectoriesz14Generated z sample indices�)
�super�__init__rrrrr�logger�info�len�robot_trajectories�_generate_all_sample_indices�sample_indices)�selfrrrrr�kwargs��	__class__r�I/scr/aliang80/reward_fm/rfm/data/samplers/eval/progress_policy_ranking.pyrs	15z%ProgressPolicyRankingSampler.__init__�returnc	Cs�d}|jr|j|jd}|�d�du}tdd��}|jD]4}|j|}|d}|rD|�d�}|durCtt|�d�}|||�|�q|d	}	|||	�|�qd16d�|��D�}17|r_dnd
}t�	dt18|19��d|���|jdur�|jdkr�t|20���}|j
�|�t|d|j��}21t�	dt22|23��d|j�d��g}
g}t|24���D]�\}}|�r|j}g}t|���D]}t||�}|r�|�|�q�g}t|�D](}t25|�|kr�q�|s�tdd�|D��r�q�q�|j
�|�}|�|�|�|�q�|D]O}|j|}|
�|�||��|�|�q�t|���D]3}	||	}t|�}t|jt26|��}|j
�||�}|D]}|j|}|
�|�||��|�|��q0�qq�t�	dt27|
��dt28|29��d��t�	d|���|
S)a�Generate sample indices by selecting tasks with multiple quality labels/partial_success values and sampling N trajectories per group.30 31        For non-RoboArena: Groups by task and quality_label.32        For RoboArena: Groups by task and partial_success values.33 34        If use_frame_steps=True, generates subsequence samples like reward_alignment (0:frame_step, 0:2*frame_step, etc.).35        If use_frame_steps=False, generates one sample per trajectory (whole trajectory).36        Fr�partial_successNcSstt�S�N)r�listrrrr �<lambda>7szKProgressPolicyRankingSampler._generate_all_sample_indices.<locals>.<lambda>�task��
quality_labelcSs"i|]
\}}t|�dkr||�qS)r
)r)�.0r&�key_to_trajsrrr �37<dictcomp>Is38��zMProgressPolicyRankingSampler._generate_all_sample_indices.<locals>.<dictcomp>zpartial_success valueszquality labelszFound z tasks with multiple zLimited to z tasks (max_tasks=�)css�|]}|VqdSr#r)r)�lstrrr �	<genexpr>ps�zLProgressPolicyRankingSampler._generate_all_sample_indices.<locals>.<genexpr>zSampled z samples across z taskszSampled trajectory indices: )r�dataset�getr�round�float�append�itemsrrrr�sorted�
_local_random�shuffle�dictr�keysr�all�choice�remove�extend� _generate_indices_for_trajectory�minr�sample)rZis_roboarenaZ39first_trajZtask_to_key_to_trajs�traj_idx�trajr&Zpartial_success_valr"�qualityZtasks_with_multiple_valuesZdataset_type_strZ40tasks_listrZall_sampled_traj_indicesr*Znum_to_sample_totalZavailable_listsZtraj_indicesZsampled_traj_indices�available_indices�sampled_idx�
num_to_samplerrr r&s�414243��44�454647�� z9ProgressPolicyRankingSampler._generate_all_sample_indicesrArBc48Cs||d}g}|jr.t|j|d|j�D]}tt|��}|�||||d|ddd��q|S|�||d|ddd��|S)49z�Generate sample indices for a single trajectory.50 51        Args:52            traj_idx: Index of the trajectory in the dataset53            traj: Trajectory dictionary54 55        Returns:56            List of sample index dictionaries57        �58num_framesr
�frames�idT)rA�
frame_indicesrG�59video_pathrIrF)rArKrIrN)r�rangerr$r3)rrArBrG�indices�end_idxrJrrr r>�s*6061���z=ProgressPolicyRankingSampler._generate_indices_for_trajectory�sample_idx_infoc62Cs�|d}|�dd�}|j|}|r;|d}|d}|d|d|d|d	|d63|r.|dndd
�}|j|||d�}n|d|d|d|d	|d64d�}|j||d�}t|d�}	|	S)z8Generate a single progress sample from trajectory index.rArTrJrGr(�data_sourcer&rIrK�����r)r(rPr&rIrKr)rBrJ�metadata)r(rPr&rIrK)rBrR)�65trajectoryN)r0r/�_get_traj_from_datar)66rrOrArrBrJrGrRrSr@rrr �_generate_sample_from_indices�s<67�	���68z:ProgressPolicyRankingSampler._generate_sample_from_indicescCs69t|j�Sr#)rr)rrrr �__len__�s70z$ProgressPolicyRankingSampler.__len__cCs|�|j|�Sr#)rUr)r�idxrrr �__getitem__�sz(ProgressPolicyRankingSampler.__getitem__)rNr
TN)�__name__�71__module__�__qualname__�__doc__�intr�boolrrr�strrrr>r8rrUrVrX�
__classcell__rrrr r
s.������*m$,r)�typingrrrr�	itertoolsr�numpy�np�collectionsr�rfm.data.dataset_typesr�rfm.data.samplers.baser	�rfm.utils.loggerr72rrrrrr �<module>s