{"cells": [{"cell_type": "markdown", "metadata": {}, "source": ["**This notebook is an exercise in the [Deep Learning for Computer Vision](https://www.kaggle.com/learn/deep-learning-for-computer-vision) course.  You can reference the tutorial at [this link](https://www.kaggle.com/dansbecker/deep-learning-from-scratch).**\n", "\n", "---\n"]}, {"cell_type": "markdown", "metadata": {}, "source": ["# Introduction\n", "\n", "You've seen how to build a model from scratch to identify handwritten digits.  You'll now build a model to identify different types of clothing.  To make models that train quickly, we'll work with very small (low-resolution) images. \n", "\n", "As an example, your model will take an images like this and identify it as a shoe:\n", "\n", "![Imgur](https://i.imgur.com/GyXOnSB.png)"]}, {"cell_type": "markdown", "metadata": {}, "source": ["# Data Preparation\n", "This code is supplied, and you don't need to change it. Just run the cell below."]}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "outputs": [], "source": ["import numpy as np\n", "from sklearn.model_selection import train_test_split\n", "from tensorflow import keras\n", "\n", "img_rows, img_cols = 28, 28\n", "num_classes = 10\n", "\n", "def prep_data(raw):\n", "    y = raw[:, 0]\n", "    out_y = keras.utils.to_categorical(y, num_classes)\n", "    \n", "    x = raw[:,1:]\n", "    num_images = raw.shape[0]\n", "    out_x = x.reshape(num_images, img_rows, img_cols, 1)\n", "    out_x = out_x / 255\n", "    return out_x, out_y\n", "\n", "fashion_file = \"../input/fashionmnist/fashion-mnist_train.csv\"\n", "fashion_data = np.loadtxt(fashion_file, skiprows=1, delimiter=',')\n", "x, y = prep_data(fashion_data)\n", "\n", "# Set up code checking\n", "from learntools.core import binder\n", "binder.bind(globals())\n", "from learntools.deep_learning.exercise_7 import *\n", "print(\"Setup Complete\")"]}, {"cell_type": "markdown", "metadata": {}, "source": ["# 1) Start the model\n", "Create a `Sequential` model called `fashion_model`. Don't add layers yet."]}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "outputs": [], "source": ["from tensorflow import keras\n", "from tensorflow.keras.models import Sequential\n", "from tensorflow.keras.layers import Dense, Flatten, Conv2D\n", "\n", "# Your Code Here\n", "____\n", "\n", "# Check your answer\n", "q_1.check()"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["#q_1.solution()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["# 2) Add the first layer\n", "\n", "Add the first `Conv2D` layer to `fashion_model`. It should have 12 filters, a kernel_size of 3 and the `relu` activation function. The first layer always requires that you specify the `input_shape`.  We have saved the number of rows and columns to the variables `img_rows` and `img_cols` respectively, so the input shape in this case is `(img_rows, img_cols, 1)`."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# Your code here\n", "____\n", "\n", "# Check your answer\n", "q_2.check()"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# q_2.hint()\n", "#q_2.solution()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["# 3) Add the remaining layers\n", "\n", "1. Add 2 more convolutional (`Conv2D layers`) with 20 filters each, 'relu' activation, and a kernel size of 3. Follow that with a `Flatten` layer, and then a `Dense` layer with 100 neurons. \n", "2. Add your prediction layer to `fashion_model`.  This is a `Dense` layer.  We alrady have a variable called `num_classes`.  Use this variable when specifying the number of nodes in this layer. The activation should be `softmax` (or you will have problems later)."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# Your code here\n", "____\n", "\n", "# Check your answer\n", "q_3.check()"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# q_3.solution()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["# 4) Compile Your Model\n", "Compile fashion_model with the `compile` method.  Specify the following arguments:\n", "1. `loss = \"categorical_crossentropy\"`\n", "2. `optimizer = 'adam'`\n", "3. `metrics = ['accuracy']`"]}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "outputs": [], "source": ["# Your code to compile the model in this cell\n", "____\n", "\n", "# Check your answer\n", "q_4.check()"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# q_4.solution()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["# 5) Fit The Model\n", "Run the command `fashion_model.fit`. The arguments you will use are\n", "1. The data used to fit the model. First comes the data holding the images, and second is the data with the class labels to be predicted. Look at the first code cell (which was supplied to you) where we called `prep_data` to find the variable names for these.\n", "2. `batch_size = 100`\n", "3. `epochs = 4`\n", "4. `validation_split = 0.2`\n", "\n", "When you run this command, you can watch your model start improving.  You will see validation accuracies after each epoch."]}, {"cell_type": "code", "execution_count": null, "metadata": {"collapsed": true}, "outputs": [], "source": ["# Your code to fit the model here\n", "____\n", "\n", "# Check your answer\n", "q_5.check()"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["#q_5.solution()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["# 6) Create A New Model\n", "\n", "Create a new model called `second_fashion_model` in the cell below.  Make some changes so it is different than `fashion_model` that you've trained above. The change could be using a different number of layers, different number of convolutions in the layers, etc.\n", "\n", "Define the model, compile it and fit it in the cell below.  See how its validation score compares to that of the original model."]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["# Your code below\n", "____\n", "\n", "\n", "# Don't remove this line (ensures comptibility with tensorflow 2.0)\n", "second_fashion_model.history.history['val_acc'] = second_fashion_model.history.history['val_accuracy']\n", "# Check your answer\n", "q_6.check()"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["#q_6.solution()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["# Keep Going\n", "You are ready to learn about **[strides and dropout](https://www.kaggle.com/dansbecker/dropout-and-strides-for-larger-models)**, which become important as you start using bigger and more powerful models.\n"]}, {"cell_type": "markdown", "metadata": {}, "source": ["---\n", "\n", "\n", "\n", "\n", "*Have questions or comments? Visit the [Learn Discussion forum](https://www.kaggle.com/learn-forum/161321) to chat with other Learners.*"]}], "metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5"}}, "nbformat": 4, "nbformat_minor": 2}