def _match(self, similarity_matrix, num_valid_rows=-1): """Bipartite matches a collection rows and columns. A greedy bi-partite. TODO(rathodv): Add num_valid_columns options to match only that many columns with all the rows. Args: similarity_matrix: Float tensor of shape [N, M] with pairwise similarity where higher values mean more similar. num_valid_rows: A scalar or a 1-D tensor with one element describing the number of valid rows of similarity_matrix to consider for the bipartite matching. If set to be negative, then all rows from similarity_matrix are used. Returns: match_results: int32 tensor of shape [M] with match_results[i]=-1 meaning that column i is not matched and otherwise that it is matched to row match_results[i]. """ # Convert similarity matrix to distance matrix as tf.image.bipartite tries # to find minimum distance matches. distance_matrix = -1 * similarity_matrix _, match_results = image_ops.bipartite_match( distance_matrix, num_valid_rows) match_results = tf.reshape(match_results, [-1]) match_results = tf.cast(match_results, tf.int32) return match_results
def _match(self, similarity_matrix, valid_rows): """Bipartite matches a collection rows and columns. A greedy bi-partite. TODO(rathodv): Add num_valid_columns options to match only that many columns with all the rows. Args: similarity_matrix: Float tensor of shape [N, M] with pairwise similarity where higher values mean more similar. valid_rows: A boolean tensor of shape [N] indicating the rows that are valid. Returns: match_results: int32 tensor of shape [M] with match_results[i]=-1 meaning that column i is not matched and otherwise that it is matched to row match_results[i]. """ valid_row_sim_matrix = tf.gather( similarity_matrix, tf.squeeze(tf.where(valid_rows), axis=-1)) invalid_row_sim_matrix = tf.gather( similarity_matrix, tf.squeeze(tf.where(tf.logical_not(valid_rows)), axis=-1)) similarity_matrix = tf.concat( [valid_row_sim_matrix, invalid_row_sim_matrix], axis=0) # Convert similarity matrix to distance matrix as tf.image.bipartite tries # to find minimum distance matches. distance_matrix = -1 * similarity_matrix num_valid_rows = tf.reduce_sum(tf.to_float(valid_rows)) _, match_results = image_ops.bipartite_match( distance_matrix, num_valid_rows=num_valid_rows) match_results = tf.reshape(match_results, [-1]) match_results = tf.cast(match_results, tf.int32) return match_results
def matching_IoU(Y, Y_hat, epsilon=1e-6): norm = norm1_tf(Y) """ j'ai essayer de supprimer les eventuelles categorie avec que des zeros, mais en vain. (ça serait 2 lignes simple de numpy !!!) """ # indexPos=tf.reshape(tf.where(norm>0),shape=[-1]) # Y=tf.nn.embedding_lookup(tf.transpose(Y,perm=[2,0,1]),indexPos) # Y=tf.transpose(Y,perm=[1,2,0]) # norm=tf.nn.embedding_lookup(norm,indexPos) n = Y.get_shape().as_list()[2] norm_hat = norm1_tf(Y_hat) Y_ext = tf.expand_dims(Y, 3) Y_hat_ext = tf.expand_dims(Y_hat, 2) """ sca[k,l] = sca( Y[:,:,k] Y_hat[:,:,l]) """ sca = norm1_tf(Y_ext * Y_hat_ext) norm = tf.expand_dims(norm, 1) norm_hat = tf.expand_dims(norm_hat, 0) F_IoU = sca / (epsilon + norm + norm_hat) """ Attention, bipartite_match cherche à minimiser """ r2c, c2r = image_ops.bipartite_match(-F_IoU, n) A_perm = tf.gather(F_IoU, c2r) return tf.reduce_sum(tf.diag_part(A_perm))
def _match(self, similarity_matrix, valid_rows): """Bipartite matches a collection rows and columns. A greedy bi-partite. TODO(rathodv): Add num_valid_columns options to match only that many columns with all the rows. Args: similarity_matrix: Float tensor of shape [N, M] with pairwise similarity where higher values mean more similar. valid_rows: A boolean tensor of shape [N] indicating the rows that are valid. Returns: match_results: int32 tensor of shape [M] with match_results[i]=-1 meaning that column i is not matched and otherwise that it is matched to row match_results[i]. """ valid_row_sim_matrix = tf.gather(similarity_matrix, tf.squeeze(tf.where(valid_rows), axis=-1)) invalid_row_sim_matrix = tf.gather( similarity_matrix, tf.squeeze(tf.where(tf.logical_not(valid_rows)), axis=-1)) similarity_matrix = tf.concat( [valid_row_sim_matrix, invalid_row_sim_matrix], axis=0) # Convert similarity matrix to distance matrix as tf.image.bipartite tries # to find minimum distance matches. distance_matrix = -1 * similarity_matrix num_valid_rows = tf.reduce_sum(tf.to_float(valid_rows)) _, match_results = image_ops.bipartite_match( distance_matrix, num_valid_rows=num_valid_rows) match_results = tf.reshape(match_results, [-1]) match_results = tf.cast(match_results, tf.int32) return match_results
def test_bipartite_matching(): tf.InteractiveSession() A = tf.constant([[-1., -10, 0], [0, 0, -2], [-3, 0, 0]]) r2c, c2r = image_ops.bipartite_match(A, 3) A_perm = tf.gather(A, c2r) min_elem = tf.diag_part(A_perm) print("A\n", A.eval()) print("c2r", c2r.eval()) print("r2c", r2c.eval()) print("A_perm\n", A_perm.eval()) print("min_elem\n", min_elem.eval())
def _BipartiteMatchTest(self, distance_mat, distance_mat_shape, num_valid_rows, expected_row_to_col_match, expected_col_to_row_match): distance_mat_np = np.array(distance_mat, dtype=np.float32).reshape( distance_mat_shape) expected_row_to_col_match_np = np.array(expected_row_to_col_match, dtype=np.int32) expected_col_to_row_match_np = np.array(expected_col_to_row_match, dtype=np.int32) with self.cached_session(): distance_mat_tf = constant_op.constant(distance_mat_np, shape=distance_mat_shape) location_to_prior, prior_to_location = image_ops.bipartite_match( distance_mat_tf, num_valid_rows) location_to_prior_np = location_to_prior.eval() prior_to_location_np = prior_to_location.eval() self.assertAllEqual(location_to_prior_np, expected_row_to_col_match_np) self.assertAllEqual(prior_to_location_np, expected_col_to_row_match_np)
def _BipartiteMatchTest(self, distance_mat, distance_mat_shape, num_valid_rows, expected_row_to_col_match, expected_col_to_row_match): distance_mat_np = np.array(distance_mat, dtype=np.float32).reshape( distance_mat_shape) expected_row_to_col_match_np = np.array(expected_row_to_col_match, dtype=np.int32) expected_col_to_row_match_np = np.array(expected_col_to_row_match, dtype=np.int32) with self.test_session(): distance_mat_tf = constant_op.constant(distance_mat_np, shape=distance_mat_shape) location_to_prior, prior_to_location = image_ops.bipartite_match( distance_mat_tf, num_valid_rows) location_to_prior_np = location_to_prior.eval() prior_to_location_np = prior_to_location.eval() self.assertAllEqual(location_to_prior_np, expected_row_to_col_match_np) self.assertAllEqual(prior_to_location_np, expected_col_to_row_match_np)
tf.stack([ tf.where( tf.reduce_max(left_gt_to_anchors_mask, axis=0) > 0, tf.argmax(left_gt_to_anchors_mask, axis=0), anchors_to_gt), tf.range(tf.cast(tf.shape(overlap_matrix)[1], tf.int64)) ], axis=1)) return tf.where( tf.reduce_max(left_gt_to_anchors_mask, axis=0) > 0, tf.argmax(left_gt_to_anchors_mask, axis=0), match_indices) print(sess.run([tf.transpose(areas(gt_bboxes), perm=[1, 0])])) print(sess.run([tf.maximum(tf.expand_dims(t1, 0), tf.expand_dims(t2, 1))])) print(sess.run([do_dual_max_match(iou_matrix(gt_bboxes, bboxes), 0.65, 0.6)])) print(sess.run([image_ops.bipartite_match(iou_matrix(gt_bboxes, bboxes), -1)])) gt_bboxes1 = tf.constant([[0.12, 0.22, 0.33, 0.44], [0.14, 0.2, 0.35, 0.4], [0.1, 0.32, 0.3, 0.54]]) gt_bboxes2 = tf.constant([[2, 1, 0, 3.]]) print(sess.run(gt_bboxes1 * tf.expand_dims(gt_bboxes2, 0))) # elems = np.array([[1,2], [2,3], [3,4]]) # alternates = tf.map_fn(lambda x: [x[0][0],x[1][1]], [elems,elems]) # print(sess.run(alternates)) # scores = tf.constant([11,12,13,14,15]) # labels = tf.constant([[111,12,13,14,15], [211,12,13,14,15], [311,12,13,14,15], [411,12,13,14,15],[511,12,13,14,15]]) # num_bboxes = tf.shape(scores)[0] # left_count = 8 - num_bboxes