Skip to content

Commit

Permalink
Small polishing changes to DELF code.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 185469605
  • Loading branch information
andrefaraujo committed Feb 13, 2018
1 parent 0d7ce10 commit 9f3d5bc
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 57 deletions.
3 changes: 2 additions & 1 deletion research/delf/delf/python/datum_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
from __future__ import division
from __future__ import print_function

from delf import datum_pb2
import numpy as np
import tensorflow as tf

from delf import datum_pb2


def ArrayToDatum(arr):
"""Converts numpy array to DatumProto.
Expand Down
8 changes: 4 additions & 4 deletions research/delf/delf/python/datum_io_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Tests for datum_io, the python interface of DatumProto."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from delf import datum_io
from delf import datum_pb2
import numpy as np
import os

import numpy as np
import tensorflow as tf

from delf import datum_io


class DatumIoTest(tf.test.TestCase):

Expand Down
7 changes: 3 additions & 4 deletions research/delf/delf/python/delf_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""DELF model implementation based on the following paper:
"""DELF model implementation based on the following paper.
Large-Scale Image Retrieval with Attentive Deep Local Features
https://arxiv.org/abs/1612.06321
Expand All @@ -26,10 +25,10 @@
from __future__ import division
from __future__ import print_function

from nets import resnet_v1

import tensorflow as tf

from nets import resnet_v1

slim = tf.contrib.slim

_SUPPORTED_TARGET_LAYER = ['resnet_v1_50/block3', 'resnet_v1_50/block4']
Expand Down
16 changes: 7 additions & 9 deletions research/delf/delf/python/examples/extract_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Extracts DELF features from a list of images, saving them to file.
The images must be in JPG format. The program checks if descriptors already
Expand All @@ -24,18 +23,17 @@
from __future__ import print_function

import argparse
from google.protobuf import text_format
import numpy as np
import os
import sys
import tensorflow as tf
from tensorflow.python.platform import app
import time

import tensorflow as tf

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 import feature_pb2

cmd_args = None

Expand Down Expand Up @@ -152,9 +150,9 @@ def main(unused_argv):
config.delf_local_config.max_feature_num
})

serialized_desc = feature_io.WriteToFile(
out_desc_fullpath, locations_out, feature_scales_out,
descriptors_out, attention_out)
feature_io.WriteToFile(out_desc_fullpath, locations_out,
feature_scales_out, descriptors_out,
attention_out)

# Finalize enqueue threads.
coord.request_stop()
Expand Down
19 changes: 11 additions & 8 deletions research/delf/delf/python/examples/match_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Matches two images using their DELF features.
The matching is done using feature-based nearest-neighbor search, followed by
Expand All @@ -26,17 +25,19 @@
from __future__ import print_function

import argparse
from delf import feature_io
import sys

import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np
from scipy.spatial import cKDTree
from skimage.feature import plot_matches
from skimage.measure import ransac
from skimage.transform import AffineTransform
import sys
import tensorflow as tf

from tensorflow.python.platform import app
from delf import feature_io

cmd_args = None

Expand All @@ -58,21 +59,23 @@ def main(unused_argv):

# Find nearest-neighbor matches using a KD tree.
d1_tree = cKDTree(descriptors_1)
distances, indices = d1_tree.query(
_, indices = d1_tree.query(
descriptors_2, distance_upper_bound=_DISTANCE_THRESHOLD)

# Select feature locations for putative matches.
locations_2_to_use = np.array([
locations_2[i,] for i in range(num_features_2)
locations_2[i,]
for i in range(num_features_2)
if indices[i] != num_features_1
])
locations_1_to_use = np.array([
locations_1[indices[i],] for i in range(num_features_2)
locations_1[indices[i],]
for i in range(num_features_2)
if indices[i] != num_features_1
])

# Perform geometric verification using RANSAC.
model_robust, inliers = ransac(
_, inliers = ransac(
(locations_1_to_use, locations_2_to_use),
AffineTransform,
min_samples=3,
Expand All @@ -82,7 +85,7 @@ def main(unused_argv):
tf.logging.info('Found %d inliers' % sum(inliers))

# Visualize correspondences, and save to file.
fig, ax = plt.subplots()
_, ax = plt.subplots()
img_1 = mpimg.imread(cmd_args.image_1_path)
img_2 = mpimg.imread(cmd_args.image_2_path)
inlier_idxs = np.nonzero(inliers)[0]
Expand Down
11 changes: 6 additions & 5 deletions research/delf/delf/python/feature_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
from __future__ import division
from __future__ import print_function

import tensorflow as tf

from delf import datum_io
from delf import delf_v1
from delf import delf_config_pb2
from object_detection.core import box_list
from object_detection.core import box_list_ops
import tensorflow as tf


def NormalizePixelValues(image,
Expand Down Expand Up @@ -170,7 +170,8 @@ def _ProcessSingleScale(scale_index,
resized_image, normalized_image=True, reuse=reuse)

rf_boxes = CalculateReceptiveBoxes(
tf.shape(feature_map)[1], tf.shape(feature_map)[2], rf, stride, padding)
tf.shape(feature_map)[1],
tf.shape(feature_map)[2], rf, stride, padding)
# Re-project back to the original image space.
rf_boxes = tf.divide(rf_boxes, scale)
attention = tf.reshape(attention, [-1])
Expand Down Expand Up @@ -236,8 +237,8 @@ def _ProcessSingleScale(scale_index,
nms_max_boxes)

return (final_boxes.get(), final_boxes.get_field('scales'),
final_boxes.get_field('features'), tf.expand_dims(
final_boxes.get_field('scores'), 1))
final_boxes.get_field('features'),
tf.expand_dims(final_boxes.get_field('scores'), 1))


def BuildModel(layer_name, attention_nonlinear, attention_type,
Expand Down
21 changes: 11 additions & 10 deletions research/delf/delf/python/feature_extractor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Tests for DELF feature extractor."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from delf import feature_extractor
import numpy as np
import tensorflow as tf

from delf import feature_extractor


class FeatureExtractorTest(tf.test.TestCase):

Expand Down Expand Up @@ -80,14 +80,15 @@ def _test_model_fn(image, normalized_image, reuse):
axis=3)
return attention, feature_map

boxes, feature_scales, features, scores = feature_extractor.ExtractKeypointDescriptor(
image,
layer_name='resnet_v1_50/block3',
image_scales=tf.constant([1.0]),
iou=1.0,
max_feature_num=10,
abs_thres=1.5,
model_fn=_test_model_fn)
boxes, feature_scales, features, scores = (
feature_extractor.ExtractKeypointDescriptor(
image,
layer_name='resnet_v1_50/block3',
image_scales=tf.constant([1.0]),
iou=1.0,
max_feature_num=10,
abs_thres=1.5,
model_fn=_test_model_fn))

exp_boxes = [[-145.0, -145.0, 145.0, 145.0], [-113.0, -145.0, 177.0, 145.0]]
exp_feature_scales = [1.0, 1.0]
Expand Down
9 changes: 5 additions & 4 deletions research/delf/delf/python/feature_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
from __future__ import division
from __future__ import print_function

from delf import feature_pb2
from delf import datum_io
import numpy as np
import tensorflow as tf

from delf import feature_pb2
from delf import datum_io


def ArraysToDelfFeatures(locations,
scales,
Expand Down Expand Up @@ -57,7 +58,7 @@ def ArraysToDelfFeatures(locations,
assert num_features == len(orientations)

delf_features = feature_pb2.DelfFeatures()
for i in xrange(num_features):
for i in range(num_features):
delf_feature = delf_features.feature.add()
delf_feature.y = locations[i, 0]
delf_feature.x = locations[i, 1]
Expand Down Expand Up @@ -98,7 +99,7 @@ def DelfFeaturesToArrays(delf_features):
attention = np.zeros([num_features])
orientations = np.zeros([num_features])

for i in xrange(num_features):
for i in range(num_features):
delf_feature = delf_features.feature[i]
locations[i, 0] = delf_feature.y
locations[i, 1] = delf_feature.x
Expand Down
25 changes: 13 additions & 12 deletions research/delf/delf/python/feature_io_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,31 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Tests for feature_io, the python interface of DelfFeatures."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from delf import feature_io
from delf import datum_pb2
import numpy as np
import os

import numpy as np
import tensorflow as tf

from delf import feature_io


def create_data():
"""Creates data to be used in tests.
Returns:
locations: [N, 2] float array which denotes the selected keypoint
locations. N is the number of features.
scales: [N] float array with feature scales.
descriptors: [N, depth] float array with DELF descriptors.
attention: [N] float array with attention scores.
orientations: [N] float array with orientations.
"""
Returns:
locations: [N, 2] float array which denotes the selected keypoint
locations. N is the number of features.
scales: [N] float array with feature scales.
descriptors: [N, depth] float array with DELF descriptors.
attention: [N] float array with attention scores.
orientations: [N] float array with orientations.
"""
locations = np.arange(8, dtype=np.float32).reshape(4, 2)
scales = np.arange(4, dtype=np.float32)
attention = np.arange(4, dtype=np.float32)
Expand Down

0 comments on commit 9f3d5bc

Please sign in to comment.