-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmilvus2_kam_create_vectors.py
177 lines (143 loc) · 5.97 KB
/
milvus2_kam_create_vectors.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import pymssql
from PIL import Image
import requests
import numpy as np
from scipy import spatial
from sklearn.preprocessing import minmax_scale
import json
import time
import logging
from pymilvus import (
connections,
utility,
FieldSchema, CollectionSchema, DataType,
Collection,
)
# Gets or creates a logger
logger = logging.getLogger(__name__)
# set log level
logger.setLevel(logging.DEBUG)
# define file handler and set formatter
file_handler = logging.FileHandler('log_KAM_create_vector.log')
formatter = logging.Formatter('%(asctime)s : %(levelname)s : %(name)s : %(message)s')
file_handler.setFormatter(formatter)
# add file handler to logger
logger.addHandler(file_handler)
conn = None
GAP = 60 # seconds to sleep between the loop steps
# handle milvus collection
print("start connecting to Milvus")
connections.connect("default", host="localhost", port="19530")
has = utility.has_collection("artifact")
print(f"Does collection artifact exist in Milvus: {has}")
fields = [
FieldSchema(name="pk", dtype=DataType.INT64, is_primary=True, auto_id=False),
FieldSchema(name="artifact_type", dtype=DataType.INT64),
FieldSchema(name="embeddings", dtype=DataType.FLOAT_VECTOR, dim=2048)
]
# description as "artifact" below is mandatory to connect
schema = CollectionSchema(fields, "artifact")
print("Collection artifact")
artifact = Collection("artifact", schema, consistency_level="Strong")
# Model REST API - tf serving - predict service URL
# tf_serving_url = 'http://localhost:8501/v1/models/similarityModel:predict'
tf_serving_url = 'http://localhost:8501/v1/models/resnet50:predict'
headers = {"content-type": "application/json"}
# mount path to access the file Server
# fs = "/mnt/muesfs/mues-images/image/ak/" # prod
fs = "/mnt/muesfs/mues/mueskam-images/dev/image/ak/" # dev
fs_mues = "/mnt/muesfs/mues/mues-images/dev/image/ak/"
# MILVUS REST API URL
#milvus_url = 'http://localhost:19121/collections/artifact/vectors'
#milvus_url = 'http://localhost:19121/collections/kam/vectors'
def prepare_image(img, target_size=(224,224)):
img = img.resize(target_size)
if img.mode != 'RGB':
img = img.convert('RGB')
img = np.array(img)
img = np.expand_dims(img, axis=0)
# img = preprocess_input(img)
return img
def connect_to_db():
global conn
try:
conn = pymssql.connect(server='10.1.37.177', port='1033', user='muest', password='Mues*test.1', database='mues_test')
logger.info('DB connected successfully')
except Exception as e:
logger.critical(e)
def create_top_n_vectors():
cursor = conn.cursor()
cursor.execute("select DISTINCT TOP 100 K.uid, F.FOTOGRAF_PATH, F.artifactId from KAM_ARTIFACT_VIEW K "
"LEFT JOIN Kam_ArtifactPhotograph F ON K.artifactId = F.artifactId "
"WHERE K.artifactType!='INVENTORY_ARTIFACT' AND K.aktif=1 AND K.silinmis=0 AND F.ANA_FOTOGRAF=1 AND F.FOTOGRAF_PATH is not null AND F.FEATURE_VECTOR_STATE is NULL ORDER BY K.uid")
records = cursor.fetchall()
ids = []
vectors = []
artifact_types = []
ok_list = []
err_list = []
for row in records:
try:
logger.info("uid:" + str(row[0]) + " : " + str(row[1]))
print(("uid: " + str(row[0]) + " : " + str(row[1])))
img = Image.open(fs + row[1])
img_data = prepare_image(img)
# prepare for tf serving service
# give the photo and get the vector from the model
data = json.dumps({"signature_name": "serving_default", "instances": img_data.tolist()})
response = requests.post(tf_serving_url, data=data, headers=headers)
dict_resp = json.loads(response.text)
feature_np = np.array(dict_resp["predictions"])
# min-max scale the data between 0 and 1
scaled_vec = minmax_scale(feature_np.flatten())
result_vec = np.round(scaled_vec, 2)
# print(result_vec)
# for milvus request
ids.append(row[0])
# KAM artifact_type = 2, MUES artifact_type = 1
artifact_types.append(2)
# print(artifact_types)
vectors.append(result_vec.tolist())
ok_list.append(str(row[2]))
except (FileNotFoundError, IOError):
logger.error("File not found: " + fs + row[1])
err_list.append(str(row[0])) # marking for FileNotFound
except ValueError as e:
logger.error("Decoding JSON has failed")
logger.error(e)
except (requests.HTTPError, requests.RequestException) as e:
logger.error("HTTP/Request error occurred")
logger.error(e)
try:
# save the n vector to the Milvus DB
if(len(vectors)):
entities = [ids, artifact_types, vectors]
insert_result = artifact.insert(entities)
except Exception as e:
logger.error("MILVUS post request error (KAM)")
logger.error(e)
try:
# commit for top N selected records
if(len(ok_list)>0):
cursor.execute("UPDATE Kam_ArtifactPhotograph set FEATURE_VECTOR_STATE='1' where ANA_FOTOGRAF=1 AND artifactId in {}".format(str(tuple(ok_list)).replace(',)', ')')))
if(len(err_list)>0):
cursor.execute("UPDATE Kam_ArtifactPhotograph set FEATURE_VECTOR_STATE='-1' where ANA_FOTOGRAF=1 AND artifactId in {}".format(str(tuple(err_list)).replace(',)', ')')))
conn.commit()
except Exception as e:
logger.error(e)
logger.info("Trying to reconnect to the DB...")
conn.close()
connect_to_db()
return len(records)
def create_all():
while True:
records_len = create_top_n_vectors()
print(str(records_len) + " KAM_vectors created successfully")
logger.info(str(records_len) + " KAM_vectors created successfully")
time.sleep(GAP)
# if records_len == 0:
# logger.info("No record found to get the KAM vector")
# break
if __name__ == "__main__":
connect_to_db()
create_all()