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

TypeError: tuple indices must be integers or slices, not tuple #92

Open
yifanzhang666777 opened this issue Feb 3, 2021 · 5 comments
Open

Comments

@yifanzhang666777
Copy link

if(d not in matched_indices[:,0]):
TypeError: tuple indices must be integers or slices, not tuple

@eplatero97
Copy link

This happened to me when I changed:

  • from sklearn.utils.linear_assignment_ import linear_assignment to
  • from scipy.optimize import linear_sum_assignment as linear_assignment

Because sklearn's linear_assignment has been removed starting with version "0.23.0" and higher, I opted for Scipy's implementation of the function (which returns a tuple of np vectors as opposed to a matrix) (click here to learn more).

Thus, to get the equivalent result as if we were using sklearn's implementation I modified sort.py with below code:

import sklearn
skver = int(sklearn.__version__[2:4]) # extract middle number of version with format \d.\d\d.\d\d
if skver >= 23:
    from scipy.optimize import linear_sum_assignment as linear_assignment
    sk = False # we will not use sklearn implementation
else:
    from sklearn.utils.linear_assignment_ import linear_assignment
    sk = True # we will use sklearn implementation


# Below code was written after the `matched_indices` object had been created under the `associate_detections_to_trackers` function


  matched_indices = linear_assignment(-iou_matrix) 

  # standardize output to match sklearn implementation if using scipy
  if sk == False:
      row, col = matched_indices
      matched_indices = np.concatenate((row.reshape(-1,1), col.reshape(-1,1)), axis = 1)

Below code highlights the differences between the two implementations:

# sklearn implementation
from sklearn.utils.linear_assignment_ import linear_assignment
import numpy as np

cost = np.array([[4, 1, 3], [2, 0, 5], [3, 2, 2]])
result = linear_assignment(cost)
result # array([[0, 1],
       # [1, 0],
       # [2, 2]])

# scipy implementation
import numpy as np
from scipy.optimize import linear_sum_assignment as linear_assignment

cost = np.array([[4, 1, 3], [2, 0, 5], [3, 2, 2]])
result = linear_assignment(cost)
result # (array([0, 1, 2]), array([1, 0, 2], dtype=int64))

row, col = result
result = np.concatenate((row.reshape(-1,1), col.reshape(-1,1)), axis = 1)
result # array([[0, 1],
       # [1, 0],
       # [2, 2]], dtype=int64)

@rdzfv
Copy link

rdzfv commented Apr 19, 2021

This happened to me when I changed:

  • from sklearn.utils.linear_assignment_ import linear_assignment to
  • from scipy.optimize import linear_sum_assignment as linear_assignment

Because sklearn's linear_assignment has been removed starting with version "0.23.0" and higher, I opted for Scipy's implementation of the function (which returns a tuple of np vectors as opposed to a matrix) (click here to learn more).

Thus, to get the equivalent result as if we were using sklearn's implementation I modified sort.py with below code:

import sklearn
skver = int(sklearn.__version__[2:4]) # extract middle number of version with format \d.\d\d.\d\d
if skver >= 23:
    from scipy.optimize import linear_sum_assignment as linear_assignment
    sk = False # we will not use sklearn implementation
else:
    from sklearn.utils.linear_assignment_ import linear_assignment
    sk = True # we will use sklearn implementation


# Below code was written after the `matched_indices` object had been created under the `associate_detections_to_trackers` function


  matched_indices = linear_assignment(-iou_matrix) 

  # standardize output to match sklearn implementation if using scipy
  if sk == False:
      row, col = matched_indices
      matched_indices = np.concatenate((row.reshape(-1,1), col.reshape(-1,1)), axis = 1)

Below code highlights the differences between the two implementations:

# sklearn implementation
from sklearn.utils.linear_assignment_ import linear_assignment
import numpy as np

cost = np.array([[4, 1, 3], [2, 0, 5], [3, 2, 2]])
result = linear_assignment(cost)
result # array([[0, 1],
       # [1, 0],
       # [2, 2]])

# scipy implementation
import numpy as np
from scipy.optimize import linear_sum_assignment as linear_assignment

cost = np.array([[4, 1, 3], [2, 0, 5], [3, 2, 2]])
result = linear_assignment(cost)
result # (array([0, 1, 2]), array([1, 0, 2], dtype=int64))

row, col = result
result = np.concatenate((row.reshape(-1,1), col.reshape(-1,1)), axis = 1)
result # array([[0, 1],
       # [1, 0],
       # [2, 2]], dtype=int64)

Sorry,I met the same problem. But I'm not quite understand where's sort.py and how to modify it?

@rdzfv
Copy link

rdzfv commented Apr 19, 2021

This happened to me when I changed:

  • from sklearn.utils.linear_assignment_ import linear_assignment to
  • from scipy.optimize import linear_sum_assignment as linear_assignment

Because sklearn's linear_assignment has been removed starting with version "0.23.0" and higher, I opted for Scipy's implementation of the function (which returns a tuple of np vectors as opposed to a matrix) (click here to learn more).

Thus, to get the equivalent result as if we were using sklearn's implementation I modified sort.py with below code:

import sklearn
skver = int(sklearn.__version__[2:4]) # extract middle number of version with format \d.\d\d.\d\d
if skver >= 23:
    from scipy.optimize import linear_sum_assignment as linear_assignment
    sk = False # we will not use sklearn implementation
else:
    from sklearn.utils.linear_assignment_ import linear_assignment
    sk = True # we will use sklearn implementation


# Below code was written after the `matched_indices` object had been created under the `associate_detections_to_trackers` function


  matched_indices = linear_assignment(-iou_matrix) 

  # standardize output to match sklearn implementation if using scipy
  if sk == False:
      row, col = matched_indices
      matched_indices = np.concatenate((row.reshape(-1,1), col.reshape(-1,1)), axis = 1)

Below code highlights the differences between the two implementations:

# sklearn implementation
from sklearn.utils.linear_assignment_ import linear_assignment
import numpy as np

cost = np.array([[4, 1, 3], [2, 0, 5], [3, 2, 2]])
result = linear_assignment(cost)
result # array([[0, 1],
       # [1, 0],
       # [2, 2]])

# scipy implementation
import numpy as np
from scipy.optimize import linear_sum_assignment as linear_assignment

cost = np.array([[4, 1, 3], [2, 0, 5], [3, 2, 2]])
result = linear_assignment(cost)
result # (array([0, 1, 2]), array([1, 0, 2], dtype=int64))

row, col = result
result = np.concatenate((row.reshape(-1,1), col.reshape(-1,1)), axis = 1)
result # array([[0, 1],
       # [1, 0],
       # [2, 2]], dtype=int64)

I got to know that we were talking about the different project, so i opened a new issue #98 (comment).

could you please give me a hand?

@SabraHashemi
Copy link

row, col = result

this worked for me,thank you

@Dipankar1997161
Copy link

If you encountered a problem in the linear_assignment.py
try adding that extra second line.

Mine is for a different project, but the error was same for me.

indices = linear_assignment(cost_matrix)
indices = np.hstack([indices[0].reshape(((indices[0].shape[0]), 1)),indices[1].reshape(((indices[0].shape[0]), 1))])

hope it helps :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants