45 lines
2.1 KiB
Python
45 lines
2.1 KiB
Python
import json
|
|
import os
|
|
from pprint import pprint
|
|
from tqdm import tqdm
|
|
import numpy as np
|
|
|
|
from app.service.outfit_matcher.dataset import FashionDataset
|
|
from app.service.outfit_matcher.outfit_evaluator import OutfitMaterTypeAware, Backbone
|
|
|
|
if __name__ == '__main__':
|
|
with open("./test_param/recommendation_test.json", "r") as f:
|
|
param = json.load(f)
|
|
fashion_dataset = FashionDataset(param["database"])
|
|
backbone_service = Backbone()
|
|
service = OutfitMaterTypeAware()
|
|
|
|
# read feature from vector database
|
|
all_items = param["query"] + param["database"]
|
|
unextracted_item = []
|
|
prepared_feature = {}
|
|
for item in all_items:
|
|
if f'{item["item_name"]}.npy' not in os.listdir("feature"):
|
|
unextracted_item.append(item)
|
|
if len(unextracted_item) > 0:
|
|
extracted_features = backbone_service.get_result(unextracted_item)
|
|
for i, item in enumerate(unextracted_item):
|
|
np.save(f'feature/{item["item_name"]}.npy', extracted_features[i])
|
|
for item in all_items:
|
|
if item["item_name"] not in prepared_feature.keys():
|
|
prepared_feature[item["item_name"]] = np.load(f'feature/{item["item_name"]}.npy')
|
|
for item in tqdm(param["query"] * 10):
|
|
outfits = fashion_dataset.generate_outfit(item, param["topk"], param["max_outfits"])
|
|
scores = service.get_result(outfits, prepared_feature)
|
|
# print(scores)
|
|
# print(len(scores))
|
|
# service.visualize(outfits, scores, param["topk"], best=True,
|
|
# output_path=os.path.join(r"D:\PhD_Study\MIXI\mitu\image\123",
|
|
# f"{item['item_name']}_best_{param['topk']}.png"))
|
|
# service.visualize(outfits, scores, param["topk"], best=False,
|
|
# output_path=os.path.join(r"D:\PhD_Study\MIXI\mitu\image\123",
|
|
# f"{item['item_name']}_worst_{param['topk']}.png"))
|
|
sorted_indices = np.argsort(scores)[:param["topk"]] # type-aware
|
|
outfits = [outfits[i] for i in sorted_indices] # 最好的五个
|
|
|