First Steps of Learning Deep Learning: Image Classification in Keras
Whether you want to start learning deep learning for you career, to have a nice adventure (e.g. with detecting huggable objects) or to get insight into machines before they take over, this post is for you!
So, which to choose? First, as always, screw all subtle performance benchmarks, as premature optimization is the root of all evil. What is crucial is to start with one which is easy to write (and read!), one with many online resources, and one that you can actually install on your computer without too much pain.
Bear in mind that core frameworks are multidimensional array expression compilers with GPU support. Current neural networks can be expressed as such. However, if you just want to work with neural networks, by rule of least power, I recommend starting with a framework just for neural networks. For example…
If you like the philosophy of Python (brevity, readability, one preferred way to do things), Keras is for you. It is a high-level library for neural networks, using TensorFlow or Theano as its backend. Also, if you want to have a propaganda picture, there is a possibly biased (or overfitted?) popularity ranking:
- The state of deep learning frameworks (from GitHub metrics), April 2017. - François Chollet (Keras creator)
If you want to consult a different source, based on arXiv papers rather than GitHub activity, see A Peek at Trends in Machine Learning by Andrej Karpathy. Popularity is important - it means that if you want to search for a network architecture, googling for it (e.g.
UNet Keras) is likely to return an example. Where to start learning it? Documentation on Keras is nice, and its blog is a valuable resource. For a complete, interactive introduction to deep learning with Keras in Jupyter Notebook, I really recommend:
- Deep Learning with Keras and TensorFlow by Valerio Maggio
For shorter ones, try one of these:
- Visualizing parts of Convolutional Neural Networks using Keras and Cats by Erik Reppel
- Deep learning for complete beginners: convolutional neural networks with Keras by Petar Veličković
- Handwritten Digit Recognition using Convolutional Neural Networks in Python with Keras by Jason Brownlee (Theano tensor dimension order6)
There are a few add-ons to Keras, which are especially useful for learning it. I created ASCII summary for sequential models to show data flow inside networks (in a nicer way than
model.summary()). It shows layers, dimensions of data
(x, y, channels) and the number of free parameters (to be optimized). For example, for a network for digit recognition it might look like:
If not Keras, then I recommend starting with bare TensorFlow. It is a bit more low-level and verbose, but makes it straightforward to optimize various multidimensional array (or, well, tensor) operations. A few good resources:
- the official TensorFlow Tutorial is very good
- Learn TensorFlow and deep learning, without a Ph.D. by Martin Görner
- TensorFlow Tutorial and Examples for beginners by Aymeric Damien (with Python 2.7)
- Simple tutorials using Google’s TensorFlow Framework by Nathan Lintz
Theano is similar to TensorFlow, but a bit older and harder to start. For example, you need to manually write updates of variables. Typical neural network layers are not included, so one often uses libraries such as Lasagne. If you’re looking for a place to start, I like this introduction:
- Theano Tutorial by Marek Rei
At the same time, if you see some nice code in Torch or PyTorch, don’t be afraid to install and run it!
EDIT (July 2017): If you want a low-level framework, PyTorch may be the best way to start. It combines relatively brief and readable code (almost like Keras) but at the same time gives low-level access to all features (actually, more than TensorFlow). Start here:
Every machine learning problem needs data. You cannot just tell it “detect if there is a cat in this picture”and expect the computer to tell you the answer. You need to show many instances of cats, and pictures not containing cats, and (hopefully) it will learn to generalize it to other cases. So, you need some data to start. And it is not a drawback of machine learning or just deep learning - it is a fundamental property of any learning!
Before you dive into uncharted waters, it is good to take a look at some popular datasets. The key part about them is that they are… popular. It means that you can find a lot of examples what works. And have a guarantee that these problems can be solved with neural networks.
Many good ideas will not work well on MNIST (e.g. batch norm). Inversely many bad ideas may work on MNIST and no[t] transfer to real [computer vision]. - François Chollet’s tweet
Still, I recommend starting with the MNIST digit recognition dataset (60k grayscale 28x28 images), included in keras.datasets. Not necessary to master it, but just to get a sense that it works at all (or to test the basics of Keras on your local machine).
Indeed, I once even proposed that the toughest challenge facing AI workers is to answer the question: “What are the letters ‘A’ and ‘I’? - Douglas R. Hofstadter (1995)
A more interesting dataset, and harder for classical machine learning algorithms, is notMNIST (letters A-J from strange fonts). If you want to start with it, here is my code for notMNIST loading and logistic regression in Keras.
If you want to play with image recognition, there is CIFAR dataset, a dataset of 32x32 photos (also in keras.datasets). It comes in two versions: 10 simple classes (including cats, dogs, frogs and airplanes ) and 100 harder and more nuanced classes (including beaver, dolphin, otter, seal and whale). I strongly suggest starting with CIFAR-10, the simpler version. Beware, more complicated networks may take quite some time (~12h on CPU my 7 year old Macbook Pro).
Deep learning requires a lot of data. If you want to train your network from scratch, it may require as many as ~10k images even if low-resolution (32x32). Especially if data is scarce, there is no guarantee that a network will learn anything. So, what are the ways to go?
- use really low res (if your eye can see it, no need to use higher resolution)
- get a lot of data (for images like 256x256 it may be: millions of instances)
- re-train a network that already saw a lot
- generate much more data (with rotations, shifts, distortions)
Often, it’s a combination of everything mentioned here.
Standing on the shoulders of giants
Creating a new neural network has a lot in common with cooking - there are typical ingredients (layers) and recipes (popular network architectures). The most important cooking contest is ImageNet Large Scale Visual Recognition Challenge, with recognition of hundreds of classes from half a million dataset of photos. Look at these Neural Network Architectures, typically using 224x224x3 input (chart by Eugenio Culurciello):
Circle size represents the number of parameters (a lot!). It doesn’t mention SqueezeNet though, an architecture vastly reducing the number of parameters (e.g. 50x fewer).
A few key networks for image classification can be readily loaded from the keras.applications module: Xception, VGG16, VGG19, ResNet50, InceptionV3. Some others are not as plug & play, but still easy to find online - yes, there is SqueezeNet in Keras. These networks serve two purposes:
- they give insight into useful building blocks and architectures
- they are great candidates for retraining (so-called transfer learning), when using architecture along with pre-trained weights)
Some other important network architectures for images:
- U-Net: Convolutional Networks for Biomedical Image Segmentation
- A Neural Algorithm of Artistic Style
- Neural Style Transfer & Neural Doodles implemented in Keras by Somshubra Majumdar
- A Brief History of CNNs in Image Segmentation: From R-CNN to Mask R-CNN by Dhruv Parthasarathy
Another set of insights:
- The Neural Network Zoo by Fjodor van Veen
- How to train your Deep Neural Network - how many layers, parameters, etc
For very small problems (e.g. MNIST, notMNIST), you can use your personal computer - even if it is a laptop and computations are on CPU.
For small problems (e.g. CIFAR, the unreasonable RNN), you might be still able to use a PC, but it requires much more patience and trade-offs.
For medium and larger problems, essentially the only way to go is to use a machine with a strong graphic card (GPU). For example, it took us 2 days to train a model for satellite image processing for a Kaggle competition, see our:
- Deep learning for satellite imagery via image segmentation by Arkadiusz Nowaczyński
On a strong CPU it would have taken weeks, see:
- Benchmarks for popular convolutional neural network models by Justin Johnson
The easiest, and the cheapest, way to use a strong GPU is to rent a remote machine on a per-hour basis. You can use Amazon (it is not only a bookstore!), here are some guides:
- Keras with GPU on Amazon EC2 – a step-by-step instruction by Mateusz Sieniawski, my mentee
- Running Jupyter notebooks on GPU on AWS: a starter guide by Francois Chollet
I encourage you to interact with code. For example, notMNIST or CIFAR-10 can be great starting points. Sometimes the best start is to start with someone’s else code and run it, then see what happens when you modify parameters.
For learning how it works, this one is a masterpiece:
- CS231n: Convolutional Neural Networks for Visual Recognition by Andrej Karpathy and the lecture videos
When it comes to books, there is a wonderful one, starting from introduction to mathematics and machine learning learning context (it even covers log-loss and entropy in a way I like!):
- Deep Learning, An MIT Press book by Ian Goodfellow, Yoshua Bengio and Aaron Courville
Alternatively, you can use (it may be good for an introduction with interactive materials, but I’ve found the style a bit long-winded):
- Neural Networks and Deep Learning by Michael Nielsen
There are many applications of deep learning (it’s not only image recognition!). I collected some introductory materials to cover its various aspects (beware: they are of various difficulty). Don’t try to read them all - I list them for inspiration, not intimidation!
- The Unreasonable Effectiveness of Recurrent Neural Networks by Andrej Karpathy
- How convolutional neural networks see the world - Keras Blog
- What convolutional neural networks look at when they see nudity – Clarifai Blog (NSFW)
- Convolutional neural networks for artistic style transfer by Harish Nrayanan
- Dreams, Drugs and ConvNets - my slides (NSFW); I am considering turning it into a longer post on machine learning vs human learning, based on common mistakes
- Yes you should understand backprop by Andrej Karpathy
- Transfer Learning using Keras by Prakash Vanapalli
- Generative Adversarial Networks (GANs) in 50 lines of code (PyTorch)
- Minimal and Clean Reinforcement Learning Examples
- An overview of gradient descent optimization algorithms by Sebastian Ruder
- Picking an optimizer for Style Transfer by Slav Ivanov
- Building Autoencoders in Keras by Francois Chollet
- Understanding LSTM Networks by Chris Olah
- Recurrent Neural Networks & LSTMs by Rohan Kapur
- Oxford Deep NLP 2017 course
- List of resources
- Staying up-to-date:
- r/MachineLearning Reddit channel covering most of new stuff
- distill.pub - an interactive, visual, open-access journal for machine learning research, with expository articles
- my links at pinboard.in/u:pmigdal/t:deep-learning - though, just saving, not an automatic recommendation
- @fastml_extra Twitter channel
- GitXiv for papers with code
- don’t be afraid to read academic papers; some are well-written and insightful (if you own Kindle or another e-reader, I recommend Dontprint)
- Data (usually from challenges)
- AF Classification from a short single lead ECG recording: the PhysioNet/Computing in Cardiology Challenge 2017
- iNaturalist 2017 Competition (675k images with 5k species), vide Mushroom AI
If you recommend a source that helped you with your adventure with deep learning - feel invited to contact me! (@pmigdal for short links, an email for longer remarks.)
The deep learning meme is not mine - I’ve just I rewrote from Theano to Keras (with TensorFlow backend).
- NOAA Right Whale Recognition, Winners’ Interview (1st place, Jan 2016), and a fresh one: Deep learning for satellite imagery via image segmentation (4th place, Apr 2017).
- This January during a 5-day workshop 6 high-school students participated in a rather NSFL project - constructing a neural network for detecting trypophobia triggers, see e.g. grzegorz225/trypophobia-detector and cytadela8/trypophobia_detector.
- It made a few episodes of webcomics obsolete: xkcd: Tasks (totally, by Park or Bird?), xkcd: Game AI (partially, by AlphaGo), PHD Comics: If TV Science was more like REAL Science (not exactly, but still it’s cool, by LapSRN).
- The title alludes to The Unreasonable Effectiveness of Mathematics in the Natural Sciences by Eugene Wigner (1960), one of my favourite texts in philosophy of science. Along with More is Different by PW Andreson (1972) and Genesis and development of a scientific fact (pdf here) by Ludwik Fleck (1935).
- If your background is in quantum information, the only thing you need to change is ℂ to ℝ. Just expect less tensor structure, but more convolutions.
- Is it only me, or does Theano tensor dimension order sound like some secret convent? Before you start searching how to join it: it is about the shape of multi-dimensional arrays:
(samples, channels, x, y)rather than TensorFlow’s
(samples, x, y, channels).
Bio: Piotr Migdał (@pmigdal)is a data science freelancer, with PhD in quantum physics; based in Warsaw, Poland. Active in gifted education, developing a quantum game and working as a data science instructor at deepsense.io.
Original. Reposted with permission.