-
Notifications
You must be signed in to change notification settings - Fork 1
/
load_coco.py
69 lines (59 loc) · 1.84 KB
/
load_coco.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import psycopg2
import os
import time
from concurrent.futures import ProcessPoolExecutor
def get_files(directory_path):
filenames = os.listdir(directory_path)
filenames = [f for f in filenames if os.path.isfile(os.path.join(directory_path, f))]
return filenames
def insert_into_db(directory_path, im):
conn = psycopg2.connect(
host="localhost",
database="",
user="",
password="",
port=5432
)
cur = conn.cursor()
path = f'{directory_path}/{im}'
insert_query = f'INSERT INTO image_table (v, location) VALUES (clip_image(\'{path}\'), \'{path}\');'
cur.execute(insert_query)
conn.commit()
conn.close()
def chunk(l, n):
for i in range(0, len(l), n):
yield l[i:i + n]
if __name__=='__main__':
directory_path = f'{os.getcwd()}/unlabeled2017'
files = get_files(directory_path)
conn = psycopg2.connect(
host="localhost",
database="",
user="",
password="",
port=5432
)
cur = conn.cursor()
cur.execute('''
CREATE TABLE IF NOT EXISTS image_table (
v REAL[],
location VARCHAR,
id SERIAL PRIMARY KEY
);''')
conn.commit()
cur.execute('''
CREATE INDEX semantic_image ON image_table
USING hnsw (v dist_cos_ops)
WITH (M=5, ef=30, ef_construction=30, dims=512
);''')
conn.commit()
conn.close()
abs_start = time.time()
for i, sub in enumerate(chunk(files, 1000)):
start = time.time()
def process(im):
insert_into_db(directory_path, im)
with ProcessPoolExecutor(max_workers=16) as executor:
executor.map(process, sub)
end = time.time()
print(f'completed {1000*i}-{1000*(i+1)} in {end-start} second, {end-abs_start} elapsed so far')