How to make an image classifier without coding. Part 3
Now that we have a clean dataset of images, we can train our image classifier. For this, we will be using an amazing new service from Google Cloud, called AutoML Vision. Please notice that by the time this article is written, this service is on Beta version. This means that the product can change without warning and should be used for tests and not for applications in production.
Google Cloud AutoML Vision is an automated and fully managed machine learning Platform as a Service that was very recently released. To our knowledge, it is currently the only service of its kind and we at the Machine Learning Lab were very eager to test it since it was first announced at the beginning of this year!!
AutoML Vision allows to train a machine learning model so that it can do image classification. It is very simple to use and it requires absolutely no coding knowledge. We followed the google official tutorials but we thought they were a little bit confusing for non developers, so we decided to write our own.
Google Cloud AutoMl Vision Tutorial
The first thing you’ll need is to create a google cloud account.
Click here and login with your gmail account (if you have one) or create a new account. Google offers $300 in credit so that you can play around with their products and apis. You can activate this credit if you want.
You will need to accept the terms and conditions
And you will need to fill your contact information and provide a valid credit card to continue. Your credit card will only be billed if you spend more than the $300 credit. The rest of this tutorial assumes you are creating an account as an individual. If you create an account as a company, some features and options could probably be different
Notice that you will probably be contacted by a sales representative from Google in Ireland (if you are located in Europe). Their team is fantastic and they can answer all your questions about Google cloud. Tell them Camilo Rodriguez from the Machine Learning Lab said hi!!
If everything went ok and your credit card was accepted, you should see a welcome screen and your google cloud console dashboard.
Now, before you start, you need to create a new project. Click here
Go ahead and create a new project.
Go ahead and give it a name. You can only use letter and numbers. I decided to name my project "can-i-hug-it", since it is representative of what I want to achieve.
Your new project should now be visible on the ressource manager dashboard. If it is not, wait a few moments and refresh your browser.
Now you need to enable billing for your project. Normally, this step is done automatically if you have only one billing account and you are the billing administrator, but just check to make sure.
Remember you will only be charged if you go over the $300 free credit.
For this, click on the right menu of the project. Click on "billing".
Your project should appear here, if not, go to this Google page to read instruction on how to enable billing for your projects.
Remember that at any point in time, you can disable billing for your project, but it will probably break it and you will not be able to use the features again until you enable billing again.
If everything is ok, click on the upper left big button "Google Cloud Platform" to go back to your dashboard.
Now, you will need to make sure a couple of APIs are activated in order to build your image classifier.
Normally, these APIs are already activated when you created the project, but just to be sure they are, click on "Go to APIs overview".
It's possible you get a message saying you don't have permission to view services enabled on this project, even though you are the administrator.
This is definetely a bug from Google Cloud on Internet explorer but don't pay much attention to it. I switched my browser to Chrome and suddenly it worked.
Normally, the three APIs you need are already enabled like in the above screenshot. But if this is not the case, you can enable them by clicking on the button "enable APIs and services"
You should see an API library. Type the names of these APIs one by one.
The library will auto complete the names.
- Google Cloud storage
- Google AutoML
- Google Cloud storage json
Make sure the three APIs mentioned above are activated or activate them now.
Now that you are ready, you can finally start building your image classifier.
But first, you will need to upload your images to google cloud. Go back to the dashboard and click on "storage" and then on "browser"
You will need to create a "bucket". A Bucket is simply a folder on Google cloud where you can store files and documents. Think of it like dropbox. Click on "create bucket"
Give it a name (it must be unique), select your region and leave all the other options untouched. Click on "create"
Now that you have your bucket, you can upload the images you downloaded to your computer in the previous article of this tutorial. I recommend you create two folders. One for the "hug" dataset and one for the "not_hug" dataset.
Now simply go to the "hug" folder and upload your "hug" images by clicking on "upload files".
You can upload multiple images at the same time, by clicking CTRL+E in your explorer and then clicking on "open".
If you have 1000 images on your "hug" dataset, depending on your internet connection, this process can take approximately 5 to 20 minutes. You will see an image on the bottom of your browser showing the process of the operation.
If for any reason, the operation is interrupted, don't worry about it. Go back to the bucket folder and check the name of the file that was imported the last, lucky for you, they all have numbers from 1 to 1000. Continue from that point.
When you are done uploading the images to the "hug" folder, repeat the same process again on the "not_hug" folder with the "not_hug" images.
And go grab a cup of coffee while you are waiting for the images to download.
Great work!! your dataset is now on the cloud.
Now that your images are uploaded to Google Cloud, you need to train your model. For this, you will need to access AutoML. Google has a nice user interface that you can access by clicking here.
You will need to provide AutoML with permissions, before using it.
Select your project and click on "continue".
You will probably be asked again to enable billing and activate the APIs. Just click on the fist button. It will open a new tab in your browser that you can later close.
If you click on the second button you will be able to continue.
Create a new dataset, give it a name and check the option that says that you will import the images later.
This user interface from Google is still in Beta and has some glitches. For instance, it automatically created a second "bucket" in Google cloud storage, different than the one we used before and tied the classifier project to that new bucket. Don't worry about it, we will transfer the images from one bucket to the other and you will feel like an expert devops engineer :)
Open a new tab and go the dashboard on the storage menu, click on the "transfer" button.
Now click on "create transfer"
Now you have to specify that you want to move the files from your previous bucket to the newly created bucket. So you have to browse the source and the destination.
Also make sure to delete the data from the source once it is transfered to its destination (to avoid duplicates) and to run the task now.
In my case, the operation took less than 45 seconds. That is pretty impressive, as Google was moving 2000 files from a datacenter in Europe to a datacenter in the USA. Pretty quick if you ask me!
If you want to make sure the operation was succesful, simply go and browse the new bucket. You will see the two folders with the images there.
Ok, your images are in the right bucket, but the model still does not know they are there. Now you will need to create a .csv file where you will specify the location of each image, along with the label it has.
Open Microsoft excel or even better, Google sheets and create a new document.
Leave that document open for now and go to the folder on your computer where you have the "hug" images.
Make sure you are seeing the files as a list and not their thumbnails.
Now select all the files with CTRL+E, press SHIFT and click on the right button of your mouse. You should see an option "copy as path" like below.
Go back to your excel or sheets file and paste the names of your files.
You should have 1000 rows on your file and if you do the same for the "not_hug" files you will end up with 2000 rows on your excel file.
Now you need to rename the paths of the images on your computer with the path from your cloud bucket.
Your bucket path starts with "gs://" and then has the bucket name and then the folder name separated by "/"
in my specific case, the path where the "hug" images are is like this:
So in the excel file, type CTRL+F and then "more options".
Find and replace all the local paths from your computer with the bucket paths in the cloud.
The goal is to replace all rows like this:
into something like this:
which is the location of the file in the cloud.
By now, your excel file should look like this:
Now, write the name of the labels on the second column and copy it to all the rows. In my example, the label of all these images is "hug"
Now, in the same file just after the last record, repeat the same process with the "not_hug" images. Remember they are on a different path, both in your computer and in the Google bucket. Also assign them all the label "not_hug".
By now, if you did everything correctly, your excel file should look like this and you should have 2000 rows in your file:
Now you need to export your excel file as a .csv file. Click on "file" and then "export to csv".
Once you create the file, go to the downloads folder in your computer and change the name of the file to "labels.csv".
Now go to your bucket and import that file you just created, you should know how to upload files to the bucket by now.
Once you do that, it should be visible on your bucket.
Ok, now that you have your images and the file with the paths to each image, along with the labels, go back to the AutoML Vision user interface and click on "add images" and "select csv file on cloud storage"
Select the path were you saved your .csv file and click "ok".
This will import the images on the AutoML project.
This operation will probably take some minutes, as it is scanning your .csv file, importing each image you have in your bucket and assigning it the label you decided to give it.
Take the opportunity to go and grab another coffee :)
Once it is done, it should look something like this...
As you can see, not all my images were imported, there are some missing.
This is not important, as I explained before, this service is still in Beta.
Ok, now that you have all the images with the correct labels imported in AutoML, now you need to train the model. Normally, this is the most difficult step in the process and machine learning professionals can spend weeks or even months doing this part. But thanks to google cloud AutoML, you now can train your model by simply clicking on the button "train"
At this point, you have the option to assign a training budget.
The more you spend training your model, the more precise it will be.
For this number of images, however, 10 minutes should be more than enough. Besides, the first hour of training is currently free :)
Click on start training.
Google will tell you that it is training your model and that you can close the window if you want.
It says it will send you an email as soon as the training is complete!
Once training is complete, you can go back to your model and see the results of the training in the "evaluate" tab. In my case, the results are pretty impressive!!
I will not go into detail explaining these results, I will leave that for another article, but just understand that my model is precise up to 98,5%, this is really incredible...
Now to the cool part. You should test your model!
Find an image on the internet of something you can hug and something you should not hug. Obviously, use images that YOU DID NOT use in your training dataset.
You can test your model by "predicting" what label your model gives to it.
Now to test it with something you cannot hug.
It worked, well done!!
You created a functioning program that uses artificial intelligence to analyse images and tell you if they should be hugged or not. And best of all, you did everything without coding!!
You could now sell this piece of technology to manufacturers of baby cameras around the world, become a millionaire and then invite me to your private island :)
Machine learning is an incredible field where much progress is done every day. If you are interested in understanding how machine learning works and what google is doing under the hood, please contact me or simply leave a comment below.
Thank you very much for reading all this tutorial, I hope you enjoyed it as much as I did.