Esempio n. 1
0
def do_train(table_name, database_path):
    if not table_name:
        table_name = DEFAULT_TABLE
    cache = Cache(default_cache_dir)
    try:
        vectors, names = feature_extract(database_path, VGGNet())
        print("start connetc to milvus")
        index_client = milvus_client()
        # delete_table(index_client, table_name=table_name)
        # time.sleep(1)
        status, ok = has_table(index_client, table_name)
        if not ok:
            print("create table.")
            create_table(index_client, table_name=table_name)
        print("insert into:", table_name)
        status, ids = insert_vectors(index_client, table_name, vectors)
        create_index(index_client, table_name)
        for i in range(len(names)):
            # cache[names[i]] = ids[i]
            cache[ids[i]] = names[i]
        print("Train finished")
        return "Train finished"
    except Exception as e:
        logging.error(e)
        return "Error with {}".format(e)
Esempio n. 2
0
def do_train(table_name, data_loader, model, args):
    if not table_name:
        table_name = DEFAULT_TABLE
    cache = Cache(default_cache_dir)
    try:
        vectors, names = feature_extract(data_loader, model, args)
        vectors = vectors / vectors.norm()
        print(vectors.shape)
        vectors = vectors.tolist()
        index_client = milvus_client()
        # delete_table(index_client, table_name=table_name)
        # time.sleep(1)
        status, ok = has_table(index_client, table_name)
        if not ok:
            print("create table.")
            create_table(index_client, table_name=table_name)
        print("insert into:", table_name)
        status, ids = insert_vectors(index_client, table_name, vectors)
        create_index(index_client, table_name)
        for i in range(len(names)):
            # cache[names[i]] = ids[i]
            cache[ids[i]] = names[i]
        print("Train finished")
        return "Train finished"
    except Exception as e:
        logging.error(e)
        return "Error with {}".format(e)
def do_load(table_name, database_path):
    if not table_name:
        table_name = DEFAULT_TABLE
    cache = Cache(default_cache_dir)
    try:
        vectors, names = feature_extract(table_name, database_path)
        print("start connetc to milvus")
        index_client = milvus_client()
        status, ok = has_table(index_client, table_name)
        if not ok:
            print("create table.")
            create_table(index_client, table_name=table_name)
        print("insert into:", table_name)

        # status, ids = insert_vectors(index_client, table_name, vectors)
        total_ids = []
        ids_lens = 0
        while ids_lens<len(vectors) :
            try:
                status, ids = insert_vectors(index_client, table_name, vectors[ids_lens:ids_lens+100000])
            except:
                status, ids = insert_vectors(index_client, table_name, vectors[ids_lens:len(vectors)])
            ids_lens += 100000
            total_ids += ids
            print("ids:",len(ids))

        create_index(index_client, table_name)
        for i in range(len(names)):
            cache[total_ids[i]] = names[i]
        print("FP finished")
        return "FP finished"
    except Exception as e:
        logging.error(e)
        return "Error with {}".format(e)
def init_table(index_client, conn, cursor, milvus_table=MILVUS_TABLE):
    status, ok = has_table(index_client, milvus_table)
    if not ok:
        print("create table.")
        create_table(index_client, milvus_table)
        create_index(index_client, milvus_table)
        create_table_mysql(conn, cursor, milvus_table)
Esempio n. 5
0
def init_table(index_client, conn, cursor, table_name):
    status, ok = has_table(index_client, table_name)
    print("has_table:", status, ok)
    if not ok:
        print("create table.")
        create_table(index_client, table_name)
        create_index(index_client, table_name)
        create_table_mysql(conn, cursor, table_name)
Esempio n. 6
0
def do_train(table_name, database_path):
    detector = Detector()
    if not table_name:
        table_name = DEFAULT_TABLE
    cache = Cache(default_cache_dir)
    try:
        result_images, object_num = run(detector, database_path)
        #print("after detect:", object_num)
        vectors, obj_images = get_object_vector(cache, image_encoder,
                                                database_path + "/object")
        #print("after detect:", len(vectors), obj_images)
        index_client = milvus_client()
        status, ok = has_table(index_client, table_name)
        if not ok:
            print("create table.")
            create_table(index_client, table_name=table_name)
        print("insert into:", table_name)
        # vectors = normaliz_vec(vectors)
        status, ids = insert_vectors(index_client, table_name, vectors)
        #print(status,ids)
        create_index(index_client, table_name)
        shutil.rmtree(database_path + "/object")
        imgs = os.listdir(database_path)
        imgs.sort()
        #print("-----imgs", imgs)
        k = 0
        ids = list(reversed(ids))
        #print("ids", ids)
        for num in object_num:
            for i in range(num):
                a = ids.pop()
                #print("real;;;;;;;;;",a, imgs[k])
                cache[a] = imgs[k]
            k += 1
        return print("train finished")
    except Exception as e:
        logging.error(e)
        return "Error with {}".format(e)
Esempio n. 7
0
def curd(vectors, img_name, mycol, partition=None, table_name=DEFAULT_TABLE):
    try:
        client = milvus_client()
        if not table_name:
            table_name = DEFAULT_TABLE
        status, ok = has_table(client, table_name)
        if not ok:
            print('开始创建table')
            create_table(client, table_name)
        if partition:
            status, ok = has_partition(client, table_name, partition)
            if not ok:
                create_partition(client, table_name, partition)
        status, id = insert_vectors(client, table_name, vectors, partition)
        # 存入缓存 以便后续进行反查
        # redis.hset(REDIS_NAME, id[0], img_name)
        mycol.insert_one({'id': id[0], 'img': img_name, 'table': table_name})
        create_index(client, table_name)
        print('OK 了')
        return True, ''
    except Exception as e:
        logging.error(e)
        return False, e