<jupyter_start><jupyter_text>This notebook is used to visualize bboxes on images.  Show detection results and GT<jupyter_code>import scipy.io as sio
devkit_path = 'data/imagenet/ILSVRC/devkit'
####VID object info####
synsets_video = sio.loadmat(os.path.join(devkit_path,'data','meta_vid.mat'))
_wnid = (0,)
_classes = ('__background__',)
for i in xrange(30):
    _classes = _classes + (synsets_video['synsets'][0][i][2][0],)
    _wnid = _wnid + (synsets_video['synsets'][0][i][1][0],)

_wnid_to_ind = dict(zip(_wnid, xrange(31)))
_class_to_ind = dict(zip(_classes, xrange(31)))
#######################
def read_annotation(dataPath):
    filename = dataPath.replace('Data','Annotations').replace('JPEG','xml')
    assert os.path.exists(filename),'%s'%(filename)
    # print 'Loading: {}'.format(filename)
    def get_data_from_tag(node, tag):
        return node.getElementsByTagName(tag)[0].childNodes[0].data

    with open(filename) as f:
        data = minidom.parseString(f.read())

    objs = data.getElementsByTagName('object')
    num_objs = len(objs)
    
    boxes = np.zeros((num_objs, 4), dtype=np.int32)
    gt_classes = np.zeros(num_objs,dtype=np.int32)
    # Load object bounding boxes into a data frame.
    for ix, obj in enumerate(objs):
        x1 = float(get_data_from_tag(obj, 'xmin'))
        y1 = float(get_data_from_tag(obj, 'ymin'))
        x2 = float(get_data_from_tag(obj, 'xmax'))
        y2 = float(get_data_from_tag(obj, 'ymax'))
        cls = _wnid_to_ind[
                str(get_data_from_tag(obj, "name")).lower().strip()]
        boxes[ix, :] = [x1, y1, x2, y2]
        gt_classes[ix] = cls
    return boxes, gt_classes
def showBoxResult(img_id, all_boxes, imdb, show_class=True, threshold=0.3, dpi = 200):
    imIdx = imdb.image_index[img_id]+'.JPEG'
    assert os.path.exists(imIdx), imIdx+' does not exist.'
    #print(imIdx)
    gt_boxes, gt_classes = read_annotation(imIdx)
    im2show = np.array(PIL.Image.open(imIdx))
    fig = plt.figure(frameon=False)
    fig.set_size_inches(im2show.shape[1] / dpi, im2show.shape[0] / dpi)
    ax = plt.Axes(fig, [0., 0., 1., 1.])
    ax.axis('off')
    fig.add_axes(ax)
    ax.imshow(im2show)
    
    #draw gt first
    for j in range(len(gt_classes)):
        ax.add_patch(
        plt.Rectangle((gt_boxes[j][0], gt_boxes[j][1]),
                      gt_boxes[j][2] - gt_boxes[j][0],
                      gt_boxes[j][3] - gt_boxes[j][1],
                      fill=False, edgecolor='g',
                      linewidth=3.0, alpha=0.8))
        if show_class:
            ax.text(gt_boxes[j][0], gt_boxes[j][1] - 2,
            '%s %f'%(imdb.classes[gt_classes[j]], 1.0),
            fontsize=10,
            #family='serif',
            bbox=dict(facecolor='g', alpha=0.4, pad=0, edgecolor='none'),
                color='white')
    
    #draw dets
    for j in range(len(imdb.classes)):
        if isinstance(all_boxes[j][img_id], np.ndarray):
            for bbox in all_boxes[j][img_id]:
                if bbox[-1]>threshold:
                    ax.add_patch(
                    plt.Rectangle((bbox[0], bbox[1]),
                              bbox[2] - bbox[0],
                              bbox[3] - bbox[1],
                              fill=False, edgecolor='y',
                              linewidth=3.0, alpha=0.4))
                    if show_class:
                        ax.text(bbox[0], bbox[1] - 2,
                        '%s %f'%(imdb.classes[j], bbox[-1]),
                        fontsize=10,
                        #family='serif',
                        bbox=dict(
                            facecolor='y', alpha=0.4, pad=0, edgecolor='none'),
                            color='white')
            #im2show = vis_detections(im2show, imdb.classes[j], all_boxes[j][img_id], 0.3)
    #plt.show()
    return fig
###Eg. output
_ = showBoxResult(4283,all_boxes,imdb)<jupyter_output><empty_output><jupyter_text>Save the figure to pdf<jupyter_code>def save_det_fig(index_range,all_boxes,imdb,output_dir,ext,dpi,merge_pdfs = False):
    assert os.path.exists(output_dir), 'Error: output_dir does not exists.'
    if index_range is None:
        index_range = range(len(all_boxes[0]))
    for i in index_range:
        sys.stdout.write('%d/%d \r'%(i+1,len(index_range)))
        sys.stdout.flush()
        fig = showBoxResult(i,all_boxes,imdb,dpi=dpi)
        output_name = '%09d'%(i) + '.' + ext
        fig.savefig(os.path.join(output_dir, '{}'.format(output_name)), dpi=dpi)
        plt.close('all')
    if merge_pdfs:
        merge_out_path = '{}/VID_val_results.pdf'.format('./output/vis')
        if os.path.exists(merge_out_path):
            os.remove(merge_out_path)
        command = "pdfunite {}/*.pdf {}".format(output_dir,
                                                merge_out_path)
        subprocess.call(command, shell=True)
def merge_pdfs(output_dir='./output/vis/VID_val'):
    merge_out_path = '{}/VID_val_results.pdf'.format('./output/vis')
    if os.path.exists(merge_out_path):
        os.remove(merge_out_path)
    command = "pdfunite {}/*.pdf {}".format(output_dir,
                                            merge_out_path)
    subprocess.call(command, shell=True)
save_det_fig(range(0,1000),all_boxes,imdb,output_dir='./output/vis/VID_val',ext='pdf',dpi=100)
merge_pdfs()<jupyter_output><empty_output><jupyter_text>Transform pickle boxes into Text format for evaluation<jupyter_code>def Pickle2Text(picklePath, txtPath, imdb, threshold = 0.8):
    with open(picklePath) as f:
        all_boxes = pickle.load(f)
        num_images = len(imdb.image_index)
        print('num_images:',num_images)
        assert len(all_boxes[0])==num_images, 'Number of images not equal.'
    with open(txtPath,'w') as ft:
        for i in tqdm(range(len(imdb.image_index))):
            for j in range(len(imdb.classes)):
                if isinstance(all_boxes[j][i], np.ndarray):
                    for bbox in all_boxes[j][i]:
                        if bbox[-1]>threshold:
                            item_temp = '%d %d %f %f %f %f %f\n'%(
                                i+1,j,bbox[-1],bbox[0],bbox[1],bbox[2],bbox[3])
                            ft.write(item_temp)
                            #sys.stdout.write(item_temp)
                            #sys.stdout.flush()
from os import path as osp
model_name = 'rfcn_light_head'
picklePath = osp.join('output/res101/imagenetDETVID_val',model_name,'detections.pkl')
txtPath = osp.join('output/res101/imagenetDETVID_val',model_name,'detections.txt')
Pickle2Text(picklePath, txtPath, imdb, threshold=0.01)
model_name = 'rfcn_light_head_OHEM'
picklePath = osp.join('output/res101/imagenetDETVID_val',model_name,'detections.pkl')
txtPath = osp.join('output/res101/imagenetDETVID_val',model_name,'detections.txt')
Pickle2Text(picklePath, txtPath, imdb, threshold=0.01)<jupyter_output>0%|          | 349/176126 [00:00<00:50, 3484.11it/s]<jupyter_text>We can test how threshold of the score influence the final score.<jupyter_code>def Boxes2Text(all_boxes, txtPath, imdb, threshold = 0.0):
    num_images = len(imdb.image_index)
    print('num_images:',num_images)
    assert len(all_boxes[0])==num_images, 'Number of images not equal.'
    with open(txtPath,'w') as ft:
        for i in tqdm(range(len(imdb.image_index))):
            for j in range(len(imdb.classes)):
                if isinstance(all_boxes[j][i], np.ndarray):
                    for bbox in all_boxes[j][i]:
                        if bbox[-1]>threshold:
                            item_temp = '%d %d %f %f %f %f %f\n'%(
                                i+1,j,bbox[-1],bbox[0],bbox[1],bbox[2],bbox[3])
                            ft.write(item_temp)
                            #sys.stdout.write(item_temp)
                            #sys.stdout.flush()
model_name = 'rfcn_light_head'
picklePath = osp.join('output/res101/imagenetDETVID_val',model_name,'detections.pkl')
with open(picklePath) as f:
    all_boxes = pickle.load(f)
threshold = 0.9
picklePath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections.pkl'
txtPath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections_%s.txt'%(str(threshold))
Boxes2Text(all_boxes, txtPath, imdb, threshold=threshold)
threshold = 0.8
picklePath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections.pkl'
txtPath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections_%s.txt'%(str(threshold))
Boxes2Text(all_boxes, txtPath, imdb, threshold=threshold)
threshold = 0.7
picklePath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections.pkl'
txtPath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections_%s.txt'%(str(threshold))
Boxes2Text(all_boxes, txtPath, imdb, threshold=threshold)
threshold = 0.6
picklePath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections.pkl'
txtPath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections_%s.txt'%(str(threshold))
Boxes2Text(all_boxes, txtPath, imdb, threshold=threshold)
threshold = 0.5
picklePath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections.pkl'
txtPath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections_%s.txt'%(str(threshold))
Boxes2Text(all_boxes, txtPath, imdb, threshold=threshold)
threshold = 0.4
picklePath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections.pkl'
txtPath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections_%s.txt'%(str(threshold))
Boxes2Text(all_boxes, txtPath, imdb, threshold=threshold)
threshold = 0.3
picklePath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections.pkl'
txtPath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections_%s.txt'%(str(threshold))
Boxes2Text(all_boxes, txtPath, imdb, threshold=threshold)
threshold = 0.2
picklePath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections.pkl'
txtPath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections_%s.txt'%(str(threshold))
Boxes2Text(all_boxes, txtPath, imdb, threshold=threshold)
threshold = 0.1
picklePath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections.pkl'
txtPath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections_%s.txt'%(str(threshold))
Boxes2Text(all_boxes, txtPath, imdb, threshold=threshold)
threshold = 0.0
picklePath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections.pkl'
txtPath = 'output/res101/imagenetDETVID_val/faster_rcnn_10/detections_%s.txt'%(str(threshold))
Boxes2Text(all_boxes, txtPath, imdb, threshold=threshold)<jupyter_output>0%|          | 169/176126 [00:00<01:44, 1682.75it/s]<jupyter_text>Effect for Seq-NMS.<jupyter_code>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import _init_paths
from model.utils.config import cfg
from roi_data_layer.roidb import combined_roidb
from model.nms.seq_nms import seq_nms
from os import path as osp
import numpy as np
from tqdm import tqdm
try:
    import cPickle as pickle
    print('import cPickle')
except:
    import pickle
    print('import python pickle')
imdbval_name = 'imagenetVID_val'
cfg.TRAIN.USE_FLIPPED = False
imdb, roidb, ratio_list, ratio_index = combined_roidb(imdbval_name, False)
imdb.competition_mode(on=True)
image_index, structured_indexes = imdb._image_index, imdb._structured_indexes
def all_boxes_by_frame_index(all_boxes, indexes):
    new_all_boxes = []
    for cat_boxes in all_boxes:
        cat_b = []
        for index in indexes:
            cat_b.append(cat_boxes[index])
        new_all_boxes.append(cat_b)
    return new_all_boxes
def seqNMS(all_boxes):
    all_boxes_nms = [all_boxes[i] for i in range(1,len(all_boxes))]
    all_boxes_nms = [all_boxes[0]] + seq_nms(all_boxes_nms)
    return all_boxes_nms
def seqNMS_all_boxes(all_boxes):
    out_all_boxes = [[] for i in range(len(all_boxes))]
    for vid_id in range(len(structured_indexes)):
        print('vid:%04d/%04d\n'%(vid_id,len(structured_indexes)),end='')
        vid_indexes = structured_indexes[vid_id]
        vid_boxes = all_boxes_by_frame_index(all_boxes, vid_indexes)
        vid_boxes = seqNMS(vid_boxes)
        for cat_id in range(len(all_boxes)):
            out_all_boxes[cat_id].extend(vid_boxes[cat_id])
        for cat_id in range(len(all_boxes)-1):
            assert len(out_all_boxes[cat_id]) == len(out_all_boxes[cat_id+1]),'length of categories are not equal.'
    return out_all_boxes
def Boxes2Text(all_boxes, txtPath, imdb, threshold = 0.0):
    num_images = len(imdb.image_index)
    print('num_images:',num_images)
    assert len(all_boxes[0])==num_images, 'Number of images not equal.'
    with open(txtPath,'w') as ft:
        for i in tqdm(range(len(imdb.image_index))):
            for j in range(len(imdb.classes)):
                if isinstance(all_boxes[j][i], np.ndarray):
                    for bbox in all_boxes[j][i]:
                        if bbox[-1]>threshold:
                            item_temp = '%d %d %f %f %f %f %f\n'%(
                                i+1,j,bbox[-1],bbox[0],bbox[1],bbox[2],bbox[3])
                            ft.write(item_temp)
# Load boxes.
model_name = 'faster_rcnn_10'
picklePath = osp.join('output/res101/imagenetDETVID_val',model_name,'detections.pkl')
with open(picklePath) as f:
    all_boxes = pickle.load(f)
# NMS.
all_boxes_nms = seqNMS_all_boxes(all_boxes)
threshold = 0.0
txtPath = osp.join('output/res101/imagenetDETVID_val',model_name,'detections_%s_%f.txt'%('seq_nms',threshold))
pklPath = osp.join('output/res101/imagenetDETVID_val',model_name,'detections_%s_%f.pkl'%('seq_nms',threshold))
with open(pklPath, 'wb') as f:
    pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)
Boxes2Text(all_boxes_nms, txtPath, imdb, threshold=threshold)
Boxes2Text(all_boxes_nms, txtPath, imdb, threshold=threshold)
# Load boxes.
model_name = 'rfcn_light_head'
picklePath = osp.join('output/res101/imagenetDETVID_val',model_name,'detections.pkl')
with open(picklePath) as f:
    all_boxes = pickle.load(f)
# NMS.
all_boxes_nms = seqNMS_all_boxes(all_boxes)
threshold = 0.0
txtPath = osp.join('output/res101/imagenetDETVID_val',model_name,'detections_%s_%f.txt'%('seq_nms',threshold))
pklPath = osp.join('output/res101/imagenetDETVID_val',model_name,'detections_%s_%f.pkl'%('seq_nms',threshold))
with open(pklPath, 'wb') as f:
    pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)
Boxes2Text(all_boxes_nms, txtPath, imdb, threshold=threshold)<jupyter_output>vid:0000/0555
vid:0001/0555
vid:0002/0555
vid:0003/0555
vid:0004/0555
vid:0005/0555
vid:0006/0555
vid:0007/0555
vid:0008/0555
vid:0009/0555
vid:0010/0555
vid:0011/0555
vid:0012/0555
vid:0013/0555
vid:0014/0555
vid:0015/0555
vid:0016/0555
vid:0017/0555
vid:0018/0555
vid:0019/0555
vid:0020/0555
vid:0021/0555
vid:0022/0555
vid:0023/0555
vid:0024/0555
vid:0025/0555
vid:0026/0555
vid:0027/0555
vid:0028/0555
vid:0029/0555
vid:0030/0555
vid:0031/0555
vid:0032/0555
vid:0033/0555
vid:0034/0555
vid:0035/0555
vid:0036/0555
vid:0037/0555
vid:0038/0555
vid:0039/0555
vid:0040/0555
vid:0041/0555
vid:0042/0555
vid:0043/0555
vid:0044/0555
vid:0045/0555
vid:0046/0555
vid:0047/0555
vid:0048/0555
vid:0049/0555
vid:0050/0555
vid:0051/0555
vid:0052/0555
vid:0053/0555
vid:0054/0555
vid:0055/0555
vid:0056/0555
vid:0057/0555
vid:0058/0555
vid:0059/0555
vid:0060/0555
vid:0061/0555
vid:0062/0555
vid:0063/0555
vid:0064/0555
vid:0065/0555
vid:0066/0555
vid:0067/0555
vid:0068/0555
vid:0069/0555
vid:0070/0555
vid:00[...]
{'fraction_non_alphanumeric': 0.13723958333333333, 'fraction_numerical': 0.061328125, 'mean_word_length': 3.5086586439683005, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '382622', 'license': 'MIT', 'n_tokens_mistral': 6394, 'n_tokens_neox': 5783, 'n_words': 888, 'path': 'Examine DET result.ipynb', 'repo_name': 'YeLyuUT/FastVOD', 'size': '15360'}