<s> """<STR_LIT>""" <EOL> import xml . etree . ElementTree as ET <EOL> import os <EOL> import cPickle <EOL> import numpy as np <EOL> def parse_rec ( filename ) : <EOL> """<STR_LIT>""" <EOL> tree = ET . parse ( filename ) <EOL> objects = [ ] <EOL> for obj in tree . findall ( '<STR_LIT:object>' ) : <EOL> obj_struct = { } <EOL> obj_struct [ '<STR_LIT:name>' ] = obj . find ( '<STR_LIT:name>' ) . text <EOL> obj_struct [ '<STR_LIT>' ] = obj . find ( '<STR_LIT>' ) . text <EOL> obj_struct [ '<STR_LIT>' ] = int ( obj . find ( '<STR_LIT>' ) . text ) <EOL> obj_struct [ '<STR_LIT>' ] = int ( obj . find ( '<STR_LIT>' ) . text ) <EOL> bbox = obj . find ( '<STR_LIT>' ) <EOL> obj_struct [ '<STR_LIT>' ] = [ int ( bbox . find ( '<STR_LIT>' ) . text ) , <EOL> int ( bbox . find ( '<STR_LIT>' ) . text ) , <EOL> int ( bbox . find ( '<STR_LIT>' ) . text ) , <EOL> int ( bbox . find ( '<STR_LIT>' ) . text ) ] <EOL> objects . append ( obj_struct ) <EOL> return objects <EOL> def voc_ap ( rec , prec , use_07_metric = False ) : <EOL> """<STR_LIT>""" <EOL> if use_07_metric : <EOL> ap = <NUM_LIT:0.> <EOL> for t in np . arange ( <NUM_LIT:0.> , <NUM_LIT> , <NUM_LIT:0.1> ) : <EOL> if np . sum ( rec >= t ) == <NUM_LIT:0> : <EOL> p = <NUM_LIT:0> <EOL> else : <EOL> p = np . max ( prec [ rec >= t ] ) <EOL> ap = ap + p / <NUM_LIT> <EOL> else : <EOL> mrec = np . concatenate ( ( [ <NUM_LIT:0.> ] , rec , [ <NUM_LIT:1.> ] ) ) <EOL> mpre = np . concatenate ( ( [ <NUM_LIT:0.> ] , prec , [ <NUM_LIT:0.> ] ) ) <EOL> for i in range ( mpre . size - <NUM_LIT:1> , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> mpre [ i - <NUM_LIT:1> ] = np . maximum ( mpre [ i - <NUM_LIT:1> ] , mpre [ i ] ) <EOL> i = np . where ( mrec [ <NUM_LIT:1> : ] != mrec [ : - <NUM_LIT:1> ] ) [ <NUM_LIT:0> ] <EOL> ap = np . sum ( ( mrec [ i + <NUM_LIT:1> ] - mrec [ i ] ) * mpre [ i + <NUM_LIT:1> ] ) <EOL> return ap <EOL> def voc_eval ( detpath , <EOL> annopath , <EOL> imagesetfile , <EOL> classname , <EOL> cachedir , <EOL> ovthresh = <NUM_LIT:0.5> , <EOL> use_07_metric = False ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . isdir ( cachedir ) : <EOL> os . mkdir ( cachedir ) <EOL> cachefile = os . path . join ( cachedir , '<STR_LIT>' ) <EOL> with open ( imagesetfile , '<STR_LIT:r>' ) as f : <EOL> lines = f . readlines ( ) <EOL> imagenames = [ x . strip ( ) for x in lines ] <EOL> if not os . path . isfile ( cachefile ) : <EOL> recs = { } <EOL> for i , imagename in enumerate ( imagenames ) : <EOL> recs [ imagename ] = parse_rec ( annopath . format ( imagename ) ) <EOL> if i % <NUM_LIT:100> == <NUM_LIT:0> : <EOL> print '<STR_LIT>' . format ( <EOL> i + <NUM_LIT:1> , len ( imagenames ) ) <EOL> print '<STR_LIT>' . format ( cachefile ) <EOL> with open ( cachefile , '<STR_LIT:w>' ) as f : <EOL> cPickle . dump ( recs , f ) <EOL> else : <EOL> with open ( cachefile , '<STR_LIT:r>' ) as f : <EOL> recs = cPickle . load ( f ) <EOL> class_recs = { } <EOL> npos = <NUM_LIT:0> <EOL> for imagename in imagenames : <EOL> R = [ obj for obj in recs [ imagename ] if obj [ '<STR_LIT:name>' ] == classname ] <EOL> bbox = np . array ( [ x [ '<STR_LIT>' ] for x in R ] ) <EOL> difficult = np . array ( [ x [ '<STR_LIT>' ] for x in R ] ) . astype ( np . bool ) <EOL> det = [ False ] * len ( R ) <EOL> npos = npos + sum ( ~ difficult ) <EOL> class_recs [ imagename ] = { '<STR_LIT>' : bbox , <EOL> '<STR_LIT>' : difficult , <EOL> '<STR_LIT>' : det } <EOL> detfile = detpath . format ( classname ) <EOL> with open ( detfile , '<STR_LIT:r>' ) as f : <EOL> lines = f . readlines ( ) <EOL> splitlines = [ x . strip ( ) . split ( '<STR_LIT:U+0020>' ) for x in lines ] <EOL> image_ids = [ x [ <NUM_LIT:0> ] for x in splitlines ] <EOL> confidence = np . array ( [ float ( x [ <NUM_LIT:1> ] ) for x in splitlines ] ) <EOL> BB = np . array ( [ [ float ( z ) for z in x [ <NUM_LIT:2> : ] ] for x in splitlines ] ) <EOL> sorted_ind = np . argsort ( - confidence ) <EOL> BB = BB [ sorted_ind , : ] <EOL> image_ids = [ image_ids [ x ] for x in sorted_ind ] <EOL> nd = len ( image_ids ) <EOL> tp = np . zeros ( nd ) <EOL> fp = np . zeros ( nd ) <EOL> for d in range ( nd ) : <EOL> R = class_recs [ image_ids [ d ] ] <EOL> bb = BB [ d , : ] . astype ( float ) <EOL> ovmax = - np . inf <EOL> BBGT = R [ '<STR_LIT>' ] . astype ( float ) <EOL> if BBGT . size > <NUM_LIT:0> : <EOL> ixmin = np . maximum ( BBGT [ : , <NUM_LIT:0> ] , bb [ <NUM_LIT:0> ] ) <EOL> iymin = np . maximum ( BBGT [ : , <NUM_LIT:1> ] , bb [ <NUM_LIT:1> ] ) <EOL> ixmax = np . minimum ( BBGT [ : , <NUM_LIT:2> ] , bb [ <NUM_LIT:2> ] ) <EOL> iymax = np . minimum ( BBGT [ : , <NUM_LIT:3> ] , bb [ <NUM_LIT:3> ] ) <EOL> iw = np . maximum ( ixmax - ixmin + <NUM_LIT:1.> , <NUM_LIT:0.> ) <EOL> ih = np . maximum ( iymax - iymin + <NUM_LIT:1.> , <NUM_LIT:0.> ) <EOL> inters = iw * ih <EOL> uni = ( ( bb [ <NUM_LIT:2> ] - bb [ <NUM_LIT:0> ] + <NUM_LIT:1.> ) * ( bb [ <NUM_LIT:3> ] - bb [ <NUM_LIT:1> ] + <NUM_LIT:1.> ) + <EOL> ( BBGT [ : , <NUM_LIT:2> ] - BBGT [ : , <NUM_LIT:0> ] + <NUM_LIT:1.> ) * <EOL> ( BBGT [ : , <NUM_LIT:3> ] - BBGT [ : , <NUM_LIT:1> ] + <NUM_LIT:1.> ) - inters ) <EOL> overlaps = inters / uni <EOL> ovmax = np . max ( overlaps ) <EOL> jmax = np . argmax ( overlaps ) <EOL> if ovmax > ovthresh : <EOL> if not R [ '<STR_LIT>' ] [ jmax ] : <EOL> if not R [ '<STR_LIT>' ] [ jmax ] : <EOL> tp [ d ] = <NUM_LIT:1.> <EOL> R [ '<STR_LIT>' ] [ jmax ] = <NUM_LIT:1> <EOL> else : <EOL> fp [ d ] = <NUM_LIT:1.> <EOL> else : <EOL> fp [ d ] = <NUM_LIT:1.> <EOL> fp = np . cumsum ( fp ) <EOL> tp = np . cumsum ( tp ) <EOL> rec = tp / float ( npos ) <EOL> prec = tp / ( tp + fp + <NUM_LIT> ) <EOL> ap = voc_ap ( rec , prec , use_07_metric ) <EOL> return rec , prec , ap </s>
