{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Home Page](../Start_Here.ipynb)\n",
" \n",
" \n",
" \n",
" \n",
" \n",
"[Next Notebook](CNN's.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CNN Primer and Keras 101"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"In this notebook you will be introduced to the concept of a convolutional neural network (CNN) and implement one using Keras. This notebook is designed as a starting point for absolute beginners to deep learning.\n",
"\n",
"**Contents of this notebook:**\n",
"\n",
"- [How a deep learning project is planned](#Machine-Learning-Pipeline)\n",
"- [Wrapping things up with an example (classification)](#Wrapping-Things-up-with-an-Example)\n",
" - [Fully connected networks](#Image-Classification-on-types-of-Clothes)\n",
"\n",
"\n",
"**By the end of this notebook the participant will:**\n",
"\n",
"- Understand machine learning pipelines\n",
"- Write a deep learning classifier and train it\n",
"\n",
"**We will be building a _multi-class classifier_ to classify images of clothing into their respective classes.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Machine Learning Pipeline\n",
"\n",
"During the bootcamp we will be making use of the following concepts to help us understand how a machine learning (ML) project should be planned and executed:\n",
"\n",
"1. **Data**: To start any ML project we need data which is pre-processed and can be fed into the network.\n",
"2. **Task**: There are many possible tasks in the field of ML; we need to make sure we understand and define the problem statement accurately.\n",
"3. **Model**: We need to build our model, which is neither too deep (requiring a lot of computational power) nor too small (preventing it from learning the important features).\n",
"4. **Loss**: Out of the many _loss functions_ that can be defined, we need to carefully choose one which is suitable for the task we are about to carry out.\n",
"5. **Learning**: There are a variety of _optimisers_, each with their advantages and disadvantages. We must choose one which is suitable for our task and train our model using some suitably chosen hyperparameters.\n",
"6. **Evaluation**: We must determine if our model has learned the features properly by analysing how it performs on data it has not previously seen. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Here we will be building a _multi-class classifier_ to classify images of clothing into their respective classes.**\n",
"\n",
"We will follow the pipeline presented above to complete the example.\n",
"\n",
"## Image classification on types of clothes \n",
"\n",
"#### Step 1: Data \n",
"\n",
"We will be using the **Fashion MNIST** dataset, which is a very popular introductory dataset in deep learning. This dataset contains 70,000 grayscale images in 10 categories. The images show individual articles of clothing at low resolution (28 by 28 pixels).\n",
"\n",
"\n",
"\n",
"*Source: https://www.tensorflow.org/tutorials/keras/classification*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import Necessary Libraries\n",
"\n",
"from __future__ import absolute_import, division, print_function, unicode_literals\n",
"\n",
"# TensorFlow and tf.keras\n",
"import tensorflow as tf\n",
"from tensorflow import keras\n",
"\n",
"# Helper libraries\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"print(tf.__version__)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Let's Import the Dataset\n",
"fashion_mnist = keras.datasets.fashion_mnist\n",
"\n",
"(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Loading the dataset returns four NumPy arrays:\n",
"\n",
"* The `train_images` and `train_labels` arrays are the *training set*—the data the model uses to learn.\n",
"* The model is tested against the *test set*, the `test_images`, and `test_labels` arrays.\n",
"\n",
"The images are 28x28 NumPy arrays, with pixel values ranging from 0 to 255. The *labels* are an array of integers, ranging from 0 to 9. These correspond to the *class* of clothing the image represents:\n",
"\n",
"
Label | \n", "Class | \n", "
---|---|
0 | \n", "T-shirt/top | \n", "
1 | \n", "Trouser | \n", "
2 | \n", "Pullover | \n", "
3 | \n", "Dress | \n", "
4 | \n", "Coat | \n", "
5 | \n", "Sandal | \n", "
6 | \n", "Shirt | \n", "
7 | \n", "Sneaker | \n", "
8 | \n", "Bag | \n", "
9 | \n", "Ankle boot | \n", "