Example #1
0
def test_accuracy():
    output = torch.rand((16, 4))
    output_np = output.numpy()
    target = torch.randint(0, 4, (16, ))
    target_np = target.numpy()
    expected = 100 * accuracy_score(target_np, np.argmax(output_np, 1))
    result = Accuracy()(output, target).item()
    assert np.allclose(expected, result)
    # check that it also works in case of extra dim for target
    result2 = Accuracy()(output, target.view(16, 1)).item()
    assert np.allclose(expected, result2)
Example #2
0
def test_accuracy_image():
    output = torch.rand((16, 4, 20, 20))
    target = torch.randint(0, 2, (16, 1, 20, 20))
    expected = 100 * accuracy_score(target.flatten().numpy(),
                                    output.argmax(1).flatten())
    result = Accuracy()(output, target).item()
    assert np.allclose(expected, result)
Example #3
0
def test_accuracy():
    output = torch.rand((16, 4))
    output_np = output.numpy()
    target = torch.randint(0, 4, (16, ))
    target_np = target.numpy()
    expected = 100 * accuracy_score(target_np, np.argmax(output_np, 1))
    result = Accuracy()(output, target).flatten().numpy()
    assert np.allclose(expected, result)
Example #4
0
def test_binary_accuracy():
    output = torch.rand((16, 1))
    output_np = output.numpy()
    target = torch.randint(0, 2, (16, ))
    target_np = target.numpy()
    expected = 100 * accuracy_score(target_np, output_np > 0)
    result = Accuracy()(output, target).item()
    assert np.allclose(expected, result)
Example #5
0
def test_binary_accuracy_image():
    output = torch.rand((16, 1, 20, 20)) - 0.5
    output_np = output.numpy()
    target = torch.randint(0, 2, (16, 1, 20, 20))
    target_np = target.numpy()
    expected = 100 * accuracy_score(target_np.flatten(),
                                    output_np.flatten() > 0)
    result = Accuracy()(output, target).item()
    assert np.allclose(expected, result)
Example #6
0
def test_accuracy_topk():
    # fmt: off
    output = torch.tensor([
        [
            10,
            5,
            -10,
        ],
        [
            10,
            -10,
            5,
        ],
        [
            5,
            10,
            -10,
        ],
        [
            -10,
            5,
            10,
        ],
        [
            -10,
            5,
            10,
        ],
    ])
    # fmt: on
    target = torch.tensor([1, 1, 1, 1, 1])
    result = Accuracy(2)(output, target).item()
    assert np.allclose(result, 4 / 5 * 100)
    target2 = torch.tensor([2, 2, 2, 2, 2])
    result2 = Accuracy(2)(output, target2).item()
    assert np.allclose(result2, 3 / 5 * 100)
Example #7
0
        return self

    def __len__(self):
        return LOADER_LEN

    def __next__(self):
        img = torch.randn(BS, 3, IMG_SHAPE, IMG_SHAPE)
        target = torch.randint(NUM_CLASSES, (BS, ))
        return img.cuda(), target.cuda()


TestLoader = Loader()
TestModel = Model().cuda()
TestOptimizer = torch.optim.SGD(TestModel.parameters(), lr=1e-3)
TestCriterion = CrossEntropyLoss().cuda()
TestMetric = Accuracy()

TestModel, TestOptimizer = apex.amp.initialize(TestModel,
                                               TestOptimizer,
                                               verbosity=0)


def test_default():
    runner = Runner(model=TestModel,
                    optimizer=TestOptimizer,
                    criterion=TestCriterion,
                    metrics=TestMetric,
                    callbacks=None)
    runner.fit(TestLoader, epochs=2)

Example #8
0
class SegmLoader(Loader):
    def __next__(self):
        img = torch.randn(BS, 3, IMG_SHAPE, IMG_SHAPE)
        target = torch.randint(2, (BS, NUM_CLASSES, IMG_SHAPE, IMG_SHAPE))
        return img.cuda(), target.cuda()


TEST_LOADER = Loader()
TEST_SEGM_LOADER = SegmLoader()
TEST_MODEL = Model().cuda()
TEST_SEGM_MODEL = SegmModel().cuda()
TEST_OPTIMIZER = torch.optim.SGD(TEST_MODEL.parameters(), lr=1e-3)
TEST_SEGM_OPTIMZER = torch.optim.SGD(TEST_SEGM_MODEL.parameters(), lr=1e-3)
TEST_CRITERION = CrossEntropyLoss().cuda()
TEST_METRIC = Accuracy()

TEST_MODEL, TEST_OPTIMIZER = apex.amp.initialize(TEST_MODEL, TEST_OPTIMIZER, verbosity=0)
TEST_SEGM_MODEL, TEST_SEGM_OPTIMZER = apex.amp.initialize(TEST_SEGM_MODEL, TEST_SEGM_OPTIMZER)


def test_default():
    runner = Runner(
        model=TEST_MODEL,
        optimizer=TEST_OPTIMIZER,
        criterion=TEST_CRITERION,
        metrics=TEST_METRIC,
        callbacks=None,
    )
    runner.fit(TEST_LOADER, epochs=2)