Skip to content
Snippets Groups Projects
Commit 2ce58855 authored by Guillaume Allegre's avatar Guillaume Allegre
Browse files

Update Conversion IRMAS/model_cnn_Irmas.ipynb, Conversion IRMAS/conversion_mel_IRMAS files

parents
Branches
No related tags found
No related merge requests found
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"conversion_mel_IRMAS","provenance":[],"collapsed_sections":[],"toc_visible":true,"mount_file_id":"1obbS6-vX1z--9i1mMt84W34a1OqQO8Dw","authorship_tag":"ABX9TyPpHeWmgS16jAN/ZFzmeKZ4"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","metadata":{"id":"zQQC4ApGdNxA","executionInfo":{"status":"ok","timestamp":1618244287875,"user_tz":-120,"elapsed":828,"user":{"displayName":"guillaume ALLEGRE","photoUrl":"","userId":"15788823947103069938"}}},"source":["import scipy\n","import os\n","from scipy.io import wavfile\n","from scipy import signal\n","import matplotlib.pyplot as plt\n","import numpy as np\n","import os\n","import librosa\n","import librosa.display\n","\n","\n","import librosa\n","import numpy\n","\n","\n","\n","# settings\n","hop_length = 512 # number of samples per time-step in spectrogram\n","n_mels = 256 # number of bins in spectrogram. Height of image\n","time_steps = 256 # number of time-steps. Width of image\n"],"execution_count":31,"outputs":[]},{"cell_type":"code","metadata":{"id":"2Y62suo5gFCc","executionInfo":{"status":"ok","timestamp":1618244288081,"user_tz":-120,"elapsed":431,"user":{"displayName":"guillaume ALLEGRE","photoUrl":"","userId":"15788823947103069938"}}},"source":["data_path = './drive/MyDrive/Automatant-Musique/Data_IRMAS/IRMAS-TrainingData'\n","data_converted_path = './drive/MyDrive/Automatant-Musique/Data_converted_train_IRMAS'\n","lst_instru = ['org','cel','cla','sax','flu','gac','gel','pia','tru','vio','voi']\n","\n","train_img = []\n","train_label = []\n","\n","\n","LIMIT = False\n","num_limit = 10 # max is 100"],"execution_count":32,"outputs":[]},{"cell_type":"code","metadata":{"id":"jz5Z5PUCvhdA","executionInfo":{"status":"ok","timestamp":1618244288903,"user_tz":-120,"elapsed":541,"user":{"displayName":"guillaume ALLEGRE","photoUrl":"","userId":"15788823947103069938"}}},"source":["def index(intru):\n","\n"," for i in range(len(lst_instru)):\n"," if instru == lst_instru[i]:\n"," return i\n","\n","def scale_minmax(X, min=0.0, max=1.0):\n"," X_std = (X - X.min()) / (X.max() - X.min())\n"," X_scaled = X_std * (max - min) + min\n"," return X_scaled\n","\n","def spectrogram_image(y, sr, out, hop_length, n_mels):\n"," # use log-melspectrogram\n"," mels = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=n_mels,\n"," n_fft=hop_length*2, hop_length=hop_length)\n"," mels = numpy.log(mels + 1e-9) # add small number to avoid log(0)\n","\n"," # min-max scale to fit inside 8-bit range\n"," img = scale_minmax(mels, 0, 255).astype(numpy.uint8)\n"," img =np.expand_dims(img, axis=2)\n","\n"," train_img.append(img)\n"," train_label.append(index(instru))\n"," \n","\n","def save_img(data_path, instru, wavfile):\n","\n"," x_path = './drive/MyDrive/Automatant-Musique/Data_IRMAS/IRMAS-TrainingData' + '/' + instru + '/' + wavfile\n"," x_save_path = './drive/MyDrive/Automatant-Musique/Data_converted_train_IRMAS' + '/' + instru + '/' + wavfile[:-3] + \"png\"\n","\n"," y, sr = librosa.load(x_path, offset=1.0, duration=2.0)\n","\n"," # extract a fixed length window\n"," start_sample = 0 # starting at beginning\n"," length_samples = time_steps*hop_length\n"," window = y\n","\n"," \n"," # convert to PNG\n"," spectrogram_image(window, sr=sr, out=x_save_path, hop_length=hop_length, n_mels=n_mels)\n"],"execution_count":33,"outputs":[]},{"cell_type":"code","metadata":{"id":"soaiQoJbtbSQ","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1618245214678,"user_tz":-120,"elapsed":924867,"user":{"displayName":"guillaume ALLEGRE","photoUrl":"","userId":"15788823947103069938"}},"outputId":"5ca52630-1cc3-41fa-b70d-c654737612d5"},"source":["\n","for instru in os.listdir(data_path):\n"," print(instru)\n"," for wavfile in os.listdir(data_path + '/' + instru):\n"," if wavfile[-3:] != 'wav':\n"," continue\n"," \n"," save_img(data_converted_path, instru, wavfile)\n","\n"],"execution_count":34,"outputs":[{"output_type":"stream","text":["cla\n","org\n","vio\n","pia\n","sax\n","cel\n","gac\n","flu\n","gel\n","tru\n","voi\n",".ipynb_checkpoints\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"VAIlPGE9ZbDZ","executionInfo":{"status":"ok","timestamp":1618245726173,"user_tz":-120,"elapsed":2121,"user":{"displayName":"guillaume ALLEGRE","photoUrl":"","userId":"15788823947103069938"}},"outputId":"86517ad6-d98b-4402-8402-469e064a95ea"},"source":["train_img = np.array(train_img)\n","print(np.shape(train_img))\n","np.save('./drive/MyDrive/Automatant-Musique/Data_converted_train_IRMAS/train_images.npy', train_img)\n","np.save('./drive/MyDrive/Automatant-Musique/Data_converted_train_IRMAS/train_labels.npy', train_label)"],"execution_count":35,"outputs":[{"output_type":"stream","text":["(6705, 256, 87, 1)\n"],"name":"stdout"}]}]}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment