def vis_graph(lang_name, imdb_name, vis):
    if 0:
        vis_bar_top()

    if 1:

        lang_db = get_language_model(lang_name)
        imdb = get_imdb(imdb_name)
        unseen_labels = imdb.get_labels(1)
        unseen_seen_labels = imdb.get_labels(2)
        seen_labels = []
        for s in unseen_seen_labels:
            if s in unseen_labels:
                continue
            seen_labels.append(s)

        lang_db.build_tree(2, unseen_seen_labels, imdb_name)

        #vis_distance(lang_db, imdb, seen_labels)
        vis_top_classes(lang_db, imdb, seen_labels)
Ejemplo n.º 2
0
    parser.add_argument(
        '--lm',
        dest='lang_mod',
        help='Initiate which language model to use with euc_distance',
        default='w2v_wiki_300D',
        type=str)

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    args = parser.parse_args()
    return args


if __name__ == '__main__':
    args = parse_args()
    print('Called with args: ')
    print args

    if args.method == 'pclass':
        evaluate_pr_class(args.fn)
    elif args.method == 'apredict':
        show_actual_predicted(args.fn)
    elif args.method == 'avg_class':
        average_gt_classes(args.fn)
    elif args.method == 'avgeuc':
        average_cosine(args.fn, get_language_model(args.lang_mod))
    else:
        evaluate_flat_map(args.fn)
            cls = words[1].lower()
            if cls not in test_cls:
                test_f.write('%s\n' % cls)
                test_cls.append(cls)
                classes.remove(cls)
        print "Total test classes: {}".format(len(test_cls))

        train_cls = []
        for c in classes:
            if lang_db.word_vector(c) is not None:
                train_cls.append(c)
                train_f.write('%s\n' % c)

        print "Total train classes: {}".format(len(train_cls))


if __name__ == '__main__':

    this_dir = osp.dirname(__file__)
    lib_path = osp.join(this_dir, '../..', 'lib')
    sys.path.insert(0, lib_path)

    from bt_datasets.factory import get_imdb
    from language_models.language_factory import get_language_model

    lang_db = get_language_model('glove_wiki_300D')
    imdb = get_imdb('imagenet1k_val')
    generate_imagenet_single(lang_db, imdb)

    #generate_splitted_classes(lang_db)
Ejemplo n.º 4
0
                        help='randomize (do not use a fixed seed)',
                        action='store_true')
    parser.add_argument('--loss',
                        dest='loss',
                        help='loss function to run',
                        default='hinge',
                        type=str)

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    args = parser.parse_args()
    return args


if __name__ == '__main__':
    args = parse_args()

    print('Called with args:')
    print(args)

    if not args.randomize:
        # fix the random seeds (numpy and caffe) for reproducibility
        np.random.seed(42)

    lang_db = get_language_model(args.lang_name)
    imdb = get_imdb(args.imdb_name)

    train_bts(lang_db, imdb, max_iters=args.max_iters, loss=args.loss)
Ejemplo n.º 5
0
def clean_train_test(lang_name, train_u_s, train_u, test):
    from language_models.language_factory import get_language_model
    lang_db = get_language_model(lang_name)
    print lang_db
    train_u_s_read = open(train_u_s).readlines()
    print "len(train_us)", len(train_u_s_read)
    unique_lbl_t = {}
    unique_lbl_u = {}
    unique_lbl_us = {}

    train_u_read = open(train_u).readlines()
    print "len(train_u)", len(train_u_s_read)

    test_read = open(test).readlines()
    print "len(train)", len(train_u_s_read)


    with open(train_u_s, "w") as train_u_s_file, \
            open(train_u, "w") as train_u_file, \
            open(test, "w") as test_file:
        for img_lbls in train_u_s_read:
            line = img_lbls.split(",")

            img = line[0].strip()
            lbls = [x.strip() for x in line[1:]]
            new_lbls = []
            for lbl in lbls:
                lbl = lbl.strip()
                vec = lang_db.word_vector(lbl)
                if vec is not None:
                    new_lbls.append(lbl)
                    if lbl is not None:
                        unique_lbl_us[lbl] = 1
            img_path = img.split("\\")
            path = img_path[0] + "/" + img_path[1]
            train_u_s_file.write(path + "," + ",".join(set(new_lbls)) + "\n")
            print "s/u", path, new_lbls

        for img_lbls in train_u_read:
            line = img_lbls.split(",")
            img = line[0].strip()
            lbls = [x.strip() for x in line[1:]]
            new_lbls = []
            for lbl in lbls:
                lbl = lbl.strip()
                vec = lang_db.word_vector(lbl.strip())
                if vec is not None:
                    new_lbls.append(lbl.strip())
                    if lbl is not unique_lbl_us:
                        unique_lbl_u[lbl] = 1
            img_path = img.split("\\")
            path = img_path[0] + "/" + img_path[1]
            print "unseen", path, new_lbls
            train_u_file.write(path + "," + ",".join(set(new_lbls)) + "\n")

        for img_lbls in test_read:
            line = img_lbls.split(",")
            img = line[0].strip()
            lbls = [x.strip() for x in line[1:]]
            new_lbls = []

            for lbl in lbls:
                lbl = lbl.strip()
                vec = lang_db.word_vector(lbl)
                if vec is not None:
                    new_lbls.append(lbl)
                    if lbl is not unique_lbl_us:
                        unique_lbl_t[lbl] = 1

            img_path = img.split("\\")
            path = img_path[0] + "/" + img_path[1]
            print "test", path, new_lbls

            test_file.write(path + "," + ",".join(set(new_lbls)) + "\n")