def gr(metric_folder):
    #     categories=["jumping", "phoning", "playinginstrument", "reading", "ridingbike", "ridinghorse", "running", "takingphoto", "usingcomputer", "walking"]
    epsilon = '0.001'
    lbd = '1.0E-4'
    detection_types = ["train"]
    categories = ["jumping"]

    gr_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))

    for category in categories:
        #     for category in ["dog"]:
        for tradeoff in ['0.0']:
            #             for scale in ["50"]:
            for scale in ['90']:
                for typ in detection_types:

                    #                     detection_filename = '_'.join(["metric", typ,scale, str(tradeoff),str(epsilon), str(lbd), category+'_0.txt'])
                    #                     detection_filename = 'metric_val_90_0.0_0.0_1.0E-4_jumping_0.txt'
                    detection_filename = 'metric_train_90_0.0_0.0_1.0E-4_jumping_2.0_10_0.txt'  #learning_rate, neuron
                    fp = os.path.join(metric_folder, detection_filename)
                    f = open(fp)

                    total_gr = 0

                    cnt = 0
                    for line in f:

                        score, yp, yi, hp, hi, filename_root = line.strip(
                        ).split(',')
                        filename_root = filename_root.split('/')[-1].strip()
                        if True:
                            #                         if yi=='1':
                            cnt += 1
                            print cnt
                            grid_1, grid_2 = metric_calculate.h2GridCoor(
                                hp, int(scale))
                            gaze_file_root = filename_root
                            ratio_file = VOC2012_OBJECT_ETLOSS + category + '/' + str(
                                metric_calculate.convert_scale(int(scale))
                            ) + '/' + gaze_file_root + '_' + str(
                                grid_1) + '_' + str(grid_2) + '.txt'
                            ratio_file = "/local/wangxin/Data/gaze_voc_actions_stefan/ETLoss_ratio/" + str(
                                metric_calculate.convert_scale(int(scale))
                            ) + '/' + gaze_file_root + '_' + str(
                                grid_1) + '_' + str(grid_2) + '.txt'
                            #
                            with open(ratio_file) as ratio_f:
                                ratio = float(ratio_f.readline().strip())
                                total_gr += ratio
                    total_gr /= cnt
                print total_gr
    return gr_res
def metric_file_analyse(metric_folder):
    #     categories=["jumping", "phoning", "playinginstrument", "reading", "ridingbike", "ridinghorse", "running", "takingphoto", "usingcomputer", "walking"]
    categories = ["jumping"]
    epsilon = '0.0'
    lbd = '1.0E-4'
    detection_types = ["train"]
    #     categories = ["aeroplane" ,"cow", "dog" ,"cat" ,"motorbike" ,"boat" , "horse" , "sofa" ,"diningtable" ,"bicycle"]

    detection_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))
    gr_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))

    all_positive = True
    all_instance = True

    for scale in ['90']:
        #         for tradeoff in ['0.0','0.1','0.2','0.3','0.4','0.5','0.6','0.7','0.8','0.9','1.0']:
        for tradeoff in ['0.0']:

            total_IoU = 0

            for category in categories:
                #     for category in ["dog"]:
                #         for tradeoff in ['0.2']:
                #             for scale in ["50"]:
                detection_res_3_tuple = [0] * 3
                gr_res_3_tuple = [0] * 3
                for typ in detection_types:

                    detection_filename = '_'.join([
                        "metric", typ, scale,
                        str(tradeoff),
                        str(epsilon),
                        str(lbd), category + '_0.txt'
                    ])
                    fp = os.path.join(metric_folder, detection_filename)
                    f = open(fp)

                    #                     f=open("/local/wangxin/results/full_stefan_gaze/glsvm_pos_neg/symil_cccpgaze_posneg_cv_single_split_test/metric/metric_val_50_1.0_0.0_0.0_1.0E-6_jumping.txt")

                    total_gr = 0

                    cnt = 0
                    for line in f:
                        score, yp, yi, hp, hi, filename_root = line.strip(
                        ).split(',')
                        #                         print score, yp, yi, hp, filename_root
                        filename_root = filename_root.split('/')[-1].strip()
                        #                         if True:
                        if yi == '1':
                            cnt += 1
                            grid_1, grid_2 = metric_calculate.h2GridCoor(
                                hp, int(scale))
                            gaze_file_root = filename_root
                            #                             ratio_file = VOC2012_OBJECT_ETLOSS+category+'/'+str(metric_calculate.convert_scale(int(scale)))+'/'+gaze_file_root+'_'+str(grid_1)+'_'+str(grid_2)+'.txt'
                            #                             ratio_file = "/local/wangxin/Data/gaze_voc_actions_stefan/ETLoss_ratio/"+str(metric_calculate.convert_scale(int(scale))) + '/'+gaze_file_root+'_'+str(grid_1)+'_'+str(grid_2)+'.txt'
                            #
                            #                             with open(ratio_file) as ratio_f:
                            #                                 ratio = float(ratio_f.readline().strip())
                            #                                 total_gr+=ratio

                            xml_filename_root = filename_root
                            #Obj#
                            #                             bbs = ground_truth_bb(VOC2012_TRAIN_ANNOTATIONS+xml_filename_root,category)
                            #                             bbs = ground_truth_bb_all(VOC2012_TRAIN_ANNOTATIONS+xml_filename_root)
                            #action
                            #                             bbs = ground_truth_bb_all_stefan_given_action(VOC2012_TRAIN_ANNOTATIONS+xml_filename_root,category)
                            bbs = ground_truth_bb_all_stefan(
                                VOC2012_TRAIN_ANNOTATIONS + xml_filename_root)
                            im = Image.open(VOC2012_TRAIN_IMAGES +
                                            xml_filename_root + '.jpg')
                            width, height = im.size
                            hxmin, hymin, hxmax, hymax = metric_calculate.h2Coor(
                                width, height, hp, int(scale))
                            #                             print hxmin, hymin, hxmax, hymax
                            IoU = metric_calculate.getTopIoU(
                                hxmin, hymin, hxmax, hymax, bbs)
                            #                             print filename_root, IoU, hp
                            total_IoU += IoU
                    total_IoU /= cnt
                    print category, scale, total_IoU
                    #                     total_gr /= cnt
                    detection_res_3_tuple[detection_types.index(
                        typ)] = total_IoU
#                     gr_res_3_tuple[detection_types.index(typ)] = total_gr
                detection_res[scale][tradeoff][
                    category] = detection_res_3_tuple


#                 gr_res[scale][tradeoff][category]= gr_res_3_tuple
    return detection_res, gr_res
def metric_file_analyse_detection_positive(metric_folder, categories):

    epsilon = '0.001'
    lbd = '1.0E-4'
    detection_types = ["train", "valval", "valtest"]
    scale_cv = ["50"]
    tradeoff_cv = [
        '0.0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9',
        '1.0'
    ]
    only_tp = True
    if data_typ == "ferrari":
        etloss_root = VOC2012_OBJECT_ETLOSS
    elif data_typ == "stefan:":
        etloss_root = VOC2012_ACTION_ETLOSS

    detection_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))
    gr_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))
    for category in categories:
        #     for category in ["dog"]:
        for tradeoff in tradeoff_cv:
            #             for scale in ["50"]:
            for scale in scale_cv:
                detection_res_3_tuple = [0] * 3
                gr_res_3_tuple = [0] * 3
                for typ in detection_types:

                    detection_filename = '_'.join([
                        "metric", typ, tradeoff, scale,
                        str(epsilon),
                        str(lbd), category + '.txt'
                    ])
                    fp = os.path.join(metric_folder, str(scale),
                                      detection_filename)
                    f = open(fp)

                    total_gr = 0
                    total_IoU = 0

                    cnt = 0
                    for line in f:

                        yp, yi, hp, filename_root = line.strip().split(',')
                        filename_root = filename_root.split('/')[-1].strip()
                        if yi == '1' and (only_tp and yp == '1'):
                            cnt += 1
                            grid_1, grid_2 = metric_calculate.h2GridCoor(
                                hp, int(scale))
                            gaze_file_root = filename_root
                            ratio_file = etloss_root + category + '/' + str(
                                metric_calculate.convert_scale(int(scale))
                            ) + '/' + gaze_file_root + '_' + str(
                                grid_1) + '_' + str(grid_2) + '.txt'

                            with open(ratio_file) as ratio_f:
                                ratio = float(ratio_f.readline().strip())
                                total_gr += ratio

                            xml_filename_root = filename_root
                            bbs = ground_truth_bb_all_stefan(
                                VOC2012_TRAIN_ANNOTATIONS + xml_filename_root)
                            #                             bbs = ground_truth_bb_all(VOC2012_TRAIN_ANNOTATIONS+xml_filename_root)
                            im = Image.open(VOC2012_TRAIN_IMAGES +
                                            xml_filename_root + '.jpg')
                            width, height = im.size
                            hxmin, hymin, hxmax, hymax = metric_calculate.h2Coor(
                                width, height, hp, int(scale))
                            IoU = metric_calculate.getTopIoU(
                                hxmin, hymin, hxmax, hymax, bbs)
                            total_IoU += IoU
                    total_IoU /= cnt
                    total_gr /= cnt
                    detection_res_3_tuple[detection_types.index(
                        typ)] = total_IoU
                    gr_res_3_tuple[detection_types.index(typ)] = total_gr
                detection_res[scale][tradeoff][
                    category] = detection_res_3_tuple
                gr_res[scale][tradeoff][category] = gr_res_3_tuple
    return detection_res, gr_res
def metric_file_analyse(metric_folder, categories):
    #     categories=["jumping", "phoning", "playinginstrument", "reading", "ridingbike", "ridinghorse", "running", "takingphoto", "usingcomputer", "walking"]
    epsilon = '0.001'
    lbd = '1.0E-4'
    detection_types = ["train", "valval", "valtest"]

    detection_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))
    gr_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))

    all_positive = True
    all_instance = True

    for category in categories:
        #     for category in ["dog"]:
        for tradeoff in [
                '0.0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8',
                '0.9', '1.0'
        ]:
            #             for scale in ["50"]:
            for scale in ['50']:
                detection_res_3_tuple = [0] * 3
                gr_res_3_tuple = [0] * 3
                for typ in detection_types:

                    detection_filename = '_'.join([
                        "metric", typ, tradeoff, scale,
                        str(epsilon),
                        str(lbd), category + '.txt'
                    ])
                    fp = os.path.join(metric_folder, str(scale),
                                      detection_filename)
                    f = open(fp)

                    total_gr = 0
                    total_IoU = 0

                    cnt = 0
                    for line in f:

                        yp, yi, hp, filename_root = line.strip().split(',')
                        filename_root = filename_root.split('/')[-1].strip()
                        if True:
                            #                         if yi=='1':
                            cnt += 1

                            grid_1, grid_2 = metric_calculate.h2GridCoor(
                                hp, int(scale))
                            gaze_file_root = filename_root
                            #                             ratio_file = VOC2012_OBJECT_ETLOSS+category+'/'+str(metric_calculate.convert_scale(int(scale)))+'/'+gaze_file_root+'_'+str(grid_1)+'_'+str(grid_2)+'.txt'
                            #                             ratio_file = "/local/wangxin/Data/gaze_voc_actions_stefan/ETLoss_ratio/"+str(metric_calculate.convert_scale(int(scale))) + '/'+gaze_file_root+'_'+str(grid_1)+'_'+str(grid_2)+'.txt'
                            #
                            #                             with open(ratio_file) as ratio_f:
                            #                                 ratio = float(ratio_f.readline().strip())
                            #                                 total_gr+=ratio

                            xml_filename_root = filename_root
                            bbs = ground_truth_bb_all_stefan(
                                VOC2012_TRAIN_ANNOTATIONS + xml_filename_root)
                            #                             bbs = ground_truth_bb_all(VOC2012_TRAIN_ANNOTATIONS+xml_filename_root)
                            im = Image.open(VOC2012_TRAIN_IMAGES +
                                            xml_filename_root + '.jpg')
                            width, height = im.size
                            hxmin, hymin, hxmax, hymax = metric_calculate.h2Coor(
                                width, height, hp, int(scale))
                            IoU = metric_calculate.getTopIoU(
                                hxmin, hymin, hxmax, hymax, bbs)
                            total_IoU += IoU
                    total_IoU /= cnt
                    #                     total_gr /= cnt
                    detection_res_3_tuple[detection_types.index(
                        typ)] = total_IoU
#                     gr_res_3_tuple[detection_types.index(typ)] = total_gr
                detection_res[scale][tradeoff][
                    category] = detection_res_3_tuple


#                 gr_res[scale][tradeoff][category]= gr_res_3_tuple
    return detection_res, gr_res
def metric_file_analyse(metric_folder, typ):
    epsilon = '0.0'
    lbd = '1.0E-4'
    action_categories = [
        "jumping", "phoning", "playinginstrument", "reading", "ridingbike",
        "ridinghorse", "running", "takingphoto", "usingcomputer", "walking"
    ]

    detection_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))
    gr_res = collections.defaultdict(lambda: collections.defaultdict(
        lambda: collections.defaultdict(lambda: None)))

    for category in action_categories:
        for tradeoff in ['0.0', '0.1', '0.5', '1.0', '10.0', '100.0']:
            for scale in ["50"]:

                detection_filename = '_'.join([
                    "metric", typ, scale, tradeoff,
                    str(epsilon),
                    str(lbd), category + '_0.txt'
                ])
                fp = os.path.join(metric_folder, detection_filename)
                f = open(fp)

                total_gr = 0
                total_IoU_positive = 0

                total_IoU_negative = 0
                cnt = 0
                tn = 0
                tp = 1
                for line in f:
                    score, yp, yi, hp, image_path = line.strip().split(',')
                    grid_1, grid_2 = metric_calculate.h2GridCoor(
                        hp, int(scale))
                    filename_root = image_path.split("/")[-1].split('.')[0]

                    if yi == '1' and yp == '1':
                        tp += 1
                        cnt += 1
                        bbs = ground_truth_bb_all_action(
                            VOC2012_TRAIN_ANNOTATIONS + filename_root)

                        #                         ratio_file = VOC2012_ACTION_ETLOSS_ACTION+category+'/'+str(metric_calculate.convert_scale(scale))+'/'+filename_root+'_'+str(grid_1)+'_'+str(grid_2)+'.txt'
                        #                         ratio_f = open(ratio_file)
                        #                         ratio = float(ratio_f.readline().strip())
                        #                         ratio_f.close()

                        #                         fixation_ratio+=ratio
                        im = Image.open(VOC2012_TRAIN_IMAGES + filename_root +
                                        '.jpg')
                        width, height = im.size
                        hxmin, hymin, hxmax, hymax = metric_calculate.h2Coor(
                            width, height, hp, int(scale))
                        IoU = metric_calculate.getTopIoU(
                            hxmin, hymin, hxmax, hymax, bbs)
                        total_IoU_positive += IoU
                    if yi == '-1' and yp == '-1':
                        tn += 1
                        cnt += 1
                        bbs = ground_truth_bb_all_action(
                            VOC2012_TRAIN_ANNOTATIONS + filename_root)

                        #                         ratio_file = VOC2012_ACTION_ETLOSS_ACTION+category+'/'+str(metric_calculate.convert_scale(scale))+'/'+filename_root+'_'+str(grid_1)+'_'+str(grid_2)+'.txt'
                        #                         ratio_f = open(ratio_file)
                        #                         ratio = float(ratio_f.readline().strip())
                        #                         ratio_f.close()

                        #                         fixation_ratio+=ratio
                        im = Image.open(VOC2012_TRAIN_IMAGES + filename_root +
                                        '.jpg')
                        width, height = im.size
                        hxmin, hymin, hxmax, hymax = metric_calculate.h2Coor(
                            width, height, hp, int(scale))
                        IoU = metric_calculate.getTopIoU(
                            hxmin, hymin, hxmax, hymax, bbs)
                        total_IoU_negative += IoU
                print "tp:%d, scale:%s, tradeoff:%s, totalIOU:%s" % (
                    tp, scale, tradeoff, total_IoU_positive / tp)
                print "tn:%d, scale:%s, tradeoff:%s, totalIOU:%s" % (
                    tn, scale, tradeoff, total_IoU_negative / tn)