{"cells":[{"metadata":{"_cell_guid":"dfdf80a5-bb2a-41c8-bb66-1e432e852b67","_uuid":"6cc8f4d4b8d92d1970c259922d95cf17f80a7487"},"cell_type":"markdown","source":"# Introduction\n\nYou'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\nAs an example, your model will take an images like this and identify it as a shoe:\n![Imgur](https://i.imgur.com/GyXOnSB.png)"},{"metadata":{"_uuid":"85022d62f8b2f581b2a067c8289fd67bcf517ccd"},"cell_type":"markdown","source":"# Data Preparation\nThis code is supplied, and you don't need to change it. Just run the cell below."},{"metadata":{"_cell_guid":"d9aa6241-74fc-4c05-bad5-6281d3f45966","_uuid":"2da7ce8c4a1de3b53665c145a91d4bc7e168ddd2","trusted":true},"cell_type":"code","source":"import numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.python import keras\nimport tensorflow as tf\n\nimg_rows, img_cols = 28, 28\nnum_classes = 10\n\ndef prep_data(raw, train_size, val_size):\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\nfashion_file = \"../input/fashionmnist/fashion-mnist_train.csv\"\nfashion_data = np.loadtxt(fashion_file, skiprows=1, delimiter=',')\nx, y = prep_data(fashion_data, train_size=50000, val_size=5000)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"0c6bbeef0057720505454507ae098d96b160b4df"},"cell_type":"markdown","source":"# Specify Model\n**STEPS:**\n1. Create a `Sequential` model. Call it `fashion_model`.\n2. Add 3 `Conv2D` layers to `fashion_model`.  Make each layer have 12 filters, a kernel_size of 3 and a **relu** activation.  You will need to specify the `input_shape` for the first `Conv2D` layer.  The input shape in this case is `(img_rows, img_cols, 1)`.\n3. Add a `Flatten` layer to `fashion_model` after the last `Conv2D` layer.\n4. Add a `Dense` layer with 100 neurons to `fashion_model` after the `Flatten` layer.  \n5. 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)."},{"metadata":{"_cell_guid":"d2e326a6-ac8a-422b-8cff-6c1d7ccd7ced","_uuid":"4d2c616eb22814d5285ba267bc839a43b3fea6b6","trusted":true},"cell_type":"code","source":"from tensorflow.python import keras\nfrom tensorflow.python.keras.models import Sequential\nfrom tensorflow.python.keras.layers import Dense, Flatten, Conv2D\n\nfashion_model = Sequential()\nfashion_model.add(Conv2D(12, kernel_size=(3, 3),\n                 activation='relu',\n                 input_shape=(img_rows, img_cols, 1)))\nfashion_model.add(Conv2D(12, kernel_size=(3, 3),\n                 activation='relu'))\nfashion_model.add(Conv2D(12, kernel_size=(3, 3),\n                 activation='relu'))                  \nfashion_model.add(Flatten())\nfashion_model.add(Dense(100,activation='relu'))\nfashion_model.add(Dense(num_classes,activation='softmax'))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5e660e46f096b8adfeeb5660d179e81a40338e40"},"cell_type":"markdown","source":"# Compile Model\nRun the command `fashion_model.compile`.  Specify the following arguments:\n1. `loss = keras.losses.categorical_crossentropy`\n2. `optimizer = 'adam'`\n3. `metrics = ['accuracy']`"},{"metadata":{"_uuid":"e74bb650acce4efaa5335f6e48803f246332c679","trusted":true},"cell_type":"code","source":"fashion_model.compile(loss = keras.losses.categorical_crossentropy, optimizer='adam', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"1ab679642ebe0a4edc511b361e1d9e1a4ab268f4"},"cell_type":"markdown","source":"# Fit Model\nRun the command `fashion_model.fit`. The arguments you will use are\n1. The first two are arguments are the data used to fit the model, which are `x` and `y` respectively.\n2. `batch_size = 100`\n3. `epochs = 4`\n4. `validation_split = 0.2`\n\nWhen you run this command, you can watch your model start improving.  You will see validation accuracies after each epoch."},{"metadata":{"_uuid":"a001324c9bc11570a15eb201070d65cd8d0e81da","trusted":true},"cell_type":"code","source":"fashion_model.fit(x=x, y=y, batch_size=100, epochs=4, validation_split=0.2)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7190e55038bb42c8c1818cc0d6e55e2f02a8092e"},"cell_type":"markdown","source":"# Keep Going\nYou 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\n---\n**[Deep Learning Track Home Page](https://www.kaggle.com/learn/deep-learning)**\n\n"},{"metadata":{"_kg_hide-output":true,"trusted":true,"_uuid":"329b353948c90ed5063cbdd7faf8ed46c7f86e08"},"cell_type":"code","source":"import matplotlib.pyplot as plt\nfashion_file_test = \"../input/fashionmnist/fashion-mnist_test.csv\"\nfashion_data_test = np.loadtxt(fashion_file, skiprows=1, delimiter=',')\nfashion_data_test[:1]\nx_test,y_test = prep_data(fashion_data_test, train_size=50000, val_size=5000)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a27230fdccacf02bf8febde91e1a75399ba567b6"},"cell_type":"code","source":"x_test[:1].reshape(28,28)\nplt.imshow(x_test[:1].reshape(28,28))\nplt.imshow(x[0].reshape(28,28), interpolation='nearest')\npreds = fashion_model.predict_classes(x_test[:10])\nprint('Predictions', preds)\nprint('Labels ',fashion_data_test[:10,0])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9fa863fd9369d5cbcd98d8fa5d851e41d6fd84bb"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}