ignite icon indicating copy to clipboard operation
ignite copied to clipboard

Remove scikit-learn dependency from `CohenKappa` metric by implementing a native PyTorch version

Open Prathamesh8989 opened this issue 8 months ago • 3 comments

The current implementation of ignite.metrics.CohenKappa relies on sklearn.metrics.cohen_kappa_score to compute the metric.

This introduces two limitations:

Mandatory CPU transfer

Inside _cohen_kappa_score, tensors are converted using:

y_true = y_targets.cpu().numpy()
y_pred = y_preds.cpu().numpy()

This forces a GPU → CPU transfer, which introduces unnecessary memory copies and prevents the metric from operating fully on GPU pipelines.


External dependency

The metric requires scikit-learn, which is not otherwise required by Ignite. This increases dependency overhead for users who may only need this metric.


Proposed Improvement

Implement the Cohen’s Kappa computation using native PyTorch operations.

The metric can be computed using the standard formulation:

κ = (pₒ − pₑ) / (1 − pₑ)

where:

  • pₒ = observed agreement

  • pₑ = expected agreement

Both quantities can be computed using a confusion matrix constructed with PyTorch tensors, allowing the entire computation to remain on the same device (CPU or GPU).


Benefits

  • Removes the scikit-learn dependency
  • Avoids GPU → CPU synchronization
  • Improves performance in GPU pipelines
  • Keeps the implementation consistent with other metrics that rely solely on PyTorch

Possible Implementation Approach

  1. Compute a confusion matrix using torch.bincount or torch.histc.
  2. Compute observed agreement from the diagonal.
  3. Compute expected agreement from marginal probabilities.
  4. Apply the Cohen’s Kappa formula.

This would make CohenKappa consistent with other metrics that are implemented purely using PyTorch operations.

If this direction is acceptable, I would be happy to work on a PR implementing this change.

Prathamesh8989 avatar Mar 27 '26 16:03 Prathamesh8989

well converting the sklearn or scipy code to pytorch native code is not our current top priority but pr is welcomed :)

aaishwarymishra avatar Mar 31 '26 06:03 aaishwarymishra

I would like to take up on this task if there are no ongoing efforts on it being taken

avishkarsonni avatar Apr 12 '26 01:04 avishkarsonni

@avishkarsonni go for it 👍

aaishwarymishra avatar Apr 12 '26 05:04 aaishwarymishra