Last active
October 24, 2022 06:44
-
-
Save marty1885/4b4a0cfa1bec0eb5fc99cbafcfb00716 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define CNN_USE_AVX //Enable the AVX backend for faster computing | |
#include "tiny_dnn/tiny_dnn.h" | |
using namespace tiny_dnn; | |
using namespace tiny_dnn::activation; | |
using namespace tiny_dnn::layers; | |
#include "AudioFile.h" | |
#include <vector> | |
#include <random> | |
using namespace std; | |
int main() | |
{ | |
//Some traning variable | |
const int SAMPLE_NUM = 1024; //How much traning samples | |
const int WINDOW_SIZE = 2048; //How long is each sample | |
const int BATCH_SIZE = 16; //How many samples per batch | |
const int NUM_EPOCHS = 2048; //How much epoch we want to run | |
//Create a sample buffer | |
vector<vec_t> samples(SAMPLE_NUM, vec_t(WINDOW_SIZE)); | |
AudioFile<float> audioFile; | |
audioFile.load("a.wav"); //Load audio | |
auto& data = audioFile.samples[0]; //Just use the first channel | |
//Create a RNG and a distribution to generate random numbers | |
mt19937 rng; | |
uniform_int_distribution<int> dist(0, data.size()-WINDOW_SIZE); | |
//Generate samples | |
for(auto& v : samples) | |
{ | |
int offset = dist(rng);//Generate random offsets | |
auto start = data.begin() + offset; | |
//Copy data from source to sample | |
copy(start, start+WINDOW_SIZE, v.begin()); | |
} | |
//Create an autoencoder | |
network<sequential> net; | |
net << fully_connected_layer(WINDOW_SIZE, 512) << tanh_layer() | |
<< fully_connected_layer(512, WINDOW_SIZE); | |
//Helper class | |
tiny_dnn::progress_display disp(SAMPLE_NUM); | |
tiny_dnn::timer t; | |
int currentEpoch = 0; | |
//Callbacks when a mini bactch is done | |
auto onMinibatch = [&]() | |
{ | |
//This updates the progress display | |
disp += BATCH_SIZE; | |
}; | |
//Callbacks when an epoch is done | |
auto onEpoch = [&]() | |
{ | |
std::cout << "Epoch " << ++currentEpoch << "/" << NUM_EPOCHS << "done. " | |
<< t.elapsed() << "s elapsed." << std::endl; | |
//Reset progress display and timer | |
disp.restart(SAMPLE_NUM); | |
t.restart(); | |
}; | |
//train the network with absolute(L1) error. | |
adagrad optimizer; | |
net.fit<absolute>(optimizer, samples, samples, BATCH_SIZE, NUM_EPOCHS | |
, onMinibatch, onEpoch); | |
net.save("net"); | |
//Let's try the network | |
vector<float> result(data.size()); | |
for(int i=0;i<data.size();i+=WINDOW_SIZE) | |
{ | |
//Input to the neural network | |
vec_t input(WINDOW_SIZE); | |
//copy data into the input vector | |
copy(data.begin()+i, data.begin()+i+WINDOW_SIZE, input.begin()); | |
//Run the neural network then copy it to the result buffer | |
vec_t predict = net.predict(input); | |
copy(result.begin()+i, result.begin()+i+WINDOW_SIZE, predict.begin()); | |
} | |
//Save the audio we ganarated | |
AudioFile<float> saveFile; | |
AudioFile<float>::AudioBuffer buffer(1); | |
buffer[0] = result; | |
audioFile.setAudioBuffer(buffer); | |
audioFile.save("audioFile2.wav"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment