{
  "id": 164033,
  "title": "Audio to spectrum using librosa",
  "url": "/competitions/birdsong-recognition/discussion/164033",
  "author_name": "",
  "post_date": "2020-07-04T13:35:10.857044Z",
  "votes": null,
  "comment_count": 4,
  "views": 0,
  "content": "<p>I'm trying to convert the audio into spectrograms using librosa library. But I'm running our of memory everytime. Code snippet:</p>\n\n<p>def create_fold_spectrograms(audio_file_path,save_img_path ):</p>\n\n<pre><code>audio, sr = librosa.load(audio_file_path, sr =None)\nfig = plt.figure(figsize=[0.72,0.72])\nax = fig.add_subplot(111)\nax.axes.get_xaxis().set_visible(False)\nax.axes.get_yaxis().set_visible(False)\nax.set_frame_on(False)\nfilename  = ath(audio_file_path).name.replace('.mp3','.png').replace('.mp2','.png').replace('.aac','.png').replace('.wav','.png')\nS = librosa.feature.melspectrogram(y=audio, sr=sr)\nlibrosa.display.specshow(librosa.power_to_db(S, ref=np.max))\nfig.savefig(os.path.join(save_img_path, filename), dpi=None, bbox_inches='tight',pad_inches=0)\nfig.clear()\nplt.close()\nfig.clf()\n</code></pre>\n\n<p>I've tried adding .clf(), .close(), .clear() but still doesn't work. My notebook:</p>\n\n<p><a href=\"https://www.kaggle.com/pawan28a95/simple-audio-to-spectrum-images?scriptVersionId=38073098\">Audio to spectrum</a></p>\n\n<p>Thanks</p>",
  "messages": [
    {
      "id": "915117",
      "postDate": "07/04/2020 13:35:10",
      "content": "<p>I'm trying to convert the audio into spectrograms using librosa library. But I'm running our of memory everytime. Code snippet:</p>\n\n<p>def create_fold_spectrograms(audio_file_path,save_img_path ):</p>\n\n<pre><code>audio, sr = librosa.load(audio_file_path, sr =None)\nfig = plt.figure(figsize=[0.72,0.72])\nax = fig.add_subplot(111)\nax.axes.get_xaxis().set_visible(False)\nax.axes.get_yaxis().set_visible(False)\nax.set_frame_on(False)\nfilename  = ath(audio_file_path).name.replace('.mp3','.png').replace('.mp2','.png').replace('.aac','.png').replace('.wav','.png')\nS = librosa.feature.melspectrogram(y=audio, sr=sr)\nlibrosa.display.specshow(librosa.power_to_db(S, ref=np.max))\nfig.savefig(os.path.join(save_img_path, filename), dpi=None, bbox_inches='tight',pad_inches=0)\nfig.clear()\nplt.close()\nfig.clf()\n</code></pre>\n\n<p>I've tried adding .clf(), .close(), .clear() but still doesn't work. My notebook:</p>\n\n<p><a href=\"https://www.kaggle.com/pawan28a95/simple-audio-to-spectrum-images?scriptVersionId=38073098\">Audio to spectrum</a></p>\n\n<p>Thanks</p>",
      "rawMarkdown": "I'm trying to convert the audio into spectrograms using librosa library. But I'm running our of memory everytime. Code snippet:\n\ndef create_fold_spectrograms(audio_file_path,save_img_path ):\n    \n    audio, sr = librosa.load(audio_file_path, sr =None)\n    fig = plt.figure(figsize=[0.72,0.72])\n    ax = fig.add_subplot(111)\n    ax.axes.get_xaxis().set_visible(False)\n    ax.axes.get_yaxis().set_visible(False)\n    ax.set_frame_on(False)\n    filename  = ath(audio_file_path).name.replace('.mp3','.png').replace('.mp2','.png').replace('.aac','.png').replace('.wav','.png')\n    S = librosa.feature.melspectrogram(y=audio, sr=sr)\n    librosa.display.specshow(librosa.power_to_db(S, ref=np.max))\n    fig.savefig(os.path.join(save_img_path, filename), dpi=None, bbox_inches='tight',pad_inches=0)\n    fig.clear()\n    plt.close()\n    fig.clf()\n\nI've tried adding .clf(), .close(), .clear() but still doesn't work. My notebook:\n\n[Audio to spectrum](https://www.kaggle.com/pawan28a95/simple-audio-to-spectrum-images?scriptVersionId=38073098)\n\nThanks",
      "votes": null
    },
    {
      "id": "916224",
      "postDate": "07/05/2020 13:37:59",
      "content": "<p>Very strange, I used torch audio to load audio data and it didn't had any memory issues, if you need you can take spectrograms from my notebook's output (The code is more or less the same, atleast for the spectrogram creation, so the output should be what you need): <a href=\"https://www.kaggle.com/pranavkasela/parallelized-spectrogram-with-torchaudio-librosa\">https://www.kaggle.com/pranavkasela/parallelized-spectrogram-with-torchaudio-librosa</a> </p>",
      "rawMarkdown": "Very strange, I used torch audio to load audio data and it didn't had any memory issues, if you need you can take spectrograms from my notebook's output (The code is more or less the same, atleast for the spectrogram creation, so the output should be what you need): https://www.kaggle.com/pranavkasela/parallelized-spectrogram-with-torchaudio-librosa",
      "votes": null
    },
    {
      "id": "920576",
      "postDate": "07/08/2020 17:21:42",
      "content": "<p>Good day!\nTry this approach:</p>\n\n<p>```\n%matplotlib</p>\n\n<h1>Using matplotlib backend: agg</h1>\n\n<p>import pandas as pd\nimport warnings\nwarnings.filterwarnings('ignore')</p>\n\n<p>import matplotlib.pyplot as plt\nimport seaborn as sns\nimport gc\nimport os</p>\n\n<p>def plot_line(df, i):\n    plt.figure()\n    sns_plot = sns.lineplot(x=\"time\", y=\"signal\", data=df)\n    sns_plot.axis(\"off\")\n    fig = sns_plot.get_figure()\n    fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)\n    fig.savefig('images/chart_' + str(i) + '.png', dpi=100)\n    plt.clf()\n    plt.close(fig)\n    del fig\n    del sns_plot\n    gc.collect()</p>\n\n<p>sns.set(rc={'figure.figsize':(19.2,10.8)})\nfor i in range(1001):\n    data = df_train[['time', 'signal']][(i - 1) * 1000 : i * 1000]\n    plot_line(data, i)\n```</p>\n\n<p>I used it at the <a href=\"https://www.kaggle.com/c/liverpool-ion-switching/overview\">University of Liverpool - Ion Switching</a> in order to generate 5k images.\nInitially, I also ran into the problem of running out of memory due to the fact that each graph was displayed in the output, but the magic line <strong>%matplotlib</strong> saved me :)</p>",
      "rawMarkdown": "Good day!\nTry this approach:\n\n```\n%matplotlib\n#Using matplotlib backend: agg\n\nimport pandas as pd\nimport warnings\nwarnings.filterwarnings('ignore')\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport gc\nimport os\n\ndef plot_line(df, i):\n    plt.figure()\n    sns_plot = sns.lineplot(x=\"time\", y=\"signal\", data=df)\n    sns_plot.axis(\"off\")\n    fig = sns_plot.get_figure()\n    fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)\n    fig.savefig('images/chart_' + str(i) + '.png', dpi=100)\n    plt.clf()\n    plt.close(fig)\n    del fig\n    del sns_plot\n    gc.collect()\n\nsns.set(rc={'figure.figsize':(19.2,10.8)})\nfor i in range(1001):\n    data = df_train[['time', 'signal']][(i - 1) * 1000 : i * 1000]\n    plot_line(data, i)\n```\n\nI used it at the [University of Liverpool - Ion Switching](https://www.kaggle.com/c/liverpool-ion-switching/overview) in order to generate 5k images.\nInitially, I also ran into the problem of running out of memory due to the fact that each graph was displayed in the output, but the magic line **%matplotlib** saved me :)",
      "votes": null
    },
    {
      "id": "923487",
      "postDate": "07/10/2020 22:55:38",
      "content": "<p>Are you running out of RAM or storage? I went into trouble while saving files because of the 5gb limit on kaggle kernels. The solution for me was to use more than one kernel for saving spectrums.</p>",
      "rawMarkdown": "Are you running out of RAM or storage? I went into trouble while saving files because of the 5gb limit on kaggle kernels. The solution for me was to use more than one kernel for saving spectrums.",
      "votes": null
    },
    {
      "id": "926001",
      "postDate": "07/12/2020 12:28:26",
      "content": "<p>Yes, my problem was with RAM.</p>",
      "rawMarkdown": "Yes, my problem was with RAM.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 916224,
      "author_name": "pranavkasela",
      "author_url": "",
      "post_date": "07/05/2020 13:37:59",
      "content": "<p>Very strange, I used torch audio to load audio data and it didn't had any memory issues, if you need you can take spectrograms from my notebook's output (The code is more or less the same, atleast for the spectrogram creation, so the output should be what you need): <a href=\"https://www.kaggle.com/pranavkasela/parallelized-spectrogram-with-torchaudio-librosa\">https://www.kaggle.com/pranavkasela/parallelized-spectrogram-with-torchaudio-librosa</a> </p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 920576,
      "author_name": "bitumok",
      "author_url": "",
      "post_date": "07/08/2020 17:21:42",
      "content": "<p>Good day!\nTry this approach:</p>\n\n<p>```\n%matplotlib</p>\n\n<h1>Using matplotlib backend: agg</h1>\n\n<p>import pandas as pd\nimport warnings\nwarnings.filterwarnings('ignore')</p>\n\n<p>import matplotlib.pyplot as plt\nimport seaborn as sns\nimport gc\nimport os</p>\n\n<p>def plot_line(df, i):\n    plt.figure()\n    sns_plot = sns.lineplot(x=\"time\", y=\"signal\", data=df)\n    sns_plot.axis(\"off\")\n    fig = sns_plot.get_figure()\n    fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)\n    fig.savefig('images/chart_' + str(i) + '.png', dpi=100)\n    plt.clf()\n    plt.close(fig)\n    del fig\n    del sns_plot\n    gc.collect()</p>\n\n<p>sns.set(rc={'figure.figsize':(19.2,10.8)})\nfor i in range(1001):\n    data = df_train[['time', 'signal']][(i - 1) * 1000 : i * 1000]\n    plot_line(data, i)\n```</p>\n\n<p>I used it at the <a href=\"https://www.kaggle.com/c/liverpool-ion-switching/overview\">University of Liverpool - Ion Switching</a> in order to generate 5k images.\nInitially, I also ran into the problem of running out of memory due to the fact that each graph was displayed in the output, but the magic line <strong>%matplotlib</strong> saved me :)</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 923487,
      "author_name": "adriel",
      "author_url": "",
      "post_date": "07/10/2020 22:55:38",
      "content": "<p>Are you running out of RAM or storage? I went into trouble while saving files because of the 5gb limit on kaggle kernels. The solution for me was to use more than one kernel for saving spectrums.</p>",
      "votes": null,
      "replies": [
        {
          "id": 926001,
          "author_name": "bitumok",
          "author_url": "",
          "post_date": "07/12/2020 12:28:26",
          "content": "<p>Yes, my problem was with RAM.</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "915117": "I'm trying to convert the audio into spectrograms using librosa library. But I'm running our of memory everytime. Code snippet:\n\ndef create_fold_spectrograms(audio_file_path,save_img_path ):\n    \n    audio, sr = librosa.load(audio_file_path, sr =None)\n    fig = plt.figure(figsize=[0.72,0.72])\n    ax = fig.add_subplot(111)\n    ax.axes.get_xaxis().set_visible(False)\n    ax.axes.get_yaxis().set_visible(False)\n    ax.set_frame_on(False)\n    filename  = ath(audio_file_path).name.replace('.mp3','.png').replace('.mp2','.png').replace('.aac','.png').replace('.wav','.png')\n    S = librosa.feature.melspectrogram(y=audio, sr=sr)\n    librosa.display.specshow(librosa.power_to_db(S, ref=np.max))\n    fig.savefig(os.path.join(save_img_path, filename), dpi=None, bbox_inches='tight',pad_inches=0)\n    fig.clear()\n    plt.close()\n    fig.clf()\n\nI've tried adding .clf(), .close(), .clear() but still doesn't work. My notebook:\n\n[Audio to spectrum](https://www.kaggle.com/pawan28a95/simple-audio-to-spectrum-images?scriptVersionId=38073098)\n\nThanks",
    "916224": "Very strange, I used torch audio to load audio data and it didn't had any memory issues, if you need you can take spectrograms from my notebook's output (The code is more or less the same, atleast for the spectrogram creation, so the output should be what you need): https://www.kaggle.com/pranavkasela/parallelized-spectrogram-with-torchaudio-librosa",
    "920576": "Good day!\nTry this approach:\n\n```\n%matplotlib\n#Using matplotlib backend: agg\n\nimport pandas as pd\nimport warnings\nwarnings.filterwarnings('ignore')\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport gc\nimport os\n\ndef plot_line(df, i):\n    plt.figure()\n    sns_plot = sns.lineplot(x=\"time\", y=\"signal\", data=df)\n    sns_plot.axis(\"off\")\n    fig = sns_plot.get_figure()\n    fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)\n    fig.savefig('images/chart_' + str(i) + '.png', dpi=100)\n    plt.clf()\n    plt.close(fig)\n    del fig\n    del sns_plot\n    gc.collect()\n\nsns.set(rc={'figure.figsize':(19.2,10.8)})\nfor i in range(1001):\n    data = df_train[['time', 'signal']][(i - 1) * 1000 : i * 1000]\n    plot_line(data, i)\n```\n\nI used it at the [University of Liverpool - Ion Switching](https://www.kaggle.com/c/liverpool-ion-switching/overview) in order to generate 5k images.\nInitially, I also ran into the problem of running out of memory due to the fact that each graph was displayed in the output, but the magic line **%matplotlib** saved me :)",
    "923487": "Are you running out of RAM or storage? I went into trouble while saving files because of the 5gb limit on kaggle kernels. The solution for me was to use more than one kernel for saving spectrums.",
    "926001": "Yes, my problem was with RAM."
  },
  "source": "meta"
}