From 2344383f2515a8d81b9bf8f99885826b54546958 Mon Sep 17 00:00:00 2001 From: i-ninte Date: Mon, 4 Nov 2024 14:41:47 +0000 Subject: [PATCH 1/2] fixed compatibility issues with tensorflow 2.x --- deep_sort/detection.py | 2 +- deep_sort/linear_assignment.py | 9 ++++++--- tools/generate_detections.py | 24 ++++++++++++++++++------ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/deep_sort/detection.py b/deep_sort/detection.py index 5c884bb8..d7f2c722 100644 --- a/deep_sort/detection.py +++ b/deep_sort/detection.py @@ -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) diff --git a/deep_sort/linear_assignment.py b/deep_sort/linear_assignment.py index 178456cf..6bd4aac5 100644 --- a/deep_sort/linear_assignment.py +++ b/deep_sort/linear_assignment.py @@ -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 @@ -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]: diff --git a/tools/generate_detections.py b/tools/generate_detections.py index c7192c26..92a22732 100644 --- a/tools/generate_detections.py +++ b/tools/generate_detections.py @@ -5,6 +5,8 @@ import numpy as np import cv2 import tensorflow as tf +tf.compat.v1.disable_eager_execution() # Add this at the top if needed for compatibility + def _run_in_batches(f, data_dict, out, batch_size): @@ -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]) @@ -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( From 71e775783e8ea019eb5b8c82b5a96665954f2ef3 Mon Sep 17 00:00:00 2001 From: i-ninte Date: Mon, 4 Nov 2024 14:43:56 +0000 Subject: [PATCH 2/2] fixed compatibility issues with tensorflow 2.x --- deep_sort/linear_assignment.py | 2 +- tools/generate_detections.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/deep_sort/linear_assignment.py b/deep_sort/linear_assignment.py index 6bd4aac5..7f61803b 100644 --- a/deep_sort/linear_assignment.py +++ b/deep_sort/linear_assignment.py @@ -1,7 +1,7 @@ # vim: expandtab:ts=4:sw=4 from __future__ import absolute_import import numpy as np -from scipy.optimize import linear_sum_assignment +from scipy.optimize import linear_sum_assignment from . import kalman_filter diff --git a/tools/generate_detections.py b/tools/generate_detections.py index 92a22732..afe7c229 100644 --- a/tools/generate_detections.py +++ b/tools/generate_detections.py @@ -5,7 +5,7 @@ import numpy as np import cv2 import tensorflow as tf -tf.compat.v1.disable_eager_execution() # Add this at the top if needed for compatibility +tf.compat.v1.disable_eager_execution() # TF 2.x compatible