In this short tutorial I'll show you how to turn any black and white photo into a colour image using a pre-packaged Machine Learning model and OpenFaaS.
OpenFaaS: Serverless Functions Made Simple is a platform for building and deploying code on any cloud.
In the community we packaged a machine-learning model developed by Richard Zhang from MIT. His model has been trained to "colorise" black and white images.
Original image: Public-Domain Cat Pictures From the Library of Congress
Up until last year I helped develop and operate a Twitter bot which allowed anyone to see their family photos in colour. The free-tier of Twitter's API changed and unfortunately we had to shut it down, but you can see see the pictures and use the "colorise" function on your own computer or cloud.
See also: Project paper
Let's deploy the version which was packaged as a function.
Get OpenFaaS
You can deploy OpenFaaS on any machine which has Docker, or you can use the cloud. In this example we'll use DigitalOcean which has a one-click image for deploying OpenFaaS.
Use my referral link for free credits when you join
- Log into your DigitalOcean dashboard
- Click Marketplace and OpenFaaS
I recommend using the 4GB droplet. The cost will be covered by our free credits. You should also pick your nearest region, which for me is London.
Finally enter the droplet name, I picked "openfaas-colorise".
Access your OpenFaaS installation
DigitalOcean will start up a VM with OpenFaaS installed.
- Log into the droplet using
ssh
and theipv4
address given in your dashboard
In my example I typed on my Mac:
ssh root@134.209.185.93
Note: If you forgot to configure your SSH access, then click "Access" and "Reset root password". DigitalOcean will now email you a password to use in the step above.
-
You'll now be asked to provide a password, the easiest way to set a secure password is to generate a new one in your terminal. Open a new terminal and type in
head -c 16 /dev/urandom |shasum
. You can now use that value. -
Wait for the initialization script to complete
-
Now get your username and password and store it for later
cat /root/.openfaas_password
[Credentials]
username: admin
password: 3702671c6d6647592120fa27fbd1e4b12d5c40f9
You can use the faas-cli login
command at a later time if you want to deploy functions from the command-line.
For advanced users set up OpenFaaS on Kubernetes
Open your dashboard
Now navigate to your OpenFaaS portal and log in, it will be the IP address on port 8080.
Deploy the "Colorise" function
There are many pre-packaged functions available in the store, and you can even build your own from scratch using any binary or programming language.
Once deployed you will see the Ready button set to grey, when it becomes available to click then we're good to go and the model has been downloaded on our droplet.
Test out your favourite cat images
I wrote this tutorial after seeing a tweet from LifeHacker where an old photograph of a black and white cat had been turned into colour.
The Library of Congress now has a free collection of vintage cat pictures https://t.co/wf77XvCx8P pic.twitter.com/qwjZudNL9s
— Lifehacker (@lifehacker) May 15, 2019
Somebody had done an excellent job of touching up the image with the "gimp" photo editing tool, but I wanted to see how OpenFaaS would do.
Let's try it.
https://i.kinja-img.com/gawker-media/image/upload/s--hs_50-b2--/c_scale,f_auto,fl_progressive,q_80,w_800/wv7kqqfxxtswupj2zhqi.jpg
Copy and paste this URL into the "Request" box and then hit "Invoke"
Note: Make sure the "Download" button is checked, because this tells the browser to download a new image after running.
The time taken to colorise an image varies depending on the CPU being used. When using a bare metal machine from Packet Host, then the colorised kitty will appear several times faster.
For our tutorial the turn-around time is not that important, but for a service or for bulk processing it does become more relevant. OpenFaaS also allows us to run our requests asynchronously and pick up the result later on.
Side-by-side
Here's the image that Tom painstakingly colorised by hand:
Here's our version:
Now there's a bit of work we can do to improve the automated version, such as removing a colour cast using ImageMagick.
Here's an outdoor scene which this model seems to do very well. It's using a photograph I took of a church in Cathedral Square in Peterborough, UK using 35mm B&W film.
See also: repainting-the-past GitHub repo.
Inception
You may also like one of our other machine-learning functions called "inception". Inception can be deployed in exactly the same way and uses the ImageNet dataset to classify what objects are found in your source image.
Find out more
There are several other functions in the store which use machine-learning, but the most interesting examples are the ones you create yourself. Whether it's a GitHub-bot, a single-page app or your corporate home-banking system.
To find out more about OpenFaaS checkout the website and try the hands-on workshop so that you can build and share your own awesome functions.