No module named 'sklearn.utils.linear_assignment_'

Viewed 35549

I am trying to run a project from github , every object counter applications using sort algorithm. I can't run any of them because of a specific error, attaching errors screenshot. Can anyone help me about fixing this issue? enter image description here

5 Answers

The linear_assignment function is deprecated in 0.21 and will be removed from 0.23, but sklearn.utils.linear_assignment_ can be replaced by scipy.optimize.linear_sum_assignment.

You can use:

from scipy.optimize import linear_sum_assignment as linear_assignment

then you can run the file and don't need to change the code.

As yiakwy points out in a github comment the scipy.optimize.linear_sum_assignment is not the perfect replacement:

I am concerned that linear_sum_assignment is not equivalent to linear_assignment which later implements "maximum values" matching strategy not "complete matching" strategy, i.e. in tracking problem maybe an old landmark lost and a new detection coming in. We don't have to make a complete assignment, just match as more as possible.

I have found this out while trying to use it inside SORT-based yolo tracking code which that replacement broke (I was lucky that it did otherwise, I would get wrong results from the experiments without realising it...)

Instead, I suggest copying the module itself to the last version of sklearn and include as module in your code.

https://github.com/scikit-learn/scikit-learn/blob/0.22.X/sklearn/utils/linear_assignment_.py

For instance if you copy this file into an utils directory import with from utils.linear_assignment_ import linear_assignment

Solution

  • Use pip to install lap and optionally scipy
  • Uncomment the import and use the following function
def linear_assignment(cost_matrix):
  try:
    import lap
    _, x, y = lap.lapjv(cost_matrix, extend_cost=True)
    return np.array([[y[i], i] for i in x if i >= 0])
  except ImportError:
    from scipy.optimize import linear_sum_assignment
    x, y = linear_sum_assignment(cost_matrix)
    return np.array(list(zip(x, y)))
Related