@@ -87,9 +87,10 @@ def get_y_true_y_pred():
87
87
return y_true , y_pred
88
88
89
89
90
- def test_multiclass_images ():
90
+ def test_multiclass_images (available_device ):
91
91
num_classes = 3
92
- cm = MultiLabelConfusionMatrix (num_classes = num_classes )
92
+ cm = MultiLabelConfusionMatrix (num_classes = num_classes , device = available_device )
93
+ assert cm ._device == torch .device (available_device )
93
94
94
95
y_true , y_pred = get_y_true_y_pred ()
95
96
@@ -107,7 +108,8 @@ def test_multiclass_images():
107
108
assert np .all (ignite_CM == sklearn_CM )
108
109
109
110
# Another test on batch of 2 images
110
- cm = MultiLabelConfusionMatrix (num_classes = num_classes )
111
+ cm = MultiLabelConfusionMatrix (num_classes = num_classes , device = available_device )
112
+ assert cm ._device == torch .device (available_device )
111
113
112
114
# Create a batch of two images:
113
115
th_y_true1 = torch .tensor (y_true )
@@ -208,7 +210,7 @@ def _test_distrib_accumulator_device(device):
208
210
), f"{ type (cm .confusion_matrix .device )} :{ cm ._num_correct .device } vs { type (metric_device )} :{ metric_device } "
209
211
210
212
211
- def test_simple_2D_input ():
213
+ def test_simple_2D_input (available_device ):
212
214
# Tests for 2D inputs with normalized = True and False
213
215
214
216
num_iters = 5
@@ -218,19 +220,21 @@ def test_simple_2D_input():
218
220
for _ in range (num_iters ):
219
221
target = torch .randint (0 , 2 , size = (num_samples , num_classes ))
220
222
prediction = torch .randint (0 , 2 , size = (num_samples , num_classes ))
221
- sklearn_CM = multilabel_confusion_matrix (target .numpy (), prediction .numpy ())
222
- mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False )
223
+ sklearn_CM = multilabel_confusion_matrix (target .cpu ().numpy (), prediction .cpu ().numpy ())
224
+ mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False , device = available_device )
225
+ assert mlcm ._device == torch .device (available_device )
223
226
mlcm .update ([prediction , target ])
224
- ignite_CM = mlcm .compute ().numpy ()
227
+ ignite_CM = mlcm .compute ().cpu (). numpy ()
225
228
assert np .all (sklearn_CM .astype (np .int64 ) == ignite_CM .astype (np .int64 ))
226
- mlcm = MultiLabelConfusionMatrix (num_classes , normalized = True )
229
+ mlcm = MultiLabelConfusionMatrix (num_classes , normalized = True , device = available_device )
230
+ assert mlcm ._device == torch .device (available_device )
227
231
mlcm .update ([prediction , target ])
228
- ignite_CM_normalized = mlcm .compute ().numpy ()
232
+ ignite_CM_normalized = mlcm .compute ().cpu (). numpy ()
229
233
sklearn_CM_normalized = sklearn_CM / sklearn_CM .sum (axis = (1 , 2 ))[:, None , None ]
230
234
assert np .allclose (sklearn_CM_normalized , ignite_CM_normalized )
231
235
232
236
233
- def test_simple_ND_input ():
237
+ def test_simple_ND_input (available_device ):
234
238
num_iters = 5
235
239
num_samples = 100
236
240
num_classes = 10
@@ -240,82 +244,88 @@ def test_simple_ND_input():
240
244
for _ in range (num_iters ): # 3D tests
241
245
target = torch .randint (0 , 2 , size = (num_samples , num_classes , size_3d ))
242
246
prediction = torch .randint (0 , 2 , size = (num_samples , num_classes , size_3d ))
243
- mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False )
247
+ mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False , device = available_device )
248
+ assert mlcm ._device == torch .device (available_device )
244
249
mlcm .update ([prediction , target ])
245
- ignite_CM = mlcm .compute ().numpy ()
250
+ ignite_CM = mlcm .compute ().cpu (). numpy ()
246
251
target_reshaped = target .permute (0 , 2 , 1 ).reshape (size_3d * num_samples , num_classes )
247
252
prediction_reshaped = prediction .permute (0 , 2 , 1 ).reshape (size_3d * num_samples , num_classes )
248
- sklearn_CM = multilabel_confusion_matrix (target_reshaped .numpy (), prediction_reshaped .numpy ())
253
+ sklearn_CM = multilabel_confusion_matrix (target_reshaped .cpu (). numpy (), prediction_reshaped . cpu () .numpy ())
249
254
assert np .all (sklearn_CM .astype (np .int64 ) == ignite_CM .astype (np .int64 ))
250
255
251
256
size_4d = 4
252
257
for _ in range (num_iters ): # 4D tests
253
258
target = torch .randint (0 , 2 , size = (num_samples , num_classes , size_3d , size_4d ))
254
259
prediction = torch .randint (0 , 2 , size = (num_samples , num_classes , size_3d , size_4d ))
255
- mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False )
260
+ mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False , device = available_device )
261
+ assert mlcm ._device == torch .device (available_device )
256
262
mlcm .update ([prediction , target ])
257
- ignite_CM = mlcm .compute ().numpy ()
263
+ ignite_CM = mlcm .compute ().cpu (). numpy ()
258
264
target_reshaped = target .permute (0 , 2 , 3 , 1 ).reshape (size_3d * size_4d * num_samples , num_classes )
259
265
prediction_reshaped = prediction .permute (0 , 2 , 3 , 1 ).reshape (size_3d * size_4d * num_samples , num_classes )
260
- sklearn_CM = multilabel_confusion_matrix (target_reshaped .numpy (), prediction_reshaped .numpy ())
266
+ sklearn_CM = multilabel_confusion_matrix (target_reshaped .cpu (). numpy (), prediction_reshaped . cpu () .numpy ())
261
267
assert np .all (sklearn_CM .astype (np .int64 ) == ignite_CM .astype (np .int64 ))
262
268
263
269
size_5d = 4
264
270
for _ in range (num_iters ): # 5D tests
265
271
target = torch .randint (0 , 2 , size = (num_samples , num_classes , size_3d , size_4d , size_5d ))
266
272
prediction = torch .randint (0 , 2 , size = (num_samples , num_classes , size_3d , size_4d , size_5d ))
267
- mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False )
273
+ mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False , device = available_device )
274
+ assert mlcm ._device == torch .device (available_device )
268
275
mlcm .update ([prediction , target ])
269
- ignite_CM = mlcm .compute ().numpy ()
276
+ ignite_CM = mlcm .compute ().cpu (). numpy ()
270
277
target_reshaped = target .permute (0 , 2 , 3 , 4 , 1 ).reshape (size_3d * size_4d * size_5d * num_samples , num_classes )
271
278
prediction_reshaped = prediction .permute (0 , 2 , 3 , 4 , 1 ).reshape (
272
279
size_3d * size_4d * size_5d * num_samples , num_classes
273
280
)
274
- sklearn_CM = multilabel_confusion_matrix (target_reshaped .numpy (), prediction_reshaped .numpy ())
281
+ sklearn_CM = multilabel_confusion_matrix (target_reshaped .cpu (). numpy (), prediction_reshaped . cpu () .numpy ())
275
282
assert np .all (sklearn_CM .astype (np .int64 ) == ignite_CM .astype (np .int64 ))
276
283
277
284
278
- def test_simple_batched ():
285
+ def test_simple_batched (available_device ):
279
286
num_iters = 5
280
287
num_samples = 100
281
288
num_classes = 10
282
289
batch_size = 1
283
290
torch .manual_seed (0 )
284
291
285
292
for _ in range (num_iters ): # 2D tests
286
- mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False )
293
+ mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False , device = available_device )
294
+ assert mlcm ._device == torch .device (available_device )
287
295
targets = torch .randint (0 , 2 , size = (int (num_samples / batch_size ), batch_size , num_classes ))
288
296
predictions = torch .randint (0 , 2 , size = (int (num_samples / batch_size ), batch_size , num_classes ))
289
297
for i in range (int (num_samples / batch_size )):
290
298
target_sample = targets [i ]
291
299
prediction_sample = predictions [i ]
292
300
mlcm .update ([prediction_sample , target_sample ])
293
301
294
- ignite_CM = mlcm .compute ().numpy ()
302
+ ignite_CM = mlcm .compute ().cpu (). numpy ()
295
303
targets_reshaped = targets .reshape (- 1 , num_classes )
296
304
predictions_reshaped = predictions .reshape (- 1 , num_classes )
297
- sklearn_CM = multilabel_confusion_matrix (targets_reshaped .numpy (), predictions_reshaped .numpy ())
305
+ sklearn_CM = multilabel_confusion_matrix (targets_reshaped .cpu (). numpy (), predictions_reshaped . cpu () .numpy ())
298
306
assert np .all (sklearn_CM .astype (np .int64 ) == ignite_CM .astype (np .int64 ))
299
307
300
308
size_3d = 4
301
309
for _ in range (num_iters ): # 3D tests
302
- mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False )
310
+ mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False , device = available_device )
311
+ assert mlcm ._device == torch .device (available_device )
303
312
targets = torch .randint (0 , 2 , size = (int (num_samples / batch_size ), batch_size , num_classes , size_3d ))
304
313
predictions = torch .randint (0 , 2 , size = (int (num_samples / batch_size ), batch_size , num_classes , size_3d ))
305
314
for i in range (int (num_samples / batch_size )):
306
315
target_sample = targets [i ]
307
316
prediction_sample = predictions [i ]
308
317
mlcm .update ([prediction_sample , target_sample ])
309
318
310
- ignite_CM = mlcm .compute ().numpy ()
319
+ ignite_CM = mlcm .compute ().cpu (). numpy ()
311
320
targets_reshaped = targets .permute (0 , 1 , 3 , 2 ).reshape (- 1 , num_classes )
312
321
predictions_reshaped = predictions .permute (0 , 1 , 3 , 2 ).reshape (- 1 , num_classes )
313
- sklearn_CM = multilabel_confusion_matrix (targets_reshaped .numpy (), predictions_reshaped .numpy ())
322
+ sklearn_CM = multilabel_confusion_matrix (targets_reshaped .cpu (). numpy (), predictions_reshaped . cpu () .numpy ())
314
323
assert np .all (sklearn_CM .astype (np .int64 ) == ignite_CM .astype (np .int64 ))
315
324
316
325
size_4d = 4
317
326
for _ in range (num_iters ): # 4D tests
318
- mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False )
327
+ mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False , device = available_device )
328
+ assert mlcm ._device == torch .device (available_device )
319
329
targets = torch .randint (0 , 2 , size = (int (num_samples / batch_size ), batch_size , num_classes , size_3d , size_4d ))
320
330
predictions = torch .randint (
321
331
0 , 2 , size = (int (num_samples / batch_size ), batch_size , num_classes , size_3d , size_4d )
@@ -325,15 +335,16 @@ def test_simple_batched():
325
335
prediction_sample = predictions [i ]
326
336
mlcm .update ([prediction_sample , target_sample ])
327
337
328
- ignite_CM = mlcm .compute ().numpy ()
338
+ ignite_CM = mlcm .compute ().cpu (). numpy ()
329
339
targets_reshaped = targets .permute (0 , 1 , 3 , 4 , 2 ).reshape (- 1 , num_classes )
330
340
predictions_reshaped = predictions .permute (0 , 1 , 3 , 4 , 2 ).reshape (- 1 , num_classes )
331
- sklearn_CM = multilabel_confusion_matrix (targets_reshaped .numpy (), predictions_reshaped .numpy ())
341
+ sklearn_CM = multilabel_confusion_matrix (targets_reshaped .cpu (). numpy (), predictions_reshaped . cpu () .numpy ())
332
342
assert np .all (sklearn_CM .astype (np .int64 ) == ignite_CM .astype (np .int64 ))
333
343
334
344
size_5d = 4
335
345
for _ in range (num_iters ): # 5D tests
336
- mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False )
346
+ mlcm = MultiLabelConfusionMatrix (num_classes , normalized = False , device = available_device )
347
+ assert mlcm ._device == torch .device (available_device )
337
348
targets = torch .randint (
338
349
0 , 2 , size = (int (num_samples / batch_size ), batch_size , num_classes , size_3d , size_4d , size_5d )
339
350
)
@@ -345,10 +356,10 @@ def test_simple_batched():
345
356
prediction_sample = predictions [i ]
346
357
mlcm .update ([prediction_sample , target_sample ])
347
358
348
- ignite_CM = mlcm .compute ().numpy ()
359
+ ignite_CM = mlcm .compute ().cpu (). numpy ()
349
360
targets_reshaped = targets .permute (0 , 1 , 3 , 4 , 5 , 2 ).reshape (- 1 , num_classes )
350
361
predictions_reshaped = predictions .permute (0 , 1 , 3 , 4 , 5 , 2 ).reshape (- 1 , num_classes )
351
- sklearn_CM = multilabel_confusion_matrix (targets_reshaped .numpy (), predictions_reshaped .numpy ())
362
+ sklearn_CM = multilabel_confusion_matrix (targets_reshaped .cpu (). numpy (), predictions_reshaped . cpu () .numpy ())
352
363
assert np .all (sklearn_CM .astype (np .int64 ) == ignite_CM .astype (np .int64 ))
353
364
354
365
0 commit comments