{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Importing Libraries"},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.layers import Input, Conv2D, Dense, Flatten, Dropout, MaxPooling2D, BatchNormalization\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Loading Data"},{"metadata":{},"cell_type":"markdown","source":"CIFAR-10 data set is available in tensorflow. So we load the data set directly from tensorflow. For normalizing we divide X_train and X_test to 255.0"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Load in the data\ncifar10 = tf.keras.datasets.cifar10\n\n(X_train, y_train), (X_test, y_test) = cifar10.load_data()\nX_train, X_test = X_train / 255.0, X_test / 255.0\ny_train, y_test = y_train.flatten(), y_test.flatten()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(X_train.shape)\nprint(X_test.shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Visualizing"},{"metadata":{},"cell_type":"markdown","source":"CIFAR-10  is an established computer-vision dataset used for object recognition. It is a subset of the 80 million tiny images dataset and consists of 60,000 32x32 color images containing one of 10 object classes, with 6000 images per class. We can look at some of the images below."},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure()\nplt.imshow(X_train[2])\nplt.colorbar()\nplt.grid(False)\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(6):\n    plt.subplot(330 + 1 + i)\n    plt.imshow(X_train[i])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Classes"},{"metadata":{"trusted":true},"cell_type":"code","source":"num_class = len(set(y_train))\nprint(\"Number Of Classes: \", num_class)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Building Model with Functional API"},{"metadata":{},"cell_type":"markdown","source":"The Keras functional API is a way to create models that are more flexible than the tf.keras.Sequential API. The functional API can handle models with non-linear topology, shared layers, and even multiple inputs or outputs.\n\nThe main idea is that a deep learning model is usually a directed acyclic graph (DAG) of layers. So the functional API is a way to build graphs of layers."},{"metadata":{"trusted":true},"cell_type":"code","source":"i = Input(shape = X_train[0].shape)\nx = Conv2D(32, (3,3), padding=\"same\", activation=\"relu\")(i)\nx = Conv2D(32, (3,3), activation=\"relu\")(x)\nx = MaxPooling2D(pool_size=(2,2))(x)\nx = Dropout(0.30)(x)\nx = Conv2D(64, (3,3), padding=\"same\", activation=\"relu\")(x)\nx = Conv2D(64, (3,3), activation=\"relu\")(x)\nx = MaxPooling2D(pool_size=(2,2))(x)\nx = Dropout(0.30)(x)\nx = BatchNormalization()(x)\nx = Flatten()(x)\nx = Dense(512, activation=\"relu\")(x)\nx = Dropout(0.5)(x)\nx = Dense(num_class, activation=\"softmax\")(x)\n\nmodel = Model(i, x)\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Keras supports the early stopping of training via a callback called EarlyStopping.\n\nThis callback allows you to specify the performance measure to monitor, the trigger, and once triggered, it will stop the training process."},{"metadata":{"trusted":true},"cell_type":"code","source":"r = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=100)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot loss per iteration\nimport matplotlib.pyplot as plt\nplt.plot(r.history['loss'], label='loss')\nplt.plot(r.history['val_loss'], label='val_loss')\nplt.legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Plot accuracy per iteration\nplt.plot(r.history['accuracy'], label='acc')\nplt.plot(r.history['val_accuracy'], label='val_acc')\nplt.legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}