def merge():
    splits_id_path = '../data/splits'
    vcoco_train = np.loadtxt(os.path.join(splits_id_path, 'vcoco_train.ids'))
    print(len(vcoco_train))
    train_list = [str(int(id)) for id in vcoco_train]

    vcoco_val = np.loadtxt(os.path.join(splits_id_path, 'vcoco_val.ids'))
    print(len(vcoco_val))
    val_list = [str(int(id)) for id in vcoco_val]

    vcoco_test = np.loadtxt(os.path.join(splits_id_path, 'vcoco_test.ids'))
    print(len(vcoco_test))
    test_list = [str(int(id)) for id in vcoco_test]

    vcoco_trainval = np.loadtxt(
        os.path.join(splits_id_path, 'vcoco_trainval.ids'))
    print(len(vcoco_trainval))
    trainval_list = [str(int(id)) for id in vcoco_trainval]

    d = {}
    d["train"] = train_list
    d["val"] = val_list
    d["test"] = test_list
    d["trainval"] = trainval_list

    stats = {}
    stats["test"] = len(test_list)
    stats["train"] = len(train_list)
    stats["trainval"] = len(trainval_list)
    stats["val"] = len(val_list)

    io.dump_json_object(d, 'data_process/split_ids_all.json')
    io.dump_json_object(stats, 'data_process/split_ids_stats_all.json')
Ejemplo n.º 2
0
def save_hois_to_json(data_dict):
    data_dict = dict(
        sorted(data_dict.items(), key=lambda d: d[0], reverse=False))
    id = 1
    data_list = []
    rare_list = []
    non_rare_list = []
    for action_ob_key in data_dict.keys():
        name = action_ob_key.split('/')
        assert len(name) == 2
        action = name[0]
        ob = name[1]
        set_num_dict = data_dict[action_ob_key]
        temp = {
            'id': str(id).zfill(3),
            'verb': action,
            'object': ob,
            'train_num': set_num_dict.get('train', 0),
            'val_num': set_num_dict.get('val', 0),
            'test_num': set_num_dict.get('test', 0),
            'rare': True if set_num_dict.get('train', 0) < 10 else False
        }
        if temp['rare']:
            rare_list.append(id)
        else:
            non_rare_list.append(id)

        id = id + 1
        data_list.append(temp)

    print(data_list)
    print(type(data_list))
    print(type(data_list[0]))

    file = os.path.join(os.getcwd(), 'hois', 'hoi_list.json')
    io.dump_json_object(data_list, file)
Ejemplo n.º 3
0
def check():
    anno_file = os.path.join("data_process", 'anno_list_gt.json')
    anno_list = io.load_json_object(anno_file)
    global_ids = [anno['global_id'] for anno in anno_list]
    print(len(global_ids))

    ids = io.load_json_object(
        os.path.join("data_process", 'split_ids_all.json'))

    train_lost = []
    val_lost = []
    test_lost = []

    train_list = []
    val_list = []
    test_list = []
    trainval_list = []

    for id in ids['train']:
        if not id in global_ids:
            print("train", id)
            train_lost.append(id)
        else:
            train_list.append(id)
            trainval_list.append(id)

    for id in ids['val']:
        if not id in global_ids:
            print("val", id)
            val_lost.append(id)
        else:
            val_list.append(id)
            trainval_list.append(id)

    for id in ids['test']:
        if not id in global_ids:
            print("test", id)
            test_lost.append(id)
        else:
            test_list.append(id)

    d = {}
    d['train'] = train_lost
    d["val"] = val_lost
    d['test'] = test_lost
    io.dump_json_object(d, 'data_process/id_lost.json')

    d = {}
    d['train'] = len(train_lost)
    d["val"] = len(val_lost)
    d['test'] = len(test_lost)
    io.dump_json_object(d, 'data_process/id_lost_stats.json')

    d = {}
    d['train'] = train_list
    d["val"] = val_list
    d['test'] = test_list
    d['trainval_list'] = trainval_list
    io.dump_json_object(d, 'data_process/split_ids.json')

    d = {}
    d['train'] = len(train_list)
    d["val"] = len(val_list)
    d['test'] = len(test_list)
    d['trainval_list'] = len(trainval_list)
    io.dump_json_object(d, 'data_process/split_ids_stats.json')
Ejemplo n.º 4
0
        category_ids_list2 = merge_two_list(trainval_list[4], test_list[4])
        name_list2 = get_names(coco, category_ids_list2)
        # print(name_list2)

    print("1.Action:", trainval_list[6])
    print('2.Roles:', "1" if len(trainval_list[5]) == 2 else "2")
    print("3.numbers of example:", trainval_list[0] + test_list[0])

    result = {}
    for i in range(1, len(trainval_list[5])):
        print("4.", trainval_list[5][i])
        print("5.", trainval_list[1] + test_list[1] if i == 1 else trainval_list[2] + test_list[2])
        print("6.Objects in role:", trainval_list[5][i], name_list1 if i == 1 else name_list2)
        result[trainval_list[5][i]] = name_list1 if i == 1 else name_list2
    dict[trainval_list[6]] = result
    return dict


if __name__ == '__main__':
    coco = COCO(os.path.join('coco/annotations/', 'instances_trainval2014.json'))
    v_coco = vu.load_coco()
    dict = {}

    # get_table1_one_row(action='sit')
    for i in range(26):
        result = get_table1_one_row(v_coco, coco, action_index=i, dict=dict)

    assert len(dict) == 26

    io.dump_json_object(dict, "table_list.json")
Ejemplo n.º 5
0
def generate_json_data_for_vcoco_total_with_gt():
    hoi_list = json.load(
        open(os.path.join(os.getcwd(), 'data_process', 'hoi_list.json'), 'r'))
    coco = COCO(
        os.path.join('..', 'coco/annotations/', 'instances_trainval2014.json'))
    anno_list = []
    verb_set = set()
    object_set = set()

    for dataset_name in ["vcoco_train", "vcoco_val", "vcoco_test"]:
        vcoco_data = vu.load_vcoco(dataset_name)
        # train_data add bbox and role_bbox
        for x in vcoco_data:
            x = vu.attach_gt_boxes(x, coco)

        assert len(vcoco_data) == 26
        for data in vcoco_data:
            # print(data.keys())
            verb_set.add(data['action_name'])

            image_ids = data['image_id']
            for i in range(len(image_ids)):
                assert data['role_object_id'][i][0] == data['ann_id'][i]

                if data['label'][i] == 0:
                    continue
                path = 'train' if 'train' in data['file_name'][i] else 'val'
                global_id = "COCO_" + path + "2014_" + str(
                    image_ids[i][0]).zfill(12)
                image_path_postfix = os.path.join(path + '2014',
                                                  global_id + '.jpg')

                global_id = str(image_ids[i][0])

                human_bboxes = data['bbox'][i]

                assert len(human_bboxes) == 4
                # get image size
                image_size = data['image_size'][i]
                assert len(image_size) == 2

                hois = []
                assert len(data['role_name']) <= 3
                for j in range(1, len(data['role_name'])):
                    object_anno_id = data['role_object_id'][i][j]
                    if object_anno_id == 0:
                        continue
                    d = {}
                    object_anno = coco.loadAnns(int(object_anno_id))[0]
                    object_bboxes = data['role_bbox'][i][4 * j:4 * (j + 1)]

                    cat_id = object_anno['category_id']
                    object_name = coco.loadCats(int(cat_id))[0]['name']
                    object_set.add(object_name)

                    id = -1
                    for k in hoi_list:
                        if k['action'] == data['action_name'] and k[
                                'object'] == object_name:
                            id = k['id']
                            break
                    assert id > 0
                    d['object'] = object_name
                    d['human_bboxes'] = human_bboxes
                    d['id'] = id
                    d['invis'] = 0
                    d['action'] = data['action_name']
                    d['object_bboxes'] = object_bboxes
                    hois.append(d)

                if hois == []:
                    # print(i, data["action_name"], data['label'][i], data['role_object_id'][i])
                    continue
                d_per_image = {}
                d_per_image['global_id'] = global_id
                d_per_image['hois'] = hois
                d_per_image['image_path_postfix'] = image_path_postfix
                d_per_image['image_size'] = image_size
                anno_list.append(d_per_image)

        print(dataset_name, len(anno_list))

    anno_dict = {}
    print(len(anno_list))

    for item in anno_list:
        assert item['hois'] is not None

        if item['global_id'] not in anno_dict:
            anno_dict[item['global_id']] = item
        else:
            anno_dict[item['global_id']]['hois'].append(item['hois'][0])

    print(len(anno_dict))
    anno_list = []
    for _, value in anno_dict.items():
        anno_list.append(value)

    io.dump_json_object(anno_list, 'data_process/' + 'anno_list_gt.json')

    ob_index = 1
    object_set = sorted(object_set)
    object_list = []
    for ob in object_set:
        object_list.append({"id": str(ob_index).zfill(3), "name": ob})
        ob_index += 1
    io.dump_json_object(object_list, 'data_process/' + 'object_list_gt.json')

    vb_index = 1
    verb_set = sorted(verb_set)
    verb_list = []
    for verb in verb_set:
        verb_list.append({"id": str(vb_index).zfill(3), "name": verb})
        vb_index += 1
    io.dump_json_object(verb_list, 'data_process/' + 'verb_list_gt.json')