用matplotlib画PR曲线

首先,头文件加入:

import matplotlib as plt

然后以VOC为例,画PR曲线:

def _do_python_eval(self, output_dir='output'):
        #rootpath = os.path.join(self.root, 'VOC' + self._year)
        rootpath = '/data/Datasets/luzs/PCB/pcb_final_test'
        name = self.image_set[0][1]
        annopath = os.path.join(
            rootpath,
            'Annotations',
            '{:s}.xml')
        #imagesetfile = os.path.join(
        #    rootpath,
        #    'ImageSets',
        #    'Main',
        #    name + '.txt')
        imagesetfile = self.txt_dir
        cachedir = os.path.join(self.root, 'annotations_cache')
        aps = []
        # The PASCAL VOC metric changed in 2010
        use_07_metric = True if int(self._year) < 2010 else False
        print('VOC07 metric? ' + ('Yes' if use_07_metric else 'No'))
        if output_dir is not None and not os.path.isdir(output_dir):
            os.mkdir(output_dir)
        for i, cls in enumerate(VOC_CLASSES):
            if cls == '__background__':
                continue
            filename = self._get_voc_results_file_template().format(cls)
            rec, prec, ap = voc_eval(
                filename, annopath, imagesetfile, cls, cachedir, ovthresh=0.5,
                use_07_metric=use_07_metric)
            aps += [ap]
            plt.plot(rec, prec, lw=2, label = 'PR curve of class {}(area={:.4f})'.format(cls,ap))
            print('AP for {} = {:.4f}'.format(cls, ap))
            if output_dir is not None:
                with open(os.path.join(output_dir, cls + '_pr.pkl'), 'wb') as f:
                    pickle.dump({'rec': rec, 'prec': prec, 'ap': ap}, f)
        plt.xlabel('Recall')    
        plt.ylabel('Precision')    
        plt.grid(True)    
        plt.ylim([0.0, 1.05])    
        plt.xlim([0.0, 1.0])    
        plt.title('Precision-Recall')    
        plt.legend(loc="bottom left")         
        plt.savefig('pr.jpg')

最主要的都是plt开头的那几行。

你可能感兴趣的:(python)