edit : 벡터파일 생성 관련 분리
This commit is contained in:
0
custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/__init__.py
Executable file → Normal file
0
custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/__init__.py
Executable file → Normal file
27
custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/const.py
Executable file → Normal file
27
custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/const.py
Executable file → Normal file
@@ -18,24 +18,23 @@ ViTL14_336 = "clip-vit-large-patch14-336"
|
||||
|
||||
#download/save path
|
||||
PRETRAINED_MODEL_PATH = "./datas"
|
||||
FAISS_VECTOR_PATH = "./datas/Eyewear_001~500_ComponentsCapture_20251121"
|
||||
VECTOR_IMG_LIST_PATH = "./datas/Eyewear_001~500_ComponentsCapture_20251121"
|
||||
|
||||
INDEX_IMAGES_PATH = "./datas/eyewear_all"
|
||||
FAISS_VECTOR_PATH = "./datas"
|
||||
IMG_LIST_PATH = "../Eyewear_data"
|
||||
|
||||
# INDEX_IMAGES_PATH = "./datas/eyewear_all"
|
||||
class VectorSearchItem:
|
||||
glass = "glass"
|
||||
bridge = "Bridge"
|
||||
hinge = "Hinges"
|
||||
lens = "Lens"
|
||||
nosepad = "Nose pad"
|
||||
other = "other"
|
||||
padarm = "Padarm"
|
||||
rim = "Rim"
|
||||
rivet = "Rivet"
|
||||
soltex = "Soltex"
|
||||
tips = "Tips"
|
||||
temple = "Temple"
|
||||
templetip = "Temple tip"
|
||||
rim = "Rim"
|
||||
rim2 = "Rim2"
|
||||
noesPad = "Nose pad"
|
||||
noseArm = "Nose arm"
|
||||
lens = "Lens"
|
||||
endPiece = "End piece"
|
||||
hinges = "Hinges"
|
||||
screw = "Screw"
|
||||
bridge = "Bridge"
|
||||
|
||||
class ImageDepths:
|
||||
parts = "Parts"
|
||||
|
||||
36
custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/faiss_functions.py
Executable file → Normal file
36
custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/faiss_functions.py
Executable file → Normal file
@@ -104,34 +104,28 @@ def get_clip_info(model, query_image_path, item_info, top_k=4):
|
||||
"""
|
||||
이미지 유사도 검색
|
||||
"""
|
||||
index_file_path = os.path.join(VECTOR_IMG_LIST_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{item_info}_{DEFAULT_INDEX_NAME_SUFFIX}')
|
||||
txt_file_path = os.path.join(VECTOR_IMG_LIST_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{item_info}.txt')
|
||||
index_file_path = os.path.join(FAISS_VECTOR_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{item_info}_{DEFAULT_INDEX_NAME_SUFFIX}')
|
||||
txt_file_path = os.path.join(FAISS_VECTOR_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{item_info}.txt')
|
||||
|
||||
if not os.path.exists(index_file_path) or not os.path.exists(txt_file_path):
|
||||
raise FileNotFoundError(f"Index file {index_file_path} or txt file {txt_file_path} does not exist.")
|
||||
|
||||
vector_model = VectorSimilarity(model_info=model,
|
||||
index_file_path=index_file_path,
|
||||
txt_file_path=txt_file_path)
|
||||
|
||||
image_lists = []
|
||||
if not os.path.exists(txt_file_path):
|
||||
if item_info == VectorSearchItem.glass:
|
||||
image_lists = find_glass_folder_images(VECTOR_IMG_LIST_PATH)
|
||||
else:
|
||||
image_lists = find_parts_folder_images(VECTOR_IMG_LIST_PATH, item_info)
|
||||
|
||||
if image_lists:
|
||||
with open(txt_file_path, 'w') as f:
|
||||
for img_path in image_lists:
|
||||
f.write(f"{img_path}\n")
|
||||
else:
|
||||
with open(txt_file_path, 'r') as f:
|
||||
image_lists = [line.strip() for line in f.readlines()]
|
||||
# image_lists = []
|
||||
|
||||
# with open(txt_file_path, 'r') as f:
|
||||
# image_lists = [line.strip() for line in f.readlines()]
|
||||
|
||||
# vector_model.create_feature_extraction_model(FEMUsageInfo.openaiclipvit)
|
||||
|
||||
vector_model.save_index_from_files(images_path_lists=image_lists,
|
||||
save_index_dir=FAISS_VECTOR_PATH,
|
||||
save_txt_dir=VECTOR_IMG_LIST_PATH,
|
||||
item_info=item_info,
|
||||
index_type=model.value[1].index_type)
|
||||
# vector_model.save_index_from_files(images_path_lists=image_lists,
|
||||
# save_index_dir=FAISS_VECTOR_PATH,
|
||||
# save_txt_dir=VECTOR_IMG_LIST_PATH,
|
||||
# item_info=item_info,
|
||||
# index_type=model.value[1].index_type)
|
||||
|
||||
inference_times, result_img_paths, result_percents = vector_model.query_faiss(query_image_path, top_k=top_k)
|
||||
|
||||
|
||||
36
custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/faiss_similarity_search.py
Executable file → Normal file
36
custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/faiss_similarity_search.py
Executable file → Normal file
@@ -372,33 +372,33 @@ class VectorSimilarity:
|
||||
|
||||
return inference_times, result_img_paths, result_percents
|
||||
|
||||
def test():
|
||||
"""
|
||||
module test function
|
||||
"""
|
||||
log.info('\nModule: faiss_similarity_search.py')
|
||||
# def test():
|
||||
# """
|
||||
# module test function
|
||||
# """
|
||||
# log.info('\nModule: faiss_similarity_search.py')
|
||||
|
||||
# index_file_path = f'{FILE_BASE_PATH}openaiclipvit_clip-vit-base-patch32_index.faiss'
|
||||
model = FEMUsageInfo.openaiclip_vit_l14_cos
|
||||
index_file_path = os.path.join(VECTOR_IMG_LIST_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{DEFAULT_INDEX_NAME_SUFFIX}')
|
||||
# # index_file_path = f'{FILE_BASE_PATH}openaiclipvit_clip-vit-base-patch32_index.faiss'
|
||||
# model = FEMUsageInfo.openaiclip_vit_l14_cos
|
||||
# index_file_path = os.path.join(VECTOR_IMG_LIST_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{DEFAULT_INDEX_NAME_SUFFIX}')
|
||||
|
||||
cm = VectorSimilarity(model_info=model,
|
||||
index_file_path=index_file_path,
|
||||
txt_file_path=os.path.join(VECTOR_IMG_LIST_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}.txt'))
|
||||
# cm = VectorSimilarity(model_info=model,
|
||||
# index_file_path=index_file_path,
|
||||
# txt_file_path=os.path.join(VECTOR_IMG_LIST_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}.txt'))
|
||||
|
||||
# cm.create_feature_extraction_model(FEMUsageInfo.openaiclipvit)
|
||||
# # cm.create_feature_extraction_model(FEMUsageInfo.openaiclipvit)
|
||||
|
||||
cm.save_index_from_files(images_path_lists=INDEX_IMAGES_PATH,
|
||||
save_index_dir=FAISS_VECTOR_PATH,
|
||||
save_txt_dir=VECTOR_IMG_LIST_PATH,
|
||||
index_type=model.value[1].index_type)
|
||||
# cm.save_index_from_files(images_path_lists=INDEX_IMAGES_PATH,
|
||||
# save_index_dir=FAISS_VECTOR_PATH,
|
||||
# save_txt_dir=VECTOR_IMG_LIST_PATH,
|
||||
# index_type=model.value[1].index_type)
|
||||
|
||||
|
||||
cm.query_faiss(os.path.join(INDEX_IMAGES_PATH,"img1.png"))
|
||||
# cm.query_faiss(os.path.join(INDEX_IMAGES_PATH,"img1.png"))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
"""
|
||||
test module
|
||||
"""
|
||||
test()
|
||||
# test()
|
||||
|
||||
0
custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/feature_extraction_model.py
Executable file → Normal file
0
custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/feature_extraction_model.py
Executable file → Normal file
0
custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/fem_openaiclipvit.py
Executable file → Normal file
0
custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/fem_openaiclipvit.py
Executable file → Normal file
0
custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/report_utils.py
Executable file → Normal file
0
custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/report_utils.py
Executable file → Normal file
@@ -21,6 +21,12 @@ def search_glass_parts(image_path_list):
|
||||
return result
|
||||
|
||||
def file_name_to_parts(file_path):
|
||||
"""
|
||||
파일경로로 파츠이름 파싱
|
||||
|
||||
:param file_path: 파일경로
|
||||
:return: 파츠이름
|
||||
"""
|
||||
filename, ext = os.path.splitext(os.path.basename(file_path))
|
||||
|
||||
name_list = filename.split('_')
|
||||
@@ -28,4 +34,6 @@ def file_name_to_parts(file_path):
|
||||
result = name_list[2:][0]
|
||||
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(file_name_to_parts(os.path.join(FAISS_VECTOR_PATH,"Glass_001",ImageDepths.parts,"Glass_001_Temple_L.png")))
|
||||
Reference in New Issue
Block a user