Пример #1
0
def test_train_iteration():
    """ Tests return types and sizes of train_iteration """
    model = test_model()
    image_dir = 'tests/test_data/images_10x'
    data = prep.prep_data(pd.read_csv('tests/test_data/10x_labels_4.csv'),
                          image_dir)
    image_dir = 'tests/test_data/images_10x'
    opt = torch.optim.Adam(model.parameters(), lr=.01)
    crit = torch.nn.CrossEntropyLoss()
    device = torch.device('cpu')
    transforms = torchvision.transforms.Compose([
        torchvision.transforms.ToPILImage(),
        torchvision.transforms.RandomRotation((-180, 180)),
        torchvision.transforms.CenterCrop((325)),
        torchvision.transforms.ToTensor()
    ])

    dataset = prep.tenX_dataset(data, image_dir, transforms)
    iterator = torch.utils.data.DataLoader(dataset, shuffle=True, batch_size=1)

    loss, acc, pred, label = construct.train_iteration(model, iterator, opt,
                                                       crit, device)
    assert isinstance(loss, float), f'train_iteration error,\
        wrong loss dtype {loss.type()}'
    assert isinstance(acc, float),\
        f'train_iteration error, wrong acc dtype {acc.type()}'
    assert pred.size() == torch.Size([1, 2]),\
        f'train_iteration error, prediction wrong shape/size {pred.size()}'
    assert label.size() == torch.Size([1, 2]),\
        f'train_iteration error, label wrong shape/size {label.size()}'
Пример #2
0
def test_get_predictions():
    """
    Tests return types and sizes of get_predictions
    """
    model = test_model()
    image_dir = 'tests/test_data/images_10x'
    data = prep.prep_data(pd.read_csv('tests/test_data/10x_labels_4.csv'),
                          image_dir)
    transforms = torchvision.transforms.Compose([
        torchvision.transforms.ToPILImage(),
        torchvision.transforms.RandomRotation((-180, 180)),
        torchvision.transforms.CenterCrop((325)),
        torchvision.transforms.ToTensor()
    ])
    dataset = prep.tenX_dataset(data, image_dir, transforms)
    images, labels, predictions, weights, acc =\
        construct.get_predictions(1, model, dataset)
    assert labels.size() == torch.Size([12, 2]),\
        f'{"get_predictions error, incorrect labels dimensions"}'
    assert isinstance(acc.item(), float),\
        f'{"get_predictions error, incorrect accuracy dtype"}'
    assert weights.size() == torch.Size([12, 2]),\
        f'{"get_predictions error, incorrect weight dimensions"}'
    assert predictions.size() == torch.Size([12]),\
        f'{"get_predictions error, incorrect predictions dimensions"}'
    assert images.size() == torch.Size([12, 3, 325, 325]),\
        f'{"get_predictions error, incorrect image"}'
def test_tenX_dataset():
    """Test if the class tenX_dataset can generate correct output."""
    # Load the inputs for tenX_dataset.
    image_dir = 'tests/test_data/images_10x'
    labels = prep.prep_data(pd.read_csv('tests/test_data/10x_labels_5.csv'),
                            image_dir)

    # To make the test more simple, define the "transform"
    # as a function that returns
    # the input directly without doing anything.

    def transforms(image):
        return image

    # Create an object tenX.
    tenX = prep.tenX_dataset(labels, image_dir, transforms)
    # check if the class tenX_dataset can generate the correct
    # output by comparing the actual output with the expected output.
    assert len(tenX) == 10, "The len method in class tenX_dataset is broken!"
    assert tenX[1]['image'].size == 1082880,\
        "The getitem method in class tenX_dataset is broken!"
    assert np.array_equal(tenX[1]['shape'], np.array([0, 0, 0, 1])),\
        "The getitem method in class tenX_dataset is broken!"
    assert np.array_equal(tenX[1]['color'], np.array([0, 0, 1, 0])),\
        "The getitem method in class tenX_dataset is broken!"
    assert tenX[1]['plastic'][0] == 1,\
        "The getitem method in class tenX_dataset is broken!"
    return None
def test_prep_data_1():
    """
    Test if the prep_data function can generate correct output.
    """
    # Load the csv file as the input data frame.
    input_df = pd.read_csv('tests/test_data/10x_labels_5.csv')
    image_dir = 'tests/test_data/images_10x'
    # Call the prep_data function and get the actual output "result".
    result = prep.prep_data(input_df, image_dir)
    # Load the output data frame from a csv file.
    output_df = pd.read_csv('tests/test_data/prep_data_output.csv')
    # Modify the format of the output data
    # frame to make it the expected output.
    for i, rowi in output_df['Sample'].iteritems():
        output_df['Sample'].loc[i] = rowi.split(',')
    for j, rowj in output_df['Color'].iteritems():
        output_df['Color'].loc[j] = np.fromstring(rowj, dtype=int, sep=' ')
    for k, rowk in output_df['Shape'].iteritems():
        output_df['Shape'].loc[k] = np.fromstring(rowk, dtype=int, sep=' ')
    output_df['isPlastic'] = [
        np.array([1, 0]),
        np.array([1, 0]),
        np.array([0, 1]),
        np.array([1, 0]),
        np.array([0, 1]),
        np.array([1, 0]),
        np.array([1, 0]),
        np.array([1, 0]),
        np.array([0, 1]),
        np.array([0, 1])
    ]
    output_df['Color'] = [
        np.array([0, 0, 1, 0]),
        np.array([0, 0, 1, 0]),
        np.array([0, 0, 0, 1]),
        np.array([1, 0, 0, 0]),
        np.array([0, 1, 0, 0]),
        np.array([0, 1, 0, 0]),
        np.array([0, 0, 1, 0]),
        np.array([0, 0, 1, 0]),
        np.array([0, 1, 0, 0]),
        np.array([0, 1, 0, 0])
    ]
    output_df['Shape'] = [
        np.array([1, 0, 0, 0]),
        np.array([0, 0, 0, 1]),
        np.array([0, 0, 0, 1]),
        np.array([0, 1, 0, 0]),
        np.array([0, 1, 0, 0]),
        np.array([1, 0, 0, 0]),
        np.array([0, 0, 0, 1]),
        np.array([0, 0, 1, 0]),
        np.array([0, 0, 0, 1]),
        np.array([1, 0, 0, 0])
    ]
    # Check if the expected output data frame is
    # the same as the actual output data frame.
    result = result.drop(columns=['index'])
    assert output_df.equals(result), "The prep_data function is broken!"
    return None
Пример #5
0
def test_kfold():
    model = test_model()
    n_splits = 5
    epochs = 2
    batch_size = 1
    transforms = torchvision.transforms.Compose([
        torchvision.transforms.ToPILImage(),
        torchvision.transforms.RandomRotation((-180, 180)),
        torchvision.transforms.CenterCrop((325)),
        torchvision.transforms.ToTensor()
    ])
    crit = torch.nn.CrossEntropyLoss()
    image_dir = 'tests/test_data/images_10x'
    df = prep.prep_data(pd.read_csv('tests/test_data/10x_labels_4.csv'),
                        image_dir)
    data = prep.tenX_dataset(df, image_dir, transforms)
    device = torch.device('cpu')
    models, losses, train_accs,\
        naive_accs, test_accs = construct.k_fold(n_splits,
                                                 epochs, batch_size,
                                                 transforms, crit,
                                                 model,
                                                 data, device)

    assert len(losses) is n_splits, f'kfold failed,\
        expected {n_splits} loss values, but got {len(losses)}'

    assert len(train_accs) is n_splits, f'kfold failed,\
        expected {n_splits} train lists, but got {len(train_accs)}'

    assert len(train_accs[0]) is (epochs + 1), f'kflod failed,\
        expected {epochs + 1} length train lists, but got {len(train_accs[0])}'

    assert len(naive_accs) is n_splits, f'kfold failed,\
        expected {n_splits} naive accs, but got {len(naive_accs)}'

    assert len(test_accs) is n_splits, f'kfold failed,\
        expected {n_splits} val accs, but got {len(test_accs)}'

    assert isinstance(test_accs[0].item(), float), f'kflod failed,\
        expected float, but got {test_accs[0].item().type()}'

    assert isinstance(naive_accs[0].item(), float), f'kflod failed,\
Пример #6
0
def test_train():
    """
    Tests returns types and sizes of train
    """
    image_dir = 'tests/test_data/images_10x'
    data = prep.prep_data(pd.read_csv('tests/test_data/10x_labels_4.csv'),
                          image_dir)
    transforms = torchvision.transforms.Compose([
        torchvision.transforms.ToPILImage(),
        torchvision.transforms.RandomRotation((-180, 180)),
        torchvision.transforms.CenterCrop((325)),
        torchvision.transforms.ToTensor()
    ])
    dataset = prep.tenX_dataset(data, image_dir, transforms)

    mod, loss, acc = construct.train(2, 1, dataset)
    assert isinstance(loss[0], float), f'train failed,\
        returned wrong dataype for loss: {loss.type()}'

    assert isinstance(acc[0], float), f'train failed,\
        returned wrong dataype for accuracy: {acc.type()}'

    assert len(loss) == 3, f'train failed,\
def test_prep_data_2():
    """
    Test if the prep_data function is responsive
    to a wrong datatype of the input.
    """
    image_dir = 'tests/test_data/images_10x'
    input_1 = 10
    test1 = False
    try:
        prep.prep_data(input_1, image_dir)
    except Exception as e:
        assert isinstance(e, TypeError), "Wrong type of error."
        test1 = True
    assert test1, "Test failed!\
        The prep_data function is not\
        responsive to the wrong input datatype 'int'."

    input_2 = 1.2
    test2 = False
    try:
        prep.prep_data(input_2, image_dir)
    except Exception as e:
        assert isinstance(e, TypeError), "Wrong type of error."
        test2 = True
    assert test2, "Test failed!\
        The prep_data function is not\
        responsive to the wrong input datatype 'float'."

    input_3 = [1, 2, 3]
    test3 = False
    try:
        prep.prep_data(input_3, image_dir)
    except Exception as e:
        assert isinstance(e, TypeError), "Wrong type of error."
        test3 = True
    assert test3, "Test failed!\
        The prep_data function is not\
        responsive to the wrong input datatype 'list'."

    input_4 = (1, 2, 3)
    test4 = False
    try:
        prep.prep_data(input_4, image_dir)
    except Exception as e:
        assert isinstance(e, TypeError), "Wrong type of error."
        test4 = True
    assert test4, "Test failed!\
        The prep_data function is not\
        responsive to the wrong input datatype 'tuple'."

    input_5 = 'input'
    test5 = False
    try:
        prep.prep_data(input_5, image_dir)
    except Exception as e:
        assert isinstance(e, TypeError), "Wrong type of error."
        test5 = True
    assert test5, "Test failed!\
        The prep_data function is not\
        responsive to the wrong input datatype 'str'."

    input_6 = False
    test6 = False
    try:
        prep.prep_data(input_6, image_dir)
    except Exception as e:
        assert isinstance(e, TypeError), "Wrong type of error."
        test6 = True
    assert test6, "Test failed!\
        The prep_data function is not\
        responsive to the wrong input datatype 'bool'."

    return None