Skip to content

Commit fa1e937

Browse files
committed
refactor: unify sift GPU feature with current ones.Merged normalization/rooting functions.Removed seperate storage
1 parent 1152881 commit fa1e937

4 files changed

Lines changed: 10 additions & 50 deletions

File tree

opensfm/commands/detect_features.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,10 @@ def detect(args):
6969
data.feature_type().upper(), image))
7070

7171
start = timer()
72-
if data.config["feature_type"] == "SIFT_GPU":
73-
unmasked, keypoints = features.extract_features(
74-
data.load_image(image), data.config)
75-
p_unmasked, f_unmasked, c_unmasked = unmasked
76-
else:
77-
p_unmasked, f_unmasked, c_unmasked = features.extract_features(
78-
data.load_image(image), data.config)
72+
p_unmasked, f_unmasked, c_unmasked = features.extract_features(
73+
data.load_image(image), data.config)
7974

8075
fmask = data.load_features_mask(image, p_unmasked)
81-
if data.config["feature_type"] == "SIFT_GPU":
82-
keypoints = keypoints[fmask]
8376
p_unsorted = p_unmasked[fmask]
8477
f_unsorted = f_unmasked[fmask]
8578
c_unsorted = c_unmasked[fmask]
@@ -93,9 +86,6 @@ def detect(args):
9386
p_sorted = p_unsorted[order, :]
9487
f_sorted = f_unsorted[order, :]
9588
c_sorted = c_unsorted[order, :]
96-
if data.config["feature_type"] == "SIFT_GPU":
97-
keypoints_sorted = keypoints[order]
98-
data.save_gpu_features(image, keypoints_sorted)
9989
data.save_features(image, p_sorted, f_sorted, c_sorted)
10090

10191
if need_words:

opensfm/dataset.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -328,21 +328,6 @@ def load_features(self, image):
328328
def save_features(self, image, points, descriptors, colors):
329329
self._save_features(self._feature_file(image), points, descriptors, colors)
330330

331-
def save_gpu_features(self, image, keypoints):
332-
io.mkdir_p(self._feature_path())
333-
path = "./"+self._gpu_feature_file(image)
334-
# Store data (serialize)
335-
with open(path, 'wb+') as handle:
336-
pickle.dump(keypoints, handle, protocol=pickle.HIGHEST_PROTOCOL)
337-
338-
def load_gpu_features(self, image):
339-
path = self._gpu_feature_file(image)
340-
if os.path.isfile(path):
341-
with open(path, 'rb') as handle:
342-
keypoints = pickle.load(handle)
343-
return keypoints
344-
return None
345-
346331
def _words_file(self, image):
347332
return os.path.join(self._feature_path(), image + '.words.npz')
348333

opensfm/features.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,12 @@ def resized_image(image, config):
3535

3636

3737
def root_feature(desc, l2_normalization=False):
38-
if l2_normalization:
39-
s2 = np.linalg.norm(desc, axis=1)
40-
desc = (desc.T / s2).T
41-
s = np.sum(desc, 1)
42-
desc = np.sqrt(desc.T / s).T
43-
return desc
44-
45-
46-
def root_feature_sift_gpu(desc, l2_normalization=False):
4738
if l2_normalization:
4839
s2 = np.linalg.norm(desc, axis=1)
4940
idx = np.where(s2 == 0)
5041
s2[idx] = 1
5142
desc = (desc.T / s2).T
52-
s = np.max(desc, 1)
43+
s = np.sum(desc, 1)
5344
idx = np.where(s == 0)
5445
s[idx] = 1
5546
desc = np.sqrt(desc.T / s).T
@@ -150,7 +141,7 @@ def extract_features_sift(image, config):
150141

151142
def extract_features_sift_gpu(image, config):
152143
check_gpu_initialization(config, image)
153-
keypoints = sift_gpu.detect_image(image)
144+
keypoints = sift_gpu.detect_image(image, config)
154145
idx = np.where(np.sum(keypoints.desc, 1) != 0)
155146
keypoints = keypoints[idx]
156147

@@ -160,8 +151,8 @@ def extract_features_sift_gpu(image, config):
160151
np.expand_dims(keypoints[:].angle, axis=1)], axis=1)
161152
desc = np.array(keypoints[:].desc, dtype=np.float32)
162153
if config['feature_root']:
163-
desc = root_feature_sift_gpu(desc)
164-
return points, desc, keypoints
154+
desc = root_feature(desc)
155+
return points, desc
165156

166157

167158
def extract_features_surf(image, config):
@@ -317,17 +308,14 @@ def extract_features(color_image, config):
317308
elif feature_type == 'ORB':
318309
points, desc = extract_features_orb(image, config)
319310
elif feature_type == 'SIFT_GPU':
320-
points, desc, keypoints = extract_features_sift_gpu(image, config)
311+
points, desc = extract_features_sift_gpu(image, config)
321312
else:
322313
raise ValueError('Unknown feature type '
323314
'(must be SURF, SIFT, AKAZE, HAHOG, SIFT_GPU or ORB)')
324315

325316
xs = points[:, 0].round().astype(int)
326317
ys = points[:, 1].round().astype(int)
327318
colors = color_image[ys, xs]
328-
if keypoints is not None:
329-
return normalize_features(points, desc, colors,
330-
image.shape[1], image.shape[0]), keypoints
331319
return normalize_features(points, desc, colors,
332320
image.shape[1], image.shape[0])
333321

opensfm/matching.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,9 @@ def match(im1, im2, camera1, camera2, data):
203203
matches = match_brute_force(f1, f2, config)
204204
elif matcher_type == 'SIFT_GPU':
205205
check_gpu_initialization(config, im1, data)
206-
k1 = data.load_gpu_features(im1)
207-
k2 = data.load_gpu_features(im2)
208-
if k1 is None or k2 is None:
209-
k1 = feature_loader.instance.create_gpu_keypoints_from_features(p1, f1)
210-
k2 = feature_loader.instance.create_gpu_keypoints_from_features(p2, f2)
211-
matches = sift_gpu.match_images(k1, k2)
206+
k1 = feature_loader.instance.create_gpu_keypoints_from_features(p1, f1)
207+
k2 = feature_loader.instance.create_gpu_keypoints_from_features(p2, f2)
208+
matches = sift_gpu.match_images(k1, k2, config)
212209
else:
213210
raise ValueError("Invalid matcher_type: {}".format(matcher_type))
214211

0 commit comments

Comments
 (0)