forked from PyRetri/PyRetri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_index.py
69 lines (50 loc) · 2.34 KB
/
single_index.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
# -*- coding: utf-8 -*-
import argparse
import os
from PIL import Image
import numpy as np
from pyretri.config import get_defaults_cfg, setup_cfg
from pyretri.datasets import build_transformers
from pyretri.models import build_model
from pyretri.extract import build_extract_helper
from pyretri.index import build_index_helper, feature_loader
def parse_args():
parser = argparse.ArgumentParser(description='A tool box for deep learning-based image retrieval')
parser.add_argument('opts', default=None, nargs=argparse.REMAINDER)
parser.add_argument('--config_file', '-cfg', default=None, metavar='FILE', type=str, help='path to config file')
args = parser.parse_args()
return args
def main():
# init args
args = parse_args()
assert args.config_file is not "", 'a config file must be provided!'
assert os.path.exists(args.config_file), 'the config file must be existed!'
# init and load retrieval pipeline settings
cfg = get_defaults_cfg()
cfg = setup_cfg(cfg, args.config_file, args.opts)
# set path for single image
path = '/data/caltech101/query/airplanes/image_0004.jpg'
# build transformers
transformers = build_transformers(cfg.datasets.transformers)
# build model
model = build_model(cfg.model)
# read image and convert it to tensor
img = Image.open(path).convert("RGB")
img_tensor = transformers(img)
# build helper and extract feature for single image
extract_helper = build_extract_helper(model, cfg.extract)
img_fea_info = extract_helper.do_single_extract(img_tensor)
stacked_feature = list()
for name in cfg.index.feature_names:
assert name in img_fea_info[0], "invalid feature name: {} not in {}!".format(name, img_fea_info[0].keys())
stacked_feature.append(img_fea_info[0][name])
img_fea = np.concatenate(stacked_feature, axis=1)
# load gallery features
gallery_fea, gallery_info, _ = feature_loader.load(cfg.index.gallery_fea_dir, cfg.index.feature_names)
# build helper and single index feature
index_helper = build_index_helper(cfg.index)
index_result_info, query_fea, gallery_fea = index_helper.do_index(img_fea, img_fea_info, gallery_fea)
index_helper.save_topk_retrieved_images('retrieved_images/', index_result_info[0], 5, gallery_info)
print('single index have done!')
if __name__ == '__main__':
main()