commit f0aa7e45805dcef769ede97a9e38bbd5083f163f
parent 608d2e4d0f38082f61afe741995607529e54cca7
Author: keith <kbloemer89@gmail.com>
Date: Sun, 6 Dec 2020 14:23:24 -0600
Updated Colab notebook with split_data argument
Diffstat:
1 file changed, 69 insertions(+), 35 deletions(-)
diff --git a/guitar_lstm_colab.ipynb b/guitar_lstm_colab.ipynb
@@ -24,7 +24,8 @@
"# 1. Upload your input and output wav files to the current directory in Colab\n",
"# 2. Edit the USER INPUTS section to point to your wav files, and choose a\n",
"# model name, and number of epochs for training. If you experience \n",
- "# crashing due to low RAM, reduce the \"input_size\" parameter.\n",
+ "# crashing due to low RAM, reduce the \"input_size\" parameter, or increase\n",
+ "# the \"split_data\" parameter.\n",
"# 3. Run each section of code. The trained models and output wav files will be \n",
"# added to the \"models\" directory.\n",
"#\n",
@@ -49,7 +50,7 @@
"import h5py\n",
"\n"
],
- "execution_count": null,
+ "execution_count": 1,
"outputs": []
},
{
@@ -64,14 +65,14 @@
"in_file = 'ts9_test1_in_FP32.wav'\n",
"out_file = 'ts9_test1_out_FP32.wav'\n",
"epochs = 1\n",
- "\n",
+ "split_data=4 # **Increase this to reduce RAM usage **\n",
"\n",
"train_mode = 0 # 0 = speed training, \n",
" # 1 = accuracy training \n",
" # 2 = extended training\n",
"\n",
- "input_size = 75 # !!!IMPORTANT !!!: The input_size is set at 75 for Colab notebook. \n",
- " # a higher setting may result in crashing due to\n",
+ "input_size = 150 # !!!IMPORTANT !!!: The input_size is set at 150 for Colab notebook. \n",
+ " # A higher setting may result in crashing due to\n",
" # memory limitation of 8GB for the free version\n",
" # of Colab. This setting limits the accuracy of\n",
" # the training, especially for complex guitar signals\n",
@@ -80,6 +81,9 @@
" # !!!IMPORTANT!!!: You will most likely need to cycle the runtime to \n",
" # free up RAM between training sessions.\n",
" #\n",
+ " # Increase the \"split_data\" parameter to reduce the RAM used and\n",
+ " # still allow for a higher \"input_size\" setting. \n",
+ " #\n",
" # Future dev note: Using a custom dataloader may be a good\n",
" # workaround for this limitation, at the cost\n",
" # of slower training.\n",
@@ -159,24 +163,6 @@
" hidden_units= 96\n",
"\n",
"\n",
- "# Load and Preprocess Data ###########################################\n",
- "in_rate, in_data = wavfile.read(in_file)\n",
- "out_rate, out_data = wavfile.read(out_file)\n",
- "\n",
- "X = in_data.astype(np.float32).flatten() \n",
- "X = normalize(X).reshape(len(X),1) \n",
- "y = out_data.astype(np.float32).flatten() \n",
- "y = normalize(y).reshape(len(y),1) \n",
- "\n",
- "y_ordered = y[input_size-1:] \n",
- "\n",
- "indices = np.arange(input_size) + np.arange(len(X)-input_size+1)[:,np.newaxis] \n",
- "X_ordered = tf.gather(X,indices) \n",
- "\n",
- "shuffled_indices = np.random.permutation(len(X_ordered)) \n",
- "X_random = tf.gather(X_ordered,shuffled_indices)\n",
- "y_random = tf.gather(y_ordered, shuffled_indices)\n",
- "\n",
"# Create Sequential Model ###########################################\n",
"clear_session()\n",
"model = Sequential()\n",
@@ -187,27 +173,75 @@
"model.compile(optimizer=Adam(learning_rate=learning_rate), loss=error_to_signal, metrics=[error_to_signal])\n",
"print(model.summary())\n",
"\n",
- "# Train Model ###################################################\n",
- "model.fit(X_random,y_random, epochs=epochs, batch_size=batch_size, validation_split=test_size) \n",
+ "# Load and Preprocess Data ###########################################\n",
+ "in_rate, in_data = wavfile.read(in_file)\n",
+ "out_rate, out_data = wavfile.read(out_file)\n",
+ "\n",
+ "X_all = in_data.astype(np.float32).flatten() \n",
+ "X_all = normalize(X_all).reshape(len(X_all),1) \n",
+ "y_all = out_data.astype(np.float32).flatten() \n",
+ "y_all = normalize(y_all).reshape(len(y_all),1) \n",
+ "\n",
+ "# If splitting the data for training, do this part\n",
+ "if split_data > 1:\n",
+ " num_split = len(X_all) // split_data\n",
+ " X = X_all[0:num_split*split_data]\n",
+ " y = y_all[0:num_split*split_data]\n",
+ " X_data = np.split(X, split_data)\n",
+ " y_data = np.split(y, split_data)\n",
+ "\n",
+ " # Perform training on each split dataset\n",
+ " for i in range(len(X_data)):\n",
+ " print(\"\\nTraining on split data \" + str(i+1) + \"/\" +str(len(X_data)))\n",
+ " X_split = X_data[i]\n",
+ " y_split = y_data[i]\n",
+ "\n",
+ " y_ordered = y_split[input_size-1:] \n",
+ "\n",
+ " indices = np.arange(input_size) + np.arange(len(X_split)-input_size+1)[:,np.newaxis] \n",
+ " X_ordered = tf.gather(X_split,indices) \n",
+ "\n",
+ " shuffled_indices = np.random.permutation(len(X_ordered)) \n",
+ " X_random = tf.gather(X_ordered,shuffled_indices)\n",
+ " y_random = tf.gather(y_ordered, shuffled_indices)\n",
+ "\n",
+ " # Train Model ###################################################\n",
+ " model.fit(X_random,y_random, epochs=epochs, batch_size=batch_size, validation_split=0.2) \n",
"\n",
- "model.save('models/'+name+'/'+name+'.h5')\n",
"\n",
- "#model.save('model_data/')\n",
- "#model = load_model('new_model_'+name+'.h5', custom_objects={'error_to_signal' : error_to_signal})\n",
- "#learning_rate = 0.005\n",
- "#model.compile(optimizer=Adam(learning_rate=learning_rate), loss=error_to_signal, metrics=[error_to_signal])\n",
+ " model.save('models/'+name+'/'+name+'.h5')\n",
+ "\n",
+ "# If training on the full set of input data in one run, do this part\n",
+ "else:\n",
+ " y_ordered = y_all[input_size-1:] \n",
+ "\n",
+ " indices = np.arange(input_size) + np.arange(len(X_all)-input_size+1)[:,np.newaxis] \n",
+ " X_ordered = tf.gather(X_all,indices) \n",
+ "\n",
+ " shuffled_indices = np.random.permutation(len(X_ordered)) \n",
+ " X_random = tf.gather(X_ordered,shuffled_indices)\n",
+ " y_random = tf.gather(y_ordered, shuffled_indices)\n",
+ "\n",
+ " # Train Model ###################################################\n",
+ " model.fit(X_random,y_random, epochs=epochs, batch_size=batch_size, validation_split=test_size) \n",
+ "\n",
+ " model.save('models/'+name+'/'+name+'.h5')\n",
"\n",
"# Run Prediction #################################################\n",
"print(\"Running prediction..\")\n",
- "y_the_rest, y_last_part = np.split(y_ordered, [int(len(y_ordered)*.8)])\n",
- "x_the_rest, x_last_part = np.split(X, [int(len(X)*.8)])\n",
"\n",
- "x_the_rest, x_ordered_last_part = np.split(X_ordered, [int(len(X_ordered)*.8)])\n",
- "prediction = model.predict(x_ordered_last_part, batch_size=batch_size)\n",
+ "# Get the last 20% of the wav data to run prediction and plot results\n",
+ "y_the_rest, y_last_part = np.split(y_all, [int(len(y_all)*.8)])\n",
+ "x_the_rest, x_last_part = np.split(X_all, [int(len(X_all)*.8)])\n",
+ "y_test = y_last_part[input_size-1:] \n",
+ "indices = np.arange(input_size) + np.arange(len(x_last_part)-input_size+1)[:,np.newaxis] \n",
+ "X_test = tf.gather(x_last_part,indices) \n",
+ "\n",
+ "prediction = model.predict(X_test, batch_size=batch_size)\n",
"\n",
"save_wav('models/'+name+'/y_pred.wav', prediction)\n",
"save_wav('models/'+name+'/x_test.wav', x_last_part)\n",
- "save_wav('models/'+name+'/y_test.wav', y_last_part)\n",
+ "save_wav('models/'+name+'/y_test.wav', y_test)\n",
"\n",
"# Add additional data to the saved model (like input_size)\n",
"filename = 'models/'+name+'/'+name+'.h5'\n",