def test_classification_fiftyone(tmpdir): tmpdir = Path(tmpdir) (tmpdir / "a").mkdir() (tmpdir / "b").mkdir() _rand_image().save(tmpdir / "a_1.png") _rand_image().save(tmpdir / "b_1.png") train_images = [ str(tmpdir / "a_1.png"), str(tmpdir / "b_1.png"), ] train_dataset = fo.Dataset.from_dir(str(tmpdir), dataset_type=fo.types.ImageDirectory) s1 = train_dataset[train_images[0]] s2 = train_dataset[train_images[1]] s1["test"] = fo.Classification(label="1") s2["test"] = fo.Classification(label="2") s1.save() s2.save() data = ImageClassificationData.from_fiftyone( train_dataset=train_dataset, label_field="test", batch_size=2, num_workers=0, image_size=(64, 64), ) model = ImageClassifier(num_classes=2, backbone="resnet18") trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True) trainer.finetune(model, datamodule=data, strategy="freeze")
def test_trainer_finetune(tmpdir): model = DummyClassifier() train_dl = torch.utils.data.DataLoader(DummyDataset()) val_dl = torch.utils.data.DataLoader(DummyDataset()) task = ClassificationTask(model, loss_fn=F.nll_loss) trainer = Trainer(fast_dev_run=True, default_root_dir=tmpdir) trainer.finetune(task, train_dl, val_dl, strategy=NoFreeze())
def test_finetuning(tmpdir: str, strategy): train_dl = torch.utils.data.DataLoader(DummyDataset()) val_dl = torch.utils.data.DataLoader(DummyDataset()) task = ImageClassifier(10, backbone="resnet18") trainer = Trainer(fast_dev_run=True, default_root_dir=tmpdir) if strategy == "cls": strategy = NoFreeze() if strategy == 'chocolat' or strategy is None: with pytest.raises(MisconfigurationException, match="strategy should be provided"): trainer.finetune(task, train_dl, val_dl, strategy=strategy) else: trainer.finetune(task, train_dl, val_dl, strategy=strategy)
def test_classification(tmpdir): data = ImageClassificationData.from_filepaths( train_filepaths=["a", "b"], train_labels=[0, 1], train_transform=lambda x: x, loader=_dummy_image_loader, num_workers=0, batch_size=2, ) model = ImageClassifier(2, backbone="resnet18") trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True) trainer.finetune(model, datamodule=data, strategy="freeze")
def test_multilabel(tmpdir): num_classes = 4 ds = DummyMultiLabelDataset(num_classes) model = ImageClassifier(num_classes, multi_label=True, serializer=Probabilities(multi_label=True)) train_dl = torch.utils.data.DataLoader(ds, batch_size=2) trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True) trainer.finetune(model, train_dl, strategy="freeze_unfreeze") image, label = ds[0][DefaultDataKeys.INPUT], ds[0][DefaultDataKeys.TARGET] predictions = model.predict([{DefaultDataKeys.INPUT: image}]) assert (torch.tensor(predictions) > 1).sum() == 0 assert (torch.tensor(predictions) < 0).sum() == 0 assert len(predictions[0]) == num_classes == len(label) assert len(torch.unique(label)) <= 2
def test_classification(tmpdir): tmpdir = Path(tmpdir) (tmpdir / "a").mkdir() (tmpdir / "b").mkdir() _rand_image().save(tmpdir / "a" / "a_1.png") _rand_image().save(tmpdir / "b" / "a_1.png") data = ImageClassificationData.from_filepaths( train_filepaths=[tmpdir / "a", tmpdir / "b"], train_labels=[0, 1], train_transform={"per_batch_transform": lambda x: x}, num_workers=0, batch_size=2, ) model = ImageClassifier(num_classes=2, backbone="resnet18") trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True) trainer.finetune(model, datamodule=data, strategy="freeze")
def test_classification(tmpdir): tmpdir = Path(tmpdir) (tmpdir / "a").mkdir() (tmpdir / "b").mkdir() image_a = str(tmpdir / "a" / "a_1.png") image_b = str(tmpdir / "b" / "b_1.png") _rand_image().save(image_a) _rand_image().save(image_b) data = ImageClassificationData.from_files( train_files=[image_a, image_b], train_targets=[0, 1], num_workers=0, batch_size=2, image_size=(64, 64), ) model = ImageClassifier(num_classes=2, backbone="resnet18") trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True) trainer.finetune(model, datamodule=data, strategy="freeze")
def test_map_labels(tmpdir): tmp_dir = Path(tmpdir) # create random dummy data images = [ str(tmp_dir / "img1.png"), str(tmp_dir / "img2.png"), str(tmp_dir / "img3.png"), ] targets = [ str(tmp_dir / "labels_img1.png"), str(tmp_dir / "labels_img2.png"), str(tmp_dir / "labels_img3.png"), ] labels_map: Dict[int, Tuple[int, int, int]] = { 0: [0, 0, 0], 1: [255, 255, 255], } num_classes: int = len(labels_map.keys()) img_size: Tuple[int, int] = (128, 128) create_random_data(images, targets, img_size, num_classes) # instantiate the data module dm = SemanticSegmentationData.from_files( train_files=images, train_targets=targets, val_files=images, val_targets=targets, batch_size=2, num_workers=0, num_classes=num_classes, ) assert dm is not None assert dm.train_dataloader() is not None # disable visualisation for testing assert dm.data_fetcher.block_viz_window is True dm.set_block_viz_window(False) assert dm.data_fetcher.block_viz_window is False dm.show_train_batch("load_sample") dm.show_train_batch("to_tensor_transform") # check training data data = next(iter(dm.train_dataloader())) imgs, labels = data[DefaultDataKeys.INPUT], data[DefaultDataKeys.TARGET] assert imgs.shape == (2, 3, 128, 128) assert labels.shape == (2, 128, 128) assert labels.min().item() == 0 assert labels.max().item() == 1 assert labels.dtype == torch.int64 # now train with `fast_dev_run` model = SemanticSegmentation(num_classes=2, backbone="resnet50", head="fpn") trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True) trainer.finetune(model, dm, strategy="freeze_unfreeze")
def test_init_train(tmpdir, backbone, metrics): model = ImageClassifier(10, backbone=backbone, metrics=metrics) train_dl = torch.utils.data.DataLoader(DummyDataset()) trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True) trainer.finetune(model, train_dl, strategy="freeze_unfreeze")
# 1. Create the DataModule download_data("https://pl-flash-data.s3.amazonaws.com/xsum.zip", "./data/") datamodule = SummarizationData.from_csv( "input", "target", train_file="data/xsum/train.csv", val_file="data/xsum/valid.csv", ) # 2. Build the task model = SummarizationTask() # 3. Create the trainer and finetune the model trainer = Trainer(max_epochs=3) trainer.finetune(model, datamodule=datamodule) # 4. Summarize some text! predictions = model.predict(""" Camilla bought a box of mangoes with a Brixton £10 note, introduced last year to try to keep the money of local people within the community.The couple were surrounded by shoppers as they walked along Electric Avenue. They came to Brixton to see work which has started to revitalise the borough. It was Charles' first visit to the area since 1996, when he was accompanied by the former South African president Nelson Mandela.Greengrocer Derek Chong, who has run a stall on Electric Avenue for 20 years, said Camilla had been ""nice and pleasant"" when she purchased the fruit. ""She asked me what was nice, what would I recommend, and I said we've got some nice mangoes. She asked me were they ripe and I said yes - they're from the Dominican Republic."" Mr Chong is one of 170 local retailers who accept the Brixton Pound. Customers exchange traditional pound coins for Brixton Pounds and then spend them at the market or in participating shops. During the visit, Prince Charles spent time talking to youth worker Marcus West, who works with children
def test_init_train(tmpdir, backbone): model = SemanticSegmentation(num_classes=10, backbone=backbone) train_dl = torch.utils.data.DataLoader(DummyDataset()) trainer = Trainer(default_root_dir=tmpdir, fast_dev_run=True) trainer.finetune(model, train_dl, strategy="freeze_unfreeze")
download_data("https://pl-flash-data.s3.amazonaws.com/xsum.zip", "./data/") datamodule = SummarizationData.from_csv( "input", "target", train_file="data/xsum/train.csv", val_file="data/xsum/valid.csv", batch_size=4, ) # 2. Build the task model = SummarizationTask() # 3. Create the trainer and finetune the model trainer = Trainer(max_epochs=3) trainer.finetune(model, datamodule=datamodule, strategy="freeze") # 4. Summarize some text! datamodule = SummarizationData.from_lists( predict_data=[ """ Camilla bought a box of mangoes with a Brixton £10 note, introduced last year to try to keep the money of local people within the community.The couple were surrounded by shoppers as they walked along Electric Avenue. They came to Brixton to see work which has started to revitalise the borough. It was Charles' first visit to the area since 1996, when he was accompanied by the former South African president Nelson Mandela.Greengrocer Derek Chong, who has run a stall on Electric Avenue for 20 years, said Camilla had been ""nice and pleasant"" when she purchased the fruit. ""She asked me what was nice, what would I recommend, and I said we've got some nice mangoes. She asked me were they ripe and I said yes - they're from the Dominican Republic."" Mr Chong is one of 170 local retailers who accept the Brixton Pound. Customers exchange traditional pound coins for Brixton Pounds and then spend them at the market