Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No module named 'sklearn.utils.linear_assignment_'

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

like image 814
Furkan Eraslan Avatar asked Jun 15 '20 14:06

Furkan Eraslan


4 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.

like image 136
enthusiastdev Avatar answered Oct 04 '22 18:10

enthusiastdev


pip install scikit-learn==0.22.2

like image 22
Santle Camilus Avatar answered Oct 04 '22 19:10

Santle Camilus


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

like image 27
Mix Avatar answered Oct 04 '22 18:10

Mix


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)))
like image 45
InputBlackBoxOutput Avatar answered Oct 04 '22 20:10

InputBlackBoxOutput