Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions tests/ignite/contrib/metrics/test_cohen_kappa.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,12 @@ def get_test_cases():
def _test_distrib_binary_input(device):

rank = idist.get_rank()
torch.manual_seed(12)

def _test(y_pred, y, batch_size, metric_device):

metric_device = torch.device(metric_device)
ck = CohenKappa(device=metric_device)

torch.manual_seed(10 + rank)

ck.reset()
if batch_size > 1:
n_iters = y.shape[0] // batch_size + 1
Expand Down Expand Up @@ -213,7 +210,8 @@ def get_test_cases():
]
return test_cases

for _ in range(3):
for i in range(3):
torch.manual_seed(10 + rank + i)
test_cases = get_test_cases()
for y_pred, y, batch_size in test_cases:
_test(y_pred, y, batch_size, "cpu")
Expand All @@ -224,23 +222,23 @@ def get_test_cases():
def _test_distrib_integration_binary_input(device):

rank = idist.get_rank()
torch.manual_seed(12)

def _test(n_epochs, metric_device):
metric_device = torch.device(metric_device)
n_iters = 80
s = 16
batch_size = 16
n_classes = 2
offset = n_iters * s

torch.manual_seed(12 + rank)

# Binary input data of shape (N,) or (N, 1)
y_true = torch.randint(0, n_classes, size=(offset * idist.get_world_size(),)).to(device)
y_preds = torch.randint(0, n_classes, size=(offset * idist.get_world_size(),)).to(device)
y_true = torch.randint(0, n_classes, size=(n_iters * batch_size,)).to(device)
y_preds = torch.randint(0, n_classes, size=(n_iters * batch_size,)).to(device)

def update(engine, i):
return (
y_preds[i * s + rank * offset : (i + 1) * s + rank * offset],
y_true[i * s + rank * offset : (i + 1) * s + rank * offset],
y_preds[i * batch_size : (i + 1) * batch_size],
y_true[i * batch_size : (i + 1) * batch_size],
)

engine = Engine(update)
Expand All @@ -251,6 +249,9 @@ def update(engine, i):
data = list(range(n_iters))
engine.run(data=data, max_epochs=n_epochs)

y_true = idist.all_gather(y_true)
y_preds = idist.all_gather(y_preds)

assert "ck" in engine.state.metrics

res = engine.state.metrics["ck"]
Expand Down