a neR*@sddlZddlZddlZddlmZmZddlmZ ddl m Z m Z m Z ddlmZddlmZmZddlmZGd d d eZGd d d eZd dZddZdS)N)HFlipTransform TransformList) functional)RandomRotationRotationTransformapply_transform_gens)detector_postprocess)DatasetMapperTTAGeneralizedRCNNWithTTA)HFlipConvertercs(eZdZfddZfddZZS)DensePoseDatasetMapperTTAcstj|d|jjj|_dS)N)cfg)super__init__TESTAUGROTATION_ANGLESangles)selfr __class__Y/home/alin0222/detectron2/projects/DensePose/densepose/modeling/test_time_augmentation.pyrsz"DensePoseDatasetMapperTTA.__init__c stj|d}|dddd}|jD]~}t|dd}t|gt|\}}t t | ddd}t |} t|dd jdg|j| d <|| d<|| q*|S) N) dataset_dictimager rT)angleexpand transforms)r__call__permutenumpyrrrnpcopytorch from_numpyascontiguousarray transposedeepcopyrr append) rrret numpy_imagerrotateZnew_numpy_imagetfms torch_imagedicrrrr!s    z"DensePoseDatasetMapperTTA.__call__)__name__ __module__ __qualname__rr! __classcell__rrrrr s r cs>eZdZd fdd ZddZddZd d Zd d ZZS)DensePoseGeneralizedRCNNWithTTANrcs&||j|_tj||||ddS)a Args: cfg (CfgNode): model (GeneralizedRCNN): a GeneralizedRCNN to apply TTA on. transform_data (DensePoseTransformData): contains symmetry label transforms used for horizontal flip tta_mapper (callable): takes a dataset dict and returns a list of augmented versions of the dataset dict. Defaults to `DatasetMapperTTA(cfg)`. batch_size (int): batch the augmented images into this batch size for inference. )rmodel tta_mapper batch_sizeN)todevice_transform_datarr)rrr7transform_datar8r9rrrr's z(DensePoseGeneralizedRCNNWithTTA.__init__c Cs|d|df}|dtj|d<||\}}|gd"|||\}}}Wdn1sh0Y|||||}|jjj s|jjj r| |||} | || } ~~ |jjj r| | ||_|jjj r|| ||_t|g|R}d|iSd|iSdS)z Args: input (dict): one dataset dict with "image" field being a CHW tensor Returns: dict: one output dict heightwidthr)mask_on keypoint_on densepose_onN instances)r:r&uint8_get_augmented_inputs_turn_off_roi_heads_get_augmented_boxes_merge_detectionsrMODELMASK_ON DENSEPOSE_ON_rescale_detected_boxes_batch_inference_reduce_pred_masks pred_masks_reduce_pred_denseposepred_denseposer) rinput orig_shapeaugmented_inputsr/ all_boxes all_scores all_classesmerged_instancesaugmented_instancesoutputsrrr_inference_one_image7s&0   z4DensePoseGeneralizedRCNNWithTTA._inference_one_imagec Cs||}g}g}g}t||D]j\}}tdd|jDs |jj} ||  } | t |  | j||j||jq t j|dd}|||fS)Ncss|]}t|tVqdSN) isinstancer.0trrr hzGDensePoseGeneralizedRCNNWithTTA._get_augmented_boxes..r)dim)rMzipanyr pred_boxestensorinverse apply_boxcpur#r+r&r'r:r;extendscores pred_classescat) rrTr/rZrUrVrWoutputtfmrforiginal_pred_boxesrrrrG]s  z4DensePoseGeneralizedRCNNWithTTA._get_augmented_boxesc Cstt||D]\}\}}|jD]0}dD]&}t|j|tt|j||jj|q(q t dd|jDrxt |j|j |_| |dj|j|q|djS)N coarse_segm fine_segmuvcss|]}t|tVqdSr\)r]rr^rrrrarbzIDensePoseGeneralizedRCNNWithTTA._reduce_pred_densepose..r) enumeraterdr setattrrQ_inverse_rotationgetattrrfrgrer convertr<_incremental_avg_dp)rrZr/idxrorpr`attrrrrrPrs  z6DensePoseGeneralizedRCNNWithTTA._reduce_pred_denseposecCsFdD]<}t||t|||t|||d|rt||dq|S)Nrrr)rxrz)ravgZnew_elr}r~rrrr|s (z3DensePoseGeneralizedRCNNWithTTA._incremental_avg_dp)Nr) r2r3r4rr[rGrPr|r5rrrrr6&s &r6c Cs t|dkst|ts|S|}|ddddf|ddddf}t||t}|ddddf|ddddf|d}t |j gj |j d }d|dddddf<td| }tt|D]&}t||dkrq||g} tj| ||ddddd} t| tttd||d} tj|| jd} t| | } | jddddd||} | dddd||d| d||d| df} t| jdkrtj| |jd ddd} | dddfd 7<| ||<q|S) Nrr )r;rbilinear)mode)sizerg|=)lenr]rintrjr#rotate_box_inverseastyper&rgrm_imager:r;floatr$maximumrangemincloneF interpolatetolistpadtuplerepeat affine_gridshape grid_sample) Zdensepose_attrsboxes transformZwh_boxesZ inv_boxesZwh_diffZrotation_matrixZl_bdsiZdensepose_attrgridZr_bdsrrrrys4(0"$  8 ryc Cs|||}|dddf|dddf|dddf|dddf}}|dddf|dddf|dddf|dddf}}d|jddksJd||j||jdd|jd}||j||jdd|jd}|dddf||d7<|dddf||d7<|dddf||d8<|dddf||d8<|S)aR rotated_box is a N * 4 array of [x0, y0, x1, y1] boxes When a bbox is rotated, it gets bigger, because we need to surround the tilted bbox So when a bbox is rotated then inverse-rotated, it is much bigger than the original This function aims to invert the rotation on the box, but also resize it to its original size Nrr rz"45 degrees angle can't be inverted)rhriabs_sinabs_cos) Zrot_tfmZ rotated_boxZ invrot_boxhwihiworig_horig_wrrrrsBB&&    r)r%r#r$r&Zfvcore.transformsrrtorch.nnrrdetectron2.data.transformsrrrZ"detectron2.modeling.postprocessingrZ*detectron2.modeling.test_time_augmentationr r convertersr r r6ryrrrrrs   k(