def test_model(coco_instances, backbone, head):
    datamodule = InstanceSegmentationData.from_coco(
        train_folder=coco_instances.train_folder,
        train_ann_file=coco_instances.train_ann_file,
        predict_folder=coco_instances.predict_folder,
        transform_kwargs=dict(image_size=(128, 128)),
        batch_size=2,
    )

    assert datamodule.num_classes == 3
    assert datamodule.labels == ["background", "cat", "dog"]

    model = InstanceSegmentation(num_classes=datamodule.num_classes, backbone=backbone, head=head)
    trainer = Trainer(fast_dev_run=True)
    trainer.fit(model, datamodule=datamodule)
    trainer.predict(model, datamodule=datamodule)
Exemple #2
0
def test_predict_sklearn():
    """Tests that we can generate predictions from a scikit-learn ``Bunch``."""
    bunch = datasets.load_iris()
    model = TemplateSKLearnClassifier(num_features=DummyDataset.num_features,
                                      num_classes=DummyDataset.num_classes)
    datamodule = TemplateData.from_sklearn(predict_bunch=bunch, batch_size=1)
    trainer = Trainer()
    out = trainer.predict(model, datamodule=datamodule, output="classes")
    assert isinstance(out[0][0], int)
Exemple #3
0
def test_predict_numpy():
    """Tests that we can generate predictions from a numpy array."""
    row = np.random.rand(1, DummyDataset.num_features)
    model = TemplateSKLearnClassifier(num_features=DummyDataset.num_features,
                                      num_classes=DummyDataset.num_classes)
    datamodule = TemplateData.from_numpy(predict_data=row, batch_size=1)
    trainer = Trainer()
    out = trainer.predict(model, datamodule=datamodule, output="classes")
    assert isinstance(out[0][0], int)
def test_predict_numpy():
    img = np.ones((1, 3, 64, 64))
    model = SemanticSegmentation(2, backbone="mobilenetv3_large_100")
    datamodule = SemanticSegmentationData.from_numpy(predict_data=img,
                                                     batch_size=1)
    trainer = Trainer()
    out = trainer.predict(model, datamodule=datamodule, output="labels")
    assert isinstance(out[0][0], list)
    assert len(out[0][0]) == 64
    assert len(out[0][0][0]) == 64
Exemple #5
0
def test_predict_dataset(tmpdir):
    """Tests that we can generate embeddings from a pytorch geometric dataset."""
    tudataset = datasets.TUDataset(root=tmpdir, name="KKI")
    model = GraphEmbedder(
        GraphClassifier(num_features=tudataset.num_features,
                        num_classes=tudataset.num_classes).backbone)
    datamodule = DataModule(
        predict_input=GraphClassificationDatasetInput(RunningStage.PREDICTING,
                                                      tudataset),
        transform=GraphClassificationInputTransform,
        batch_size=4,
    )
    trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True)
    out = trainer.predict(model, datamodule=datamodule)
    assert isinstance(out[0][0], torch.Tensor)
def test_pointcloud_segmentation_data(tmpdir):

    seed_everything(52)

    download_data(
        "https://pl-flash-data.s3.amazonaws.com/SemanticKittiMicro.zip",
        tmpdir)

    datamodule = PointCloudSegmentationData.from_folders(
        train_folder=join(tmpdir, "SemanticKittiMicro", "train"),
        predict_folder=join(tmpdir, "SemanticKittiMicro", "predict"),
        batch_size=4,
    )

    class MockModel(PointCloudSegmentation):
        def training_step(self, batch, batch_idx: int):
            assert batch[DataKeys.INPUT]["xyz"][0].shape == torch.Size(
                [2, 45056, 3])
            assert batch[DataKeys.INPUT]["xyz"][1].shape == torch.Size(
                [2, 11264, 3])
            assert batch[DataKeys.INPUT]["xyz"][2].shape == torch.Size(
                [2, 2816, 3])
            assert batch[DataKeys.INPUT]["xyz"][3].shape == torch.Size(
                [2, 704, 3])
            assert batch[DataKeys.INPUT]["labels"].shape == torch.Size(
                [2, 45056])
            assert batch[DataKeys.INPUT]["labels"].max() == 19
            assert batch[DataKeys.INPUT]["labels"].min() == 0
            assert batch[DataKeys.METADATA][0]["name"] in ("00_000000",
                                                           "00_000001")
            assert batch[DataKeys.METADATA][1]["name"] in ("00_000000",
                                                           "00_000001")

    num_classes = 19
    model = MockModel(backbone="randlanet", num_classes=num_classes)
    trainer = Trainer(max_epochs=1, limit_train_batches=1, limit_val_batches=0)
    trainer.fit(model, datamodule=datamodule)

    predictions = trainer.predict(model, datamodule=datamodule)[0]
    assert predictions[0][DataKeys.INPUT].shape == torch.Size([45056, 3])
    assert predictions[0][DataKeys.PREDS].shape == torch.Size([45056, 19])
    assert predictions[0][DataKeys.TARGET].shape == torch.Size([45056])
Exemple #7
0
def test_data_module():
    seed_everything(42)

    def train_fn(data):
        return data - 100

    def val_fn(data):
        return data + 100

    def test_fn(data):
        return data - 1000

    def predict_fn(data):
        return data + 1000

    @dataclass
    class TestTransform(InputTransform):
        def per_sample_transform(self):
            def fn(x):
                return x

            return fn

        def train_per_batch_transform_on_device(self) -> Callable:
            return train_fn

        def val_per_batch_transform_on_device(self) -> Callable:
            return val_fn

        def test_per_batch_transform_on_device(self) -> Callable:
            return test_fn

        def predict_per_batch_transform_on_device(self) -> Callable:
            return predict_fn

    transform = TestTransform()
    assert transform._transform is not None

    train_dataset = Input(RunningStage.TRAINING, np.arange(10,
                                                           dtype=np.float32))
    assert train_dataset.running_stage == RunningStage.TRAINING

    val_dataset = Input(RunningStage.VALIDATING, np.arange(10,
                                                           dtype=np.float32))
    assert val_dataset.running_stage == RunningStage.VALIDATING

    test_dataset = Input(RunningStage.TESTING, np.arange(10, dtype=np.float32))
    assert test_dataset.running_stage == RunningStage.TESTING

    predict_dataset = Input(RunningStage.PREDICTING,
                            np.arange(10, dtype=np.float32))
    assert predict_dataset.running_stage == RunningStage.PREDICTING

    dm = DataModule(
        train_input=train_dataset,
        val_input=val_dataset,
        test_input=test_dataset,
        predict_input=predict_dataset,
        transform=transform,
        batch_size=2,
    )
    assert len(dm.train_dataloader()) == 5
    batch = next(iter(dm.train_dataloader()))
    assert batch.shape == torch.Size([2])
    assert batch.min() >= 0 and batch.max() < 10

    assert len(dm.val_dataloader()) == 5
    batch = next(iter(dm.val_dataloader()))
    assert batch.shape == torch.Size([2])
    assert batch.min() >= 0 and batch.max() < 10

    class TestModel(Task):
        def training_step(self, batch, batch_idx):
            assert sum(batch < 0) == 2

        def validation_step(self, batch, batch_idx):
            assert sum(batch > 0) == 2

        def test_step(self, batch, batch_idx):
            assert sum(batch < 500) == 2

        def predict_step(self, batch, *args, **kwargs):
            assert sum(batch > 500) == 2
            assert torch.equal(batch, torch.tensor([1000.0, 1001.0]))

        def on_train_dataloader(self) -> None:
            pass

        def on_val_dataloader(self) -> None:
            pass

        def on_test_dataloader(self, *_) -> None:
            pass

        def on_predict_dataloader(self) -> None:
            pass

        def on_predict_end(self) -> None:
            pass

        def on_fit_end(self) -> None:
            pass

    model = TestModel(torch.nn.Linear(1, 1))
    trainer = Trainer(fast_dev_run=True)
    trainer.fit(model, datamodule=dm)
    trainer.validate(model, datamodule=dm)
    trainer.test(model, datamodule=dm)
    trainer.predict(model, datamodule=dm)

    # Test that plain lightning module works with FlashDataModule
    class SampleBoringModel(BoringModel):
        def __init__(self):
            super().__init__()
            self.layer = torch.nn.Linear(2, 1)

    model = SampleBoringModel()
    trainer = Trainer(fast_dev_run=True)
    trainer.fit(model, datamodule=dm)
    trainer.validate(model, datamodule=dm)
    trainer.test(model, datamodule=dm)
    trainer.predict(model, datamodule=dm)

    transform = TestTransform()
    input = Input(RunningStage.TRAINING)
    dm = DataModule(train_input=input, batch_size=1, transform=transform)
    assert isinstance(dm.input_transform, TestTransform)

    class RandomDataset(Dataset):
        def __init__(self, size: int, length: int):
            self.len = length
            self.data = torch.ones(length, size)

        def __getitem__(self, index):
            return self.data[index]

        def __len__(self):
            return self.len

    def _add_hundred(x):
        if isinstance(x, Dict):
            x["input"] += 100
        else:
            x += 100
        return x

    class TrainInputTransform(InputTransform):
        def _add_one(self, x):
            if isinstance(x, Dict):
                x["input"] += 1
            else:
                x += 1
            return x

        def per_sample_transform(self) -> Callable:
            return self._add_one

        def val_per_sample_transform(self) -> Callable:
            return _add_hundred

    dm = DataModule(
        train_input=DatasetInput(RunningStage.TRAINING, RandomDataset(64, 32)),
        val_input=DatasetInput(RunningStage.VALIDATING, RandomDataset(64, 32)),
        test_input=DatasetInput(RunningStage.TESTING, RandomDataset(64, 32)),
        batch_size=3,
        transform=TrainInputTransform(),
    )
    batch = next(iter(dm.train_dataloader()))
    assert batch["input"][0][0] == 2
    batch = next(iter(dm.val_dataloader()))
    assert batch["input"][0][0] == 101
    batch = next(iter(dm.test_dataloader()))
    assert batch["input"][0][0] == 2
            """
        The Normans (Norman: Nourmands; French: Normands; Latin: Normanni) were the people who in the 10th
        and 11th centuries gave their name to Normandy, a region in France. They were descended from Norse
        ("Norman" comes from "Norseman") raiders and pirates from Denmark, Iceland and Norway who, under
        their leader Rollo, agreed to swear fealty to King Charles III of West Francia. Through generations
        of assimilation and mixing with the native Frankish and Roman-Gaulish populations, their
        descendants would gradually merge with the Carolingian-based cultures of West Francia. The distinct
        cultural and ethnic identity of the Normans emerged initially in the first half of the 10th
        century, and it continued to evolve over the succeeding centuries.
        """,
            """
        The Normans (Norman: Nourmands; French: Normands; Latin: Normanni) were the people who in the 10th
        and 11th centuries gave their name to Normandy, a region in France. They were descended from Norse
        ("Norman" comes from "Norseman") raiders and pirates from Denmark, Iceland and Norway who, under
        their leader Rollo, agreed to swear fealty to King Charles III of West Francia. Through generations
        of assimilation and mixing with the native Frankish and Roman-Gaulish populations, their
        descendants would gradually merge with the Carolingian-based cultures of West Francia. The distinct
        cultural and ethnic identity of the Normans emerged initially in the first half of the 10th
        century, and it continued to evolve over the succeeding centuries.
        """,
        ],
        "question": ["When were the Normans in Normandy?", "In what country is Normandy located?"],
    },
    batch_size=4,
)
predictions = trainer.predict(model, datamodule=datamodule)
print(predictions)

# 5. Save the model!
trainer.save_checkpoint("question_answering_on_sqaud_v2.pt")