def test_from_filepaths_visualise(tmpdir): tmpdir = Path(tmpdir) _rand_image().save(tmpdir / "e_1.png") train_images = [ str(tmpdir / "e_1.png"), str(tmpdir / "e_1.png"), str(tmpdir / "e_1.png"), ] dm = AudioClassificationData.from_files( train_files=train_images, train_targets=[0, 3, 6], val_files=train_images, val_targets=[1, 4, 7], test_files=train_images, test_targets=[2, 5, 8], batch_size=2, num_workers=0, ) # 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 # call show functions # dm.show_train_batch() dm.show_train_batch("per_sample_transform") dm.show_train_batch(["per_sample_transform", "per_batch_transform"])
def test_from_filepaths_visualise_multilabel(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) dm = AudioClassificationData.from_files( train_files=[image_a, image_b], train_targets=[[0, 1, 0], [0, 1, 1]], val_files=[image_b, image_a], val_targets=[[1, 1, 0], [0, 0, 1]], test_files=[image_b, image_b], test_targets=[[0, 0, 1], [1, 1, 0]], batch_size=2, transform_kwargs=dict(spectrogram_size=(64, 64)), ) # 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 # call show functions dm.show_train_batch() dm.show_train_batch("per_sample_transform") dm.show_val_batch("per_batch_transform")
def test_from_filepaths_smoke(tmpdir): tmpdir = Path(tmpdir) _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"), ] spectrograms_data = AudioClassificationData.from_files( train_files=train_images, train_targets=[1, 2], batch_size=2, num_workers=0, ) assert spectrograms_data.train_dataloader() is not None assert spectrograms_data.val_dataloader() is None assert spectrograms_data.test_dataloader() is None data = next(iter(spectrograms_data.train_dataloader())) imgs, labels = data["input"], data["target"] assert imgs.shape == (2, 3, 128, 128) assert labels.shape == (2, ) assert sorted(list(labels.numpy())) == [1, 2]
def run(transform: Any = None): dm = AudioClassificationData.from_files( train_files=train_filepaths, train_targets=train_labels, transform=transform, batch_size=B, num_workers=0, val_split=val_split, ) data = next(iter(dm.train_dataloader())) imgs, labels = data["input"], data["target"] assert imgs.shape == (B, 3, H, W) assert labels.shape == (B, )
def from_urban8k( batch_size: int = 4, **data_module_kwargs, ) -> AudioClassificationData: """Downloads and loads the Urban 8k sounds images data set.""" download_data("https://pl-flash-data.s3.amazonaws.com/urban8k_images.zip", "./data") return AudioClassificationData.from_folders( train_folder="data/urban8k_images/train", val_folder="data/urban8k_images/val", batch_size=batch_size, **data_module_kwargs, )
def test_from_filepaths(tmpdir, file_generator): train_images, channels = file_generator(tmpdir) spectrograms_data = AudioClassificationData.from_files( train_files=train_images, train_targets=[1, 2], batch_size=2, num_workers=0, ) assert spectrograms_data.train_dataloader() is not None data = next(iter(spectrograms_data.train_dataloader())) imgs, labels = data["input"], data["target"] assert imgs.shape == (2, channels, 128, 128) assert labels.shape == (2, ) assert sorted(list(labels.numpy())) == [1, 2]
def test_from_filepaths_list_image_paths(tmpdir): tmpdir = Path(tmpdir) _rand_image().save(tmpdir / "e_1.png") train_images = [ str(tmpdir / "e_1.png"), str(tmpdir / "e_1.png"), str(tmpdir / "e_1.png"), ] spectrograms_data = AudioClassificationData.from_files( train_files=train_images, train_targets=[0, 3, 6], val_files=train_images, val_targets=[1, 4, 7], test_files=train_images, test_targets=[2, 5, 8], batch_size=2, num_workers=0, ) # check training data data = next(iter(spectrograms_data.train_dataloader())) imgs, labels = data["input"], data["target"] assert imgs.shape == (2, 3, 128, 128) assert labels.shape == (2, ) assert labels.numpy()[0] in [0, 3, 6] # data comes shuffled here assert labels.numpy()[1] in [0, 3, 6] # data comes shuffled here # check validation data data = next(iter(spectrograms_data.val_dataloader())) imgs, labels = data["input"], data["target"] assert imgs.shape == (2, 3, 128, 128) assert labels.shape == (2, ) assert list(labels.numpy()) == [1, 4] # check test data data = next(iter(spectrograms_data.test_dataloader())) imgs, labels = data["input"], data["target"] assert imgs.shape == (2, 3, 128, 128) assert labels.shape == (2, ) assert list(labels.numpy()) == [2, 5]
def test_from_filepaths_multilabel(tmpdir): tmpdir = Path(tmpdir) (tmpdir / "a").mkdir() _rand_image().save(tmpdir / "a1.png") _rand_image().save(tmpdir / "a2.png") train_images = [str(tmpdir / "a1.png"), str(tmpdir / "a2.png")] train_labels = [[1, 0, 1, 0], [0, 0, 1, 1]] valid_labels = [[1, 1, 1, 0], [1, 0, 0, 1]] test_labels = [[1, 0, 1, 0], [1, 1, 0, 1]] dm = AudioClassificationData.from_files( train_files=train_images, train_targets=train_labels, val_files=train_images, val_targets=valid_labels, test_files=train_images, test_targets=test_labels, batch_size=2, num_workers=0, ) data = next(iter(dm.train_dataloader())) imgs, labels = data["input"], data["target"] assert imgs.shape == (2, 3, 128, 128) assert labels.shape == (2, 4) data = next(iter(dm.val_dataloader())) imgs, labels = data["input"], data["target"] assert imgs.shape == (2, 3, 128, 128) assert labels.shape == (2, 4) torch.testing.assert_allclose(labels, torch.tensor(valid_labels)) data = next(iter(dm.test_dataloader())) imgs, labels = data["input"], data["target"] assert imgs.shape == (2, 3, 128, 128) assert labels.shape == (2, 4) torch.testing.assert_allclose(labels, torch.tensor(test_labels))
def test_from_folders_only_train(tmpdir): seed_everything(42) train_dir = Path(tmpdir / "train") train_dir.mkdir() (train_dir / "a").mkdir() _rand_image().save(train_dir / "a" / "1.png") _rand_image().save(train_dir / "a" / "2.png") (train_dir / "b").mkdir() _rand_image().save(train_dir / "b" / "1.png") _rand_image().save(train_dir / "b" / "2.png") spectrograms_data = AudioClassificationData.from_folders(train_dir, batch_size=1) data = next(iter(spectrograms_data.train_dataloader())) imgs, labels = data["input"], data["target"] assert imgs.shape == (1, 3, 128, 128) assert labels.shape == (1, )
def test_from_folders_train_val(tmpdir): seed_everything(42) train_dir = Path(tmpdir / "train") train_dir.mkdir() (train_dir / "a").mkdir() _rand_image().save(train_dir / "a" / "1.png") _rand_image().save(train_dir / "a" / "2.png") (train_dir / "b").mkdir() _rand_image().save(train_dir / "b" / "1.png") _rand_image().save(train_dir / "b" / "2.png") spectrograms_data = AudioClassificationData.from_folders( train_dir, val_folder=train_dir, test_folder=train_dir, batch_size=2, num_workers=0, ) data = next(iter(spectrograms_data.train_dataloader())) imgs, labels = data["input"], data["target"] assert imgs.shape == (2, 3, 128, 128) assert labels.shape == (2, ) assert list(labels.numpy()) == [0, 1] data = next(iter(spectrograms_data.val_dataloader())) imgs, labels = data["input"], data["target"] assert imgs.shape == (2, 3, 128, 128) assert labels.shape == (2, ) assert list(labels.numpy()) == [0, 0] data = next(iter(spectrograms_data.test_dataloader())) imgs, labels = data["input"], data["target"] assert imgs.shape == (2, 3, 128, 128) assert labels.shape == (2, ) assert list(labels.numpy()) == [0, 0]
# See the License for the specific language governing permissions and # limitations under the License. import torch import flash from flash.audio import AudioClassificationData from flash.core.data.utils import download_data from flash.core.finetuning import FreezeUnfreeze from flash.image import ImageClassifier # 1. Create the DataModule download_data("https://pl-flash-data.s3.amazonaws.com/urban8k_images.zip", "./data") datamodule = AudioClassificationData.from_folders( train_folder="data/urban8k_images/train", val_folder="data/urban8k_images/val", spectrogram_size=(64, 64), ) # 2. Build the model. model = ImageClassifier(backbone="resnet18", num_classes=datamodule.num_classes) # 3. Create the trainer and finetune the model trainer = flash.Trainer(max_epochs=3, gpus=torch.cuda.device_count()) trainer.finetune(model, datamodule=datamodule, strategy=FreezeUnfreeze(unfreeze_epoch=1)) # 4. Predict what's on few images! air_conditioner, children_playing, siren e.t.c predictions = model.predict( [ "data/urban8k_images/test/air_conditioner/13230-0-0-5.wav.jpg", "data/urban8k_images/test/children_playing/9223-2-0-15.wav.jpg",
# See the License for the specific language governing permissions and # limitations under the License. import torch import flash from flash.audio import AudioClassificationData from flash.core.data.utils import download_data from flash.image import ImageClassifier # 1. Create the DataModule download_data("https://pl-flash-data.s3.amazonaws.com/urban8k_images.zip", "./data") datamodule = AudioClassificationData.from_folders( train_folder="data/urban8k_images/train", val_folder="data/urban8k_images/val", transform_kwargs=dict(spectrogram_size=(64, 64)), batch_size=4, ) # 2. Build the model. model = ImageClassifier(backbone="resnet18", labels=datamodule.labels) # 3. Create the trainer and finetune the model trainer = flash.Trainer(max_epochs=3, gpus=torch.cuda.device_count()) trainer.finetune(model, datamodule=datamodule, strategy=("freeze_unfreeze", 1)) # 4. Predict what's on few images! air_conditioner, children_playing, siren etc. datamodule = AudioClassificationData.from_files( predict_files=[ "data/urban8k_images/test/air_conditioner/13230-0-0-5.wav.jpg", "data/urban8k_images/test/children_playing/9223-2-0-15.wav.jpg",