Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Detection Class for TensorFlow 2.x and NumPy Compatibility #337

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deep_sort/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Detection(object):
"""

def __init__(self, tlwh, confidence, feature):
self.tlwh = np.asarray(tlwh, dtype=np.float)
self.tlwh = np.asarray(tlwh, dtype=float)
self.confidence = float(confidence)
self.feature = np.asarray(feature, dtype=np.float32)

Expand Down
9 changes: 6 additions & 3 deletions deep_sort/linear_assignment.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# vim: expandtab:ts=4:sw=4
from __future__ import absolute_import
import numpy as np
from sklearn.utils.linear_assignment_ import linear_assignment
from scipy.optimize import linear_sum_assignment
from . import kalman_filter


INFTY_COST = 1e+5


Expand Down Expand Up @@ -55,8 +55,11 @@ def min_cost_matching(
cost_matrix = distance_metric(
tracks, detections, track_indices, detection_indices)
cost_matrix[cost_matrix > max_distance] = max_distance + 1e-5
indices = linear_assignment(cost_matrix)

#use linear_sum_assignment from scipy
row_indices, col_indices = linear_sum_assignment(cost_matrix)
indices = np.array(list(zip(row_indices, col_indices)))

matches, unmatched_tracks, unmatched_detections = [], [], []
for col, detection_idx in enumerate(detection_indices):
if col not in indices[:, 1]:
Expand Down
24 changes: 18 additions & 6 deletions tools/generate_detections.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import numpy as np
import cv2
import tensorflow as tf
tf.compat.v1.disable_eager_execution() # TF 2.x compatible



def _run_in_batches(f, data_dict, out, batch_size):
Expand Down Expand Up @@ -55,7 +57,7 @@ def extract_image_patch(image, bbox, patch_shape):

# convert to top left, bottom right
bbox[2:] += bbox[:2]
bbox = bbox.astype(np.int)
bbox = bbox.astype(int)

# clip at image boundaries
bbox[:2] = np.maximum(0, bbox[:2])
Expand All @@ -70,23 +72,33 @@ def extract_image_patch(image, bbox, patch_shape):

class ImageEncoder(object):



def __init__(self, checkpoint_filename, input_name="images",
output_name="features"):
self.session = tf.Session()
with tf.gfile.GFile(checkpoint_filename, "rb") as file_handle:
graph_def = tf.GraphDef()
# Use TensorFlow 2.x compatible session
self.session = tf.compat.v1.Session()

# Load the graph definition file
with tf.io.gfile.GFile(checkpoint_filename, "rb") as file_handle:
graph_def = tf.compat.v1.GraphDef()
graph_def.ParseFromString(file_handle.read())

# Import the graph and get the input and output tensors
tf.import_graph_def(graph_def, name="net")
self.input_var = tf.get_default_graph().get_tensor_by_name(
self.input_var = tf.compat.v1.get_default_graph().get_tensor_by_name(
"net/%s:0" % input_name)
self.output_var = tf.get_default_graph().get_tensor_by_name(
self.output_var = tf.compat.v1.get_default_graph().get_tensor_by_name(
"net/%s:0" % output_name)

# Validate the tensor shapes
assert len(self.output_var.get_shape()) == 2
assert len(self.input_var.get_shape()) == 4
self.feature_dim = self.output_var.get_shape().as_list()[-1]
self.image_shape = self.input_var.get_shape().as_list()[1:]



def __call__(self, data_x, batch_size=32):
out = np.zeros((len(data_x), self.feature_dim), np.float32)
_run_in_batches(
Expand Down