σ ›lΛ\c@@s†ddlmZmZddlZddlZddlmZddlj j Z ddl m Z ddlZdefd„ƒYZdS(i(tprint_functiontabsolute_importN(tImdb(tvoc_evalt PascalVoccB@s˜eZdZeedded„Zed„ƒZd„Zd„Z d„Z d„Z d„Z d „Z d „Zd „Zd „Zd „Zd„ZRS(s¨ Implementation of Imdb for Pascal VOC datasets Parameters: ---------- image_set : str set to be used, can be train, val, trainval, test year : str year of dataset, can be 2007, 2010, 2012... devkit_path : str devkit path of VOC dataset shuffle : boolean whether to initial shuffle the image list is_train : boolean if true, will load annotations spascal_voc.namesc C@sMtt|ƒjd|d|ƒ||_||_||_tjj|d|ƒ|_ d|_ ||_ ||_ |dk r›|jƒjdƒ|_n0|j|tjjtjjtƒdƒƒ|_itd6dd 6|_t|jƒ|_|j|ƒ|_t|jƒ|_|j r3|jƒ|_n|j sI|jƒndS( Ntvoc_t_tVOCs.jpgt,tnamest use_difficulttcomp4tcomp_id(tsuperRt__init__t image_settyeart devkit_pathtostpathtjoint data_patht extensiontis_trainttrue_negative_imagestNonetstriptsplittclassest_load_class_namestdirnamet__file__tTruetconfigtlent num_classest_load_image_set_indextimage_set_indext num_imagest_load_image_labelstlabelst_filter_image_with_no_gt( tselfRRRtshuffleRt class_namesR R((s—/home/ec2-user/SageMaker/amazon-sagemaker-examples/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/DataGeneration/tools/pascal_voc.pyRs*"        '    cC@sJtjjtjjtƒddƒ}tjj|ƒsFtj|ƒn|S(sq make a directory to store all caches Returns: --------- cache path s..tcache(RRRRRtexiststmkdir(R*t cache_path((s—/home/ec2-user/SageMaker/amazon-sagemaker-examples/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/DataGeneration/tools/pascal_voc.pyR08s $cC@s-g|jD][}|tj|dd…dfdk|dd…df|jdkƒdd…f^q |_gt|jƒD]!\}}|jdks~|^q~}g|D]}|j|^q¬|_g|D]}|j|^qΟ|_|j}t|jƒ|_t dƒt dj |j|ƒƒdS(sŽ filter images that have no ground-truth labels. use case: when you wish to work only on a subset of pascal classes, you have 2 options: 1. use only the sub-dataset that contains the subset of classes 2. use all images, and images with no ground-truth will count as true-negative images :return: self object with filtered information NiisRfiltering images with no gt-labels. can abort filtering using *true_negative* flags... remaining {0}/{1} images. ( R(tnpt logical_andR#t enumeratetsizeR%R&R"tprinttformat(R*tftidxt gt_indicestold_num_images((s—/home/ec2-user/SageMaker/amazon-sagemaker-examples/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/DataGeneration/tools/pascal_voc.pyR)Fs n7##  cC@s£tjj|jdd|jdƒ}tjj|ƒsLtdj|ƒƒ‚t|ƒ,}g|j ƒD]}|j ƒ^qh}WdQX|rŸt j j |ƒn|S(s  find out which indexes correspond to given image set (train or val) Parameters: ---------- shuffle : boolean whether to shuffle the image list Returns: ---------- entire list of images specified in the setting t ImageSetstMains.txtsPath does not exist: {}N(RRRRRR.tAssertionErrorR6topent readlinesRR1trandomR+(R*R+timage_set_index_fileR7txR%((s—/home/ec2-user/SageMaker/amazon-sagemaker-examples/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/DataGeneration/tools/pascal_voc.pyR$]s %'+cC@su|jdk stdƒ‚|j|}tjj|jd||jƒ}tjj|ƒsqtdj |ƒƒ‚|S(sά given image index, find out full path Parameters: ---------- index: int index of a specific image Returns: ---------- full path of this image sDataset not initializedt JPEGImagessPath does not exist: {}N( R%RR=RRRRRR.R6(R*tindextnamet image_file((s—/home/ec2-user/SageMaker/amazon-sagemaker-examples/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/DataGeneration/tools/pascal_voc.pytimage_path_from_indexqs  "'cC@s&|jdk stdƒ‚|j|S(sξ given image index, return preprocessed ground-truth Parameters: ---------- index: int index of a specific image Returns: ---------- ground-truths of this image sLabels not processedN(R(RR=(R*RD((s—/home/ec2-user/SageMaker/amazon-sagemaker-examples/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/DataGeneration/tools/pascal_voc.pytlabel_from_indexƒs cC@sJtjj|jd|dƒ}tjj|ƒsFtdj|ƒƒ‚|S(sθ given image index, find out annotation path Parameters: ---------- index: int index of a specific image Returns: ---------- full path of annotation file t Annotationss.xmlsPath does not exist: {}(RRRRR.R=R6(R*RDt label_file((s—/home/ec2-user/SageMaker/amazon-sagemaker-examples/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/DataGeneration/tools/pascal_voc.pyt_label_path_from_index’s 'c C@sΈg}x«|jD] }|j|ƒ}tj|ƒ}|jƒ}|jdƒ}t|jdƒjƒ}t|jdƒjƒ}g} x|jdƒD]} t | jdƒjƒ} | jdƒj} | |j krζt |j ƒ} n|j j | ƒ} | jdƒ}t|jdƒjƒ|}t|jd ƒjƒ|}t|jd ƒjƒ|}t|jd ƒjƒ|}| j | ||||| gƒq•W|j tj| ƒƒqW|S( s– preprocess all ground-truths Returns: ---------- labels packed in [num_images x max_num_objects x 5] tensor R4twidththeighttobjectt difficultREtbndboxtxmintymintxmaxtymax(R%RKtETtparsetgetroottfindtfloatttexttitertintRR"RDtappendR1tarray(R*ttempR8RJttreetrootR4RLRMtlabeltobjROtcls_nametcls_idtxml_boxRQRRRSRT((s—/home/ec2-user/SageMaker/amazon-sagemaker-examples/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/DataGeneration/tools/pascal_voc.pyR'£s. #cC@sΰtjj|jdƒ}tjj|ƒs:tj|ƒntjj|jdd|jƒ}tjj|ƒs~tj|ƒntjj|jdd|jdƒ}tjj|ƒsΕtj|ƒn|j|ƒ|jƒdS(sΪ top level evaluations Parameters: ---------- detections: list result list, each entry is a matrix of detections Returns: ---------- None tresultsRR<N( RRRRR.R/Rtwrite_pascal_resultstdo_python_eval(R*t detectionst result_dirt year_foldertres_file_folder((s—/home/ec2-user/SageMaker/amazon-sagemaker-examples/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/DataGeneration/tools/pascal_voc.pytevaluate_detectionsΙs "% cC@s`tjj|jdd|jdƒ}|jd}|d|jd}tjj||ƒ}|S(s― this is a template VOCdevkit/results/VOC2007/Main/_det_test_aeroplane.txt Returns: ---------- a string template RgRR<R t_det_s _{:s}.txt(RRRRRR!R(R*RmR tfilenameR((s—/home/ec2-user/SageMaker/amazon-sagemaker-examples/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/DataGeneration/tools/pascal_voc.pytget_result_file_templateβs % c C@s‘xŠt|jƒD]y\}}tdj|ƒƒ|jƒj|ƒ}t|dƒ4}x*t|jƒD]\}}||}|jddkr•qfn|j|j |ƒƒ\} } xΙt |jdƒD]΄} t || dfƒ|krΗ|j dj||| dft || df| ƒdt || df| ƒdt || df| ƒdt || d f| ƒdƒƒqΗqΗWqfWWd QXqWd S( sΰ write results files in pascal devkit path Parameters: ---------- all_boxes: list boxes to be processed [bbox, confidence] Returns: ---------- None sWriting {} VOC results filetwtiis({:s} {:.3f} {:.1f} {:.1f} {:.1f} {:.1f} iiiiN( R3RR5R6RqR>R%tshapet _get_imsizeRGtrangeR\twrite( R*t all_boxestcls_indtclsRpR7tim_indRDtdetsthtwtk((s—/home/ec2-user/SageMaker/amazon-sagemaker-examples/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/DataGeneration/tools/pascal_voc.pyRhρs   6c C@sAtjj|jddƒ}tjj|jdd|jdƒ}tjj|j|jƒ}g}t|jƒdkr|t nt }t d|r”dnd ƒx‚t |j ƒD]q\}}|jƒj|ƒ}t|||||d d d |ƒ\} } } || g7}t d j|| ƒƒq¬Wt djtj|ƒƒƒdS(s] python evaluation wrapper Returns: ---------- None RIs{:s}.xmlR;R<s.txtiΪsVOC07 metric? tYtNotovthreshgΰ?t use_07_metricsAP for {} = {:.4f}sMean AP = {:.4f}N(RRRRRR0RER\RR tFalseR5R3RRqR6RR1tmean( R*tannopatht imageset_filet cache_dirtapsR‚RxRyRptrectprectap((s—/home/ec2-user/SageMaker/amazon-sagemaker-examples/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/DataGeneration/tools/pascal_voc.pyRi s%! cC@s'tj|ƒ}|jd|jdfS(sj get image size info Returns: ---------- tuple of (height, width) ii(tcv2timreadRs(R*tim_nametimg((s—/home/ec2-user/SageMaker/amazon-sagemaker-examples/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/DataGeneration/tools/pascal_voc.pyRt$sN(t__name__t __module__t__doc__RƒRRtpropertyR0R)R$RGRHRKR'RnRqRhRiRt(((s—/home/ec2-user/SageMaker/amazon-sagemaker-examples/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/DataGeneration/tools/pascal_voc.pyR s       &    (t __future__RRRtnumpyR1timdbRtxml.etree.ElementTreetetreet ElementTreeRUteval_vocRRŒR(((s—/home/ec2-user/SageMaker/amazon-sagemaker-examples/introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/DataGeneration/tools/pascal_voc.pyts