forked from luigifreda/pyslam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_delf.py
288 lines (225 loc) · 11.8 KB
/
feature_delf.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""
* This file is part of PYSLAM
* Adapted from https://github.com/tensorflow/models/blob/master/research/delf/delf/python/examples/extract_features.py, see the license therein.
*
* Copyright (C) 2016-present Luigi Freda <luigi dot freda at gmail dot com>
*
* PYSLAM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PYSLAM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PYSLAM. If not, see <http://www.gnu.org/licenses/>.
"""
import config
config.cfg.set_lib('delf')
import cv2
from threading import RLock
from utils_sys import Printer
import warnings # to disable tensorflow-numpy warnings: from https://github.com/tensorflow/tensorflow/issues/30427
warnings.filterwarnings('ignore', category=FutureWarning)
import argparse
import os
import sys
import time
import json
import numpy as np
import h5py
if False:
import tensorflow as tf
else:
# from https://stackoverflow.com/questions/56820327/the-name-tf-session-is-deprecated-please-use-tf-compat-v1-session-instead
import tensorflow.compat.v1 as tf
# from https://kobkrit.com/using-allow-growth-memory-option-in-tensorflow-and-keras-dc8c8081bc96 to cope with the following error:
# "[...tensorflow/stream_executor/cuda/cuda_dnn.cc:329] Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR"
tf_config = tf.ConfigProto()
tf_config.gpu_options.allow_growth = True
tf_config.gpu_options.per_process_gpu_memory_fraction=0.333 # from https://stackoverflow.com/questions/34199233/how-to-prevent-tensorflow-from-allocating-the-totality-of-a-gpu-memory
#session = tf.Session(config=tf_config, ...)
from google.protobuf import text_format
from tensorflow.python.platform import app
# from delf import delf_config_pb2
# from delf import feature_extractor
# from delf import feature_io
from delf.protos import aggregation_config_pb2
from delf.protos import box_pb2
from delf.protos import datum_pb2
from delf.protos import delf_config_pb2
from delf.protos import feature_pb2
from delf.python import box_io
from delf.python import datum_io
from delf.python import delf_v1
from delf.python import feature_aggregation_extractor
from delf.python import feature_aggregation_similarity
from delf.python import feature_extractor
from delf.python import feature_io
from delf.python.examples import detector
from delf.python.examples import extractor
from delf.python import detect_to_retrieve
from delf.python import google_landmarks_dataset
from utils_tf import set_tf_logging
#from utils import print_options
delf_base_path = config.cfg.root_folder + '/thirdparty/tensorflow_models/research/delf/delf/python/'
delf_config_file = delf_base_path + 'examples/delf_config_example.pbtxt'
delf_model_path = delf_base_path + 'examples/parameters/delf_gld_20190411/model/'
delf_mean_path = delf_base_path + 'examples/parameters/delf_gld_20190411/pca/mean.datum'
delf_projection_matrix_path = delf_base_path + 'examples/parameters/delf_gld_20190411/pca/pca_proj_mat.datum'
kVerbose = True
def MakeExtractor(sess, config, import_scope=None):
"""Creates a function to extract features from an image.
Args:
sess: TensorFlow session to use.
config: DelfConfig proto containing the model configuration.
import_scope: Optional scope to use for model.
Returns:
Function that receives an image and returns features.
"""
tf.saved_model.loader.load( sess, [tf.saved_model.tag_constants.SERVING], config.model_path, import_scope=import_scope)
import_scope_prefix = import_scope + '/' if import_scope is not None else ''
input_image = sess.graph.get_tensor_by_name('%sinput_image:0' % import_scope_prefix)
input_score_threshold = sess.graph.get_tensor_by_name('%sinput_abs_thres:0' % import_scope_prefix)
input_image_scales = sess.graph.get_tensor_by_name('%sinput_scales:0' % import_scope_prefix)
input_max_feature_num = sess.graph.get_tensor_by_name('%sinput_max_feature_num:0' % import_scope_prefix)
boxes = sess.graph.get_tensor_by_name('%sboxes:0' % import_scope_prefix)
raw_descriptors = sess.graph.get_tensor_by_name('%sfeatures:0' % import_scope_prefix)
feature_scales = sess.graph.get_tensor_by_name('%sscales:0' % import_scope_prefix)
attention_with_extra_dim = sess.graph.get_tensor_by_name('%sscores:0' % import_scope_prefix)
attention = tf.reshape(attention_with_extra_dim,[tf.shape(attention_with_extra_dim)[0]])
locations, descriptors = feature_extractor.DelfFeaturePostProcessing(boxes, raw_descriptors, config)
def ExtractorFn(image):
"""Receives an image and returns DELF features.
Args:
image: Uint8 array with shape (height, width 3) containing the RGB image.
Returns:
Tuple (locations, descriptors, feature_scales, attention)
"""
return sess.run([locations, descriptors, feature_scales, attention],
feed_dict={
input_image: image,
input_score_threshold:
config.delf_local_config.score_threshold,
input_image_scales: list(config.image_scales),
input_max_feature_num:
config.delf_local_config.max_feature_num
})
return ExtractorFn
# convert matrix of pts into list of keypoints
def convert_pts_to_keypoints(pts, scores, sizes):
assert(len(pts)==len(scores))
kps = []
if pts is not None:
# convert matrix [Nx2] of pts into list of keypoints
kps = [ cv2.KeyPoint(p[0], p[1], _size=sizes[i], _response=scores[i]) for i,p in enumerate(pts) ]
return kps
# interface for pySLAM
class DelfFeature2D:
def __init__(self,
num_features=1000,
score_threshold=100,
do_tf_logging=False):
print('Using DelfFeature2D')
self.lock = RLock()
set_tf_logging(do_tf_logging)
# Parse DelfConfig proto.
self.delf_config = delf_config_pb2.DelfConfig()
with tf.gfile.FastGFile(delf_config_file, 'r') as f:
text_format.Merge(f.read(), self.delf_config)
self.delf_config.model_path = delf_model_path
self.delf_config.delf_local_config.pca_parameters.mean_path = delf_mean_path
self.delf_config.delf_local_config.pca_parameters.projection_matrix_path = delf_projection_matrix_path
self.delf_config.delf_local_config.max_feature_num = num_features
self.delf_config.delf_local_config.score_threshold = score_threshold
print('DELF CONFIG\n:', self.delf_config)
self.keypoint_size = 30 # just a representative size for visualization and in order to convert extracted points to cv2.KeyPoint
self.image_scales = list(self.delf_config.image_scales)
#print('image scales: ',self.image_scales)
try:
self.scale_factor = self.image_scales[1]/self.image_scales[0]
except:
self.scale_factor = np.sqrt(2) # according to default config and the paper
#print('scale_factor: ',self.scale_factor)
#self.image_levels = np.round(-np.log(self.image_scales)/np.log(self.scale_factor)).astype(np.int32)
#print('image levels: ',self.image_levels)
self.session = None
self.pts = []
self.kps = []
self.des = []
self.scales = []
self.scores = []
self.frame = None
print('==> Loading pre-trained network.')
self.load_model()
print('==> Successfully loaded pre-trained network.')
@property
def num_features(self):
return self.delf_config.delf_local_config.max_feature_num
@property
def score_threshold(self):
return self.delf_config.delf_local_config.score_threshold
def __del__(self):
self.close()
def load_model(self):
# Create graph before session :)
self.graph = tf.Graph().as_default()
self.session = tf.Session()
init_op = tf.global_variables_initializer()
self.session.run(init_op)
self.extractor_fn = MakeExtractor(self.session, self.delf_config)
def close(self):
if self.session is not None:
print('DELF: closing tf session')
self.session.close()
tf.reset_default_graph()
def compute_kps_des(self, frame):
with self.lock:
image_tf = tf.convert_to_tensor(frame, np.float32)
im = self.session.run(image_tf)
# Extract and save features.
(locations_out, descriptors_out, feature_scales_out, attention_out) = self.extractor_fn(im)
self.pts = locations_out[:, ::-1]
self.des = descriptors_out
self.scales = feature_scales_out
self.scores = attention_out
# N.B.: according to the paper "Large-Scale Image Retrieval with Attentive Deep Local Features":
# We construct image pyramids by using scales that are a 2 factor apart. For the set of scales
# with range from 0.25 to 2.0, 7 different scales are used.
# The size of receptive field is inversely proportional to the scale; for example, for the 2.0 scale, the
# receptive field of the network covers 146 × 146 pixels.
# The receptive field size for the image at the original scale is 291 × 291.
#sizes = self.keypoint_size * 1./self.scales
sizes = self.keypoint_size * self.scales
if False:
# print('kps.shape', self.pts.shape)
# print('des.shape', self.des.shape)
# print('scales.shape', self.scales.shape)
# print('scores.shape', self.scores.shape)
print('scales:',self.scales)
print('sizes:',sizes)
self.kps = convert_pts_to_keypoints(self.pts, self.scores, sizes)
return self.kps, self.des
def detectAndCompute(self, frame, mask=None): #mask is a fake input
with self.lock:
self.frame = frame
self.kps, self.des = self.compute_kps_des(frame)
if kVerbose:
print('detector: DELF, descriptor: DELF, #features: ', len(self.kps), ', frame res: ', frame.shape[0:2])
return self.kps, self.des
# return keypoints if available otherwise call detectAndCompute()
def detect(self, frame, mask=None): # mask is a fake input
with self.lock:
#if self.frame is not frame:
self.detectAndCompute(frame)
return self.kps
# return descriptors if available otherwise call detectAndCompute()
def compute(self, frame, kps=None, mask=None): # kps is a fake input, mask is a fake input
with self.lock:
if self.frame is not frame:
Printer.orange('WARNING: DELF is recomputing both kps and des on last input frame', frame.shape)
self.detectAndCompute(frame)
return self.kps, self.des