Hello, and welcome to the fourth episode of AWS DeepComposer:
Train it again, Maestro. Today,
I'm going to show you how to build a custom GAN architecture and train your own
music genre model using Amazon Sagemaker.
We will then use our custom genre model to generate a brand new song.
And as a sneak peek,
I'm going to play you the track that we're going to develop.
And you'll find that this track sounds much better than the first one we created
because we're training a custom genre model using our own midi files.
This lesson is hardcore machine learning.
We're going to use the Python, programming language and data science libraries,
Amazon Sagemaker, and Jupyter notebooks.
I will give you an overview of Sagemaker,
but if you're not familiar with Python or Jupyter notebooks,
you're in luck because we have those courses on our platform.
So feel free to check them out. Now I'm going to play for you.
What we're going to develop during this lab.
It's a reggae inspired AI track. [Song plays]
I really like how this turned out. So let's get started.
When training a model, the first thing you need is reputable data. In our case,
we'll new royalty, free music data that is already in midi formats.
I've found several resources on the internet. Some are paid and some are free.
For the Jupyter notebook,
I've included one of the free midi files I found so that we can run the code,
but you will need to source your own files.
I've included links to some of the free sites I found in the resources section.
There are multiple training options for our custom genre model.
The simplest way to get started with training a custom model is through the
built-in wizard. You get to that wizard by clicking on models,
and then click create a model.
This wizard allows you to train a new model by choosing one of the supplied
training datasets. So symphony jazz pop or rock,
and it also gives you two GAN architectures to choose from
this wizard does limit you to the existing music genres,
and it will not let you train a custom genre model using your own dataset.
So it's very limiting.
The only reason you would custom train a model in those genres is if you need
slightly more control over setting the hyper parameters.
There are two GAN architectures based on the convolutional neural networks
provided here in the AWS deep composer console,
muse GAN and u-net.
Muse GAN is a bit more complex than u-net and it's purposely built for music
generation.
It's an official TensorFlow implementation that uses GANs to generate
multi-track music compositions. On the other hand,
u-net was initially created for image translation
and it's mainly used for computer vision,
but just happens to work well for music because music can be
represented as a two dimensional matrix.
Just like an image can look at this image.
You'll see that time is on the horizontal axis and pitch on the vertical axis.
This looks a lot like an image, doesn't it? Let's click muse GAN.
And let's scroll down to the hyper parameters
when using one of these GAN architectures to train your model,
you'll see the normal training hyper-parameters and hyper parameters are
configuration settings for the job.
The training process does require a lot of experimentation with these parameters
to find the optimal combination.
The first hyper parameter is called an epoch, or if you're from London,
epoch, I like how that sounds - epoch.
Now an epoch represents a single pass the machine makes over the training
data.
This parameter in the console allows you to define the amount of passes the
machine will make during the training process.
It's important for the machine to make multiple passes over the data,
trying to learn and find trends and patterns.
The default value here is 140, that can be changed.
At the end of each epoch, a model is produced and evaluated,
and most times thrown away until an optimal performing model is reached.
Or the number of epochs has been reached after each epoch or iteration.
The models produced become smarter and smarter.
So decreasing the number of epochs will decrease the training time and
your overall AWS bill,
but it could also decrease the accuracy of the final model.
So just keep that in mind. The next hyper parameter is learning rate.
The learning rate is sometimes called the step size and it dictates how much to
change the model in response to errors found during each
epoch. Remember I mentioned that each model produced is evaluated.
So errors, called loss, errors are uncovered,
and that information is fed back into the process that produces the next
model, which should be a smarter model.
That feedback comes in the form of adjusting the weights or
the ranked order of importance of each input value to the neural network.
In our case, our features like pitch time, et cetera. And it's based on loss.
Typically weights are random at first,
then changed after each epoch or iteration to correct errors.
The learning rate dictates how much these weights are adjusted.
The default value is 0.001,
a higher learning rate gives the network the ability to explore a wider
range of weights. So keep that in mind. Lastly,
we have update ratio. With the GAN,
remember I told you there are two networks,
both going through the training process and they are sometimes in a tug
of war. Remember,
there's the generator that produces the copycat or the fake music.
And then it's the discriminator's job to determine whether or not what was
produced is real or fake.
There are multiple iterations or epochs of this tug of war.
The update ratio here attempts to balance out the tug of war
between the discriminator and the generator.
The default value is five. The range is between one and five.
Five means there are five iterations of the discriminator
before the generator is updated. Think about it like this,
a better performing or smarter discriminator can provide
more meaningful feedback to the generator,
but this does increase training time and your AWS bill on the
screen, you give your model a name. You can give it a description,
and then you can click this button to start training.
This note here models usually take eight hours to train given the preexisting
data sets, but it can vary depending on the hyper parameters selected.
I just wanted to give you a disclaimer on the cost to train a custom model in
Sagemaker.
The cost of using some of the more powerful instances is significant.
My account is under the free tier option.
And if your account is under the free tier option,
let's take a look at what's included in that free tier.
You have 250 hours per month of T2 medium notebook
usage for the first two months.
You also have 50 hours per month of M4 extra large for
training and you have 125 hours per month of M4
extra large for hosting. So keep this in mind.
When you start to select the instance type that you're using for
training or for hosting or for your notebook.
That's a look at the process to train the custom genre model using one of the
preexisting datasets. Again, since this is just too limiting,
because I want to generate reggae music,
we're going to use Amazon Sagemaker to build our own reggae genre model
completely from scratch using our own reggae music data training set.
This process is simply teaching the machine to compose music of a given genre
by developing a practical understanding of the distribution of musical
instruments and reggae music.
I love reggae and I am a huge Bob Marley fan.
His song, One Love is one of my favorites.
So that song will be my input melody.
If you're not familiar with reggae is from an Island in the Caribbean sea called
Jamaica. Jamaica is my favorite vacation spot.
Since Sagemaker is what makes this custom training possible.
Let me give you a high level overview of the service.
Sagemaker is a fully managed machine learning service that provides an easy to
use interface to walk through the machine learning life cycle.
All of these steps fall under one umbrella.
Think of Sagemaker as machine learning as a service.
The first step is to create a Jupyter notebook and we'll create that
through the Sagemaker console.
And if you're not familiar with Jupyter notebooks,
we have an introduction to Jupyter notebooks in our course library that you can
watch.
So first click on notebook instances and then click
create notebook instance and give your notebook a name,
say deep composer notebook, A cloud guru.
And this is the notebook instance type that I was telling you about.
Be mindful of the different types that fall within the free tier.
Now, AWS,
for this custom training recommends using a C5
4 extra large,
here it is C5 4 extra large.
By default your account will probably not have access to this notebook
instance type.
And this is just a protective measure put in place by AWS so that you don't end
up with a surprisingly high Sagemaker bill in order to
access an instance type that you don't have access to.
You actually have to open a support ticket with AWS support to increase your
limit.
I do want to show you the error message that you'll see if you don't have
access to an instance type.
So I'm just going to scroll down and pick this one is pretty large and I should
not have access to that.
And I'm just going to leave everything as is for now and click create notebook
and see, this is the error that you'll see. It says resource limit exceeded,
and it just tells you, you need to contact AWS support here. Again,
I recommend if your account is on the free tier to use one of the free tier
instances for this training, I am going to use what Amazon recommends,
and it does have a slightly higher cost because it is not a part of
the free tier.
Underneath permissions and encryption. Click on create a new role.
Look at these permissions here.
So basically S3 bucket,
any S3 bucket or any S3 bucket with Sagemaker in the name,
any S3 object with Sagemaker in the name, et cetera,
I'm fine with all of these default. So I'm creating a new role.
Everything else we're going to leave for the default,
except the git repositories. So click that.
I have created a Jupyter notebook for you and you can find it in
the AWS DeepComposer: Train it again, Maestro github.
And this is the link.
All you need to do is copy that link to gain access to the
Jupyter notebook with all of the source code. So copy that link.
Go back to Amazon Sagemaker,
under default repository,
click clone a public git repository to this notebook instance only.
Paste that URL and then click create notebook instance.
It will bring you to a screen like this,
and it will take time to create this notebook.
So I'm going to pause the video and I'll come back once it's created.
The notebook instance was successfully created and it shows in service.
Click on open Jupyter, and it brings you to a screen like this.
Look for the notebook and click on it. Now,
when you look at this notebook,
it contains all of the code to create a custom GAN model from
scratch. There are a lot of Python cells here to go through.
So if you'd like to deep dive into the code and follow along to build your own
custom GAN model from scratch, check out the bonus content video,
linked in the resources.
So now that we've generated a song based on our dataset,
I'm going to show you how you can make the track sound even more unique and
personalized. I'm using a Mac.
So I'm going to play you the melody using garage band. With garage band,
I've found the sound is much better than trying to listen to what's produced
within that Jupyter notebook. If you don't use a Mac,
you can use any other digital audio workstation that supports midi files.
Using this kind of software will really improve the sound quality when listening
to your AI generated music,
and you can even attach pro-grade USB speakers to amplify the
sound. And that's what I did. So let's play my melody.
This is the melody I played on the AWS deep composer keyboard.
And it's inspired by Bob Marley.
[Song plays].
Okay. So that's the melody.
So this is what was produced at iteration 150 and I'm going to play it.
[Song plays].
Okay. And you can see the accompany instruments here.
And there are a few things that we can tweak using garage band to make it even
better. We can cut out bits of the track. We can play the track on a loop.
We're able to change the accompanying instruments to whatever we think sounds
best for the track. Up here, we can adjust the tempo.
We can adjust the volume of individual instruments,
and when we're happy with how everything sounds, we can export to disc,
save it as an MP3 and upload it to SoundCloud.
Now don't forget to clean up your notebook.
Don't forget to shut it down.
Do not leave any of the notebook instances running as they have hourly
charges. And you don't want to amass a huge bill,
even on the free tier.
I really like how this turned out. Well, that's it.
For this episode you were introduced to GAN architectures like u-net and muse
GAN.
You also learned about the training process and important hyper parameters like
epochs, learning rate and update ratio.
You were even introduced to Amazon Sagemaker and Jupyter notebooks to train your
custom model. Great job.
Join me in the next episode where we'll learn to evaluate how well your model is
performing. See you soon.