From 38a7d0510138a60a7109c149b68ae79d9ad2b9dc Mon Sep 17 00:00:00 2001 From: sammlapp Date: Thu, 15 Dec 2022 14:57:12 -0500 Subject: [PATCH 1/4] add caveat for bandpassing resolves #601 --- docs/tutorials/cnn.ipynb | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/docs/tutorials/cnn.ipynb b/docs/tutorials/cnn.ipynb index 73943e00..192eef0b 100644 --- a/docs/tutorials/cnn.ipynb +++ b/docs/tutorials/cnn.ipynb @@ -92,8 +92,8 @@ " Dload Upload Total Spent Left Speed\n", " 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\n", " 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\n", - "100 7 0 7 0 0 6 0 --:--:-- 0:00:01 --:--:-- 0\n", - "100 9499k 100 9499k 0 0 4778k 0 0:00:01 0:00:01 --:--:-- 20.5M\n" + "100 7 0 7 0 0 5 0 --:--:-- 0:00:01 --:--:-- 5\n", + "100 9499k 100 9499k 0 0 3663k 0 0:00:02 0:00:02 --:--:-- 7642k\n" ] }, { @@ -468,7 +468,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -477,6 +477,16 @@ "model = CNN('resnet18',classes=classes,sample_duration=2.0,single_target=True)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "_CAVEAT_: the default audio preprocessing in this class bandpasses spectrograms to 0-11025 Hz. If your audio has a sample rate of less than 22050 Hz, the preprocessing will raise an error because the spectrogram will not contain the expected frequencies. In this case you could change the parameters of the bandpass action, or simply disable the bandpass action:\n", + "```\n", + "model.preprocessor.pipeline.bandpass.bypass=True\n", + "```" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -487,7 +497,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -1980,7 +1990,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.12" + "version": "3.9.13" } }, "nbformat": 4, From c29314b56666643c541e4214d969fa1b2fb28e46 Mon Sep 17 00:00:00 2001 From: sammlapp Date: Thu, 15 Dec 2022 15:02:17 -0500 Subject: [PATCH 2/4] remove outdated PytorchModel references #586 --- opensoundscape/torch/architectures/cnn_architectures.py | 4 +--- tests/test_cnn.py | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/opensoundscape/torch/architectures/cnn_architectures.py b/opensoundscape/torch/architectures/cnn_architectures.py index fe6e8321..2feb60ed 100644 --- a/opensoundscape/torch/architectures/cnn_architectures.py +++ b/opensoundscape/torch/architectures/cnn_architectures.py @@ -19,7 +19,7 @@ Then you can initialize a model object from `opensoundscape.torch.models.cnn` with your architecture: -`model=PytorchModel(my_arch,classes)` +`model=CNN(my_arch,classes,sample_duration)` or override an existing model's architecture: @@ -28,8 +28,6 @@ Note: the InceptionV3 architecture must be used differently than other architectures - the easiest way is to simply use the InceptionV3 class in opensoundscape.torch.models.cnn. - -Note 2: For resnet architectures, if num_channels != 3, averages the conv1 weights across all channels. """ from torchvision import models from torch import nn diff --git a/tests/test_cnn.py b/tests/test_cnn.py index 3e12911e..d167594f 100644 --- a/tests/test_cnn.py +++ b/tests/test_cnn.py @@ -180,7 +180,7 @@ def test_train_predict_inception(train_df): def test_train_predict_architecture(train_df): - """test passing a specific architecture to PytorchModel""" + """test passing architecture object to CNN class""" arch = alexnet(2, use_pretrained=False) model = cnn.CNN(arch, [0, 1], sample_duration=2) model.train( From a16acf34fbff3a6320d117b9d1a4b967b474c251 Mon Sep 17 00:00:00 2001 From: sammlapp Date: Thu, 15 Dec 2022 15:03:50 -0500 Subject: [PATCH 3/4] resolves #584 incorrect return value documentation --- opensoundscape/ribbit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opensoundscape/ribbit.py b/opensoundscape/ribbit.py index afd62cce..3283e1ff 100644 --- a/opensoundscape/ribbit.py +++ b/opensoundscape/ribbit.py @@ -106,7 +106,7 @@ def ribbit( plot=False: if True, plot the power spectral density for each clip Returns: - DataFrame of index=('start_time','end_time'), columns=['score'], + DataFrame with columns ['start_time','end_time','score'], with a row for each clip. Notes From 50c5adc27a0e97fe47ee7d1b02eaddf04b7c4989 Mon Sep 17 00:00:00 2001 From: sammlapp Date: Thu, 15 Dec 2022 15:28:47 -0500 Subject: [PATCH 4/4] document use of preprocessor for CNN #527 #580 --- docs/tutorials/preprocessors.ipynb | 43 +++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/docs/tutorials/preprocessors.ipynb b/docs/tutorials/preprocessors.ipynb index 5abc1679..88000803 100644 --- a/docs/tutorials/preprocessors.ipynb +++ b/docs/tutorials/preprocessors.ipynb @@ -28,7 +28,28 @@ "\n", "it also uses the Dataset classes to demonstrate\n", "- how to load one sample per file path\n", - "- how to load long audio files as a series of shorter clips" + "- how to load long audio files as a series of shorter clips\n", + "\n", + "\n", + "### Modifying the preprocessor of the CNN class\n", + "When training a CNN model in OpenSoundscape, you will create an object of the CNN class. There are two ways to modify the preprocessing:\n", + "\n", + "1) modify the model.preprocessor directly\n", + "The model contains a preprocessor object that you can modify, for instance:\n", + "```\n", + "model.preprocessor.pipeline.bandpass.bypass = True\n", + "```\n", + "\n", + "2) overwrite the preprocessor with a new one:\n", + "```\n", + "my_preprocessor = SpectrogramPreprocessor(....) #this tutorial will help you with how to make this object\n", + "model.preprocessor = my_preprocessor\n", + "```\n", + "Note that if you want to create a preprocessor with overlay augmentation, it's easiest to use option 2 and initialize the preprocessor with an overlay_df. \n", + "\n", + "Note on augmentations: \n", + "- While training, the CNN class will use all actions in the preprocessor's pipeline. \n", + "- When runing validation or prediction, by default, the CNN will bypass actions with action.is_augmentation==True." ] }, { @@ -1087,6 +1108,26 @@ "dataset[0]['X'].plot()" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# adding the preprocessor to a CNN" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You can always overwrite the preprocessor of a CNN model object with a new one:\n", + "```\n", + "my_preprocessor = SpectrogramPreprocessor(....) \n", + "...\n", + "model.preprocessor = my_preprocessor\n", + "```\n", + "_WARNING_: Be careful! If your new preprocessor has a different sample duration (eg 3 seconds instead of 2) or shape (eg [100,100,3] instead of [224,224,1]), these new values will also take effect when using the CNN. " + ] + }, { "cell_type": "markdown", "metadata": {},