{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"## Name: Sailesh Nair","metadata":{"id":"zf6dSjG-meue"}},{"cell_type":"markdown","source":"# Cifar Solution:","metadata":{"id":"BC0zvo0SqiGr"}},{"cell_type":"code","source":"from keras.datasets import cifar10\nfrom tensorflow.keras.layers import Conv2D,MaxPooling2D\nfrom keras.layers.core import  Dense, Dropout, Activation\nfrom keras.models import Sequential\n\nimport matplotlib.pyplot as plt\nfrom keras import models,layers\nfrom tensorflow.keras.utils import to_categorical\nfrom keras import optimizers\nimport numpy as np","metadata":{"id":"EjSGcBT1y4D8","execution":{"iopub.status.busy":"2021-07-31T14:52:15.622515Z","iopub.execute_input":"2021-07-31T14:52:15.623032Z","iopub.status.idle":"2021-07-31T14:52:19.671956Z","shell.execute_reply.started":"2021-07-31T14:52:15.622821Z","shell.execute_reply":"2021-07-31T14:52:19.670918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(xtrain, ytrain), (xtest, ytest) = cifar10.load_data()\nprint(xtrain.shape)\nprint(xtest.shape)\nprint(ytrain.shape)\nprint(ytest.shape)","metadata":{"id":"gv5NWnEPy4FQ","outputId":"5f38193e-f681-466d-f74a-b35d4e1c04f8","execution":{"iopub.status.busy":"2021-07-31T14:52:19.673633Z","iopub.execute_input":"2021-07-31T14:52:19.673991Z","iopub.status.idle":"2021-07-31T14:52:27.760494Z","shell.execute_reply.started":"2021-07-31T14:52:19.673958Z","shell.execute_reply":"2021-07-31T14:52:27.759458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# convert the pixel values in float\nxtrain = xtrain.astype('float32')\nxtest = xtest.astype('float32')\n# scale the images\nxtrain /= 255  # ths is eqvalent to xtrain = xtrain/255 \nxtest /= 255\nytrain = to_categorical(ytrain)\nytest = to_categorical(ytest)   ","metadata":{"id":"R3PnsOiqy4Ep","execution":{"iopub.status.busy":"2021-07-31T14:52:27.762607Z","iopub.execute_input":"2021-07-31T14:52:27.762993Z","iopub.status.idle":"2021-07-31T14:52:28.022783Z","shell.execute_reply.started":"2021-07-31T14:52:27.762952Z","shell.execute_reply":"2021-07-31T14:52:28.021672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=['airplane', 'automobile','bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']","metadata":{"id":"ixQwYgs3y4F_","execution":{"iopub.status.busy":"2021-07-31T14:52:28.024743Z","iopub.execute_input":"2021-07-31T14:52:28.025171Z","iopub.status.idle":"2021-07-31T14:52:28.030950Z","shell.execute_reply.started":"2021-07-31T14:52:28.025130Z","shell.execute_reply":"2021-07-31T14:52:28.029403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#explore data\nprint(ytrain[2000])\nplt.imshow(xtrain[2000])\nplt.show()","metadata":{"id":"mtn_1uyTy4GP","outputId":"9778f8d1-4978-45c3-838c-254485ce54a4","execution":{"iopub.status.busy":"2021-07-31T14:52:28.032606Z","iopub.execute_input":"2021-07-31T14:52:28.033027Z","iopub.status.idle":"2021-07-31T14:52:28.234682Z","shell.execute_reply.started":"2021-07-31T14:52:28.032991Z","shell.execute_reply":"2021-07-31T14:52:28.233644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from datetime import datetime\ndef timer(start_time=None):\n    if not start_time:\n        print(datetime.now())\n        start_time = datetime.now()\n        return start_time\n    elif start_time:\n        thour, temp_sec = divmod((datetime.now() - start_time).total_seconds(), 3600)\n        tmin, tsec = divmod(temp_sec, 60)\n        print('Time taken: %i hours %i minutes and %s seconds.' % (thour, tmin, round(tsec, 2)))","metadata":{"id":"0RdMlS19meui","execution":{"iopub.status.busy":"2021-07-31T14:52:28.236199Z","iopub.execute_input":"2021-07-31T14:52:28.236598Z","iopub.status.idle":"2021-07-31T14:52:28.245764Z","shell.execute_reply.started":"2021-07-31T14:52:28.236559Z","shell.execute_reply":"2021-07-31T14:52:28.244908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(xtrain[10])","metadata":{"id":"uD2GMDhNpPNv","outputId":"a6d3e3d3-c622-4d6b-f64c-2a48146bf3e7","execution":{"iopub.status.busy":"2021-07-31T14:52:28.247173Z","iopub.execute_input":"2021-07-31T14:52:28.247576Z","iopub.status.idle":"2021-07-31T14:52:28.470684Z","shell.execute_reply.started":"2021-07-31T14:52:28.247537Z","shell.execute_reply":"2021-07-31T14:52:28.469183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ytrain[10]","metadata":{"id":"u0JDs9BdpgzM","outputId":"6b03915e-b873-4d62-995e-6f51e5fc1fbf","execution":{"iopub.status.busy":"2021-07-31T14:52:28.472688Z","iopub.execute_input":"2021-07-31T14:52:28.473254Z","iopub.status.idle":"2021-07-31T14:52:28.483483Z","shell.execute_reply.started":"2021-07-31T14:52:28.473211Z","shell.execute_reply":"2021-07-31T14:52:28.482264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Creating the Model Layers","metadata":{"id":"3RO8gtsboFhY"}},{"cell_type":"code","source":"model = models.Sequential()\n\nmodel.add(layers.Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=(32,32,3)))\nmodel.add(layers.Conv2D(32,(3,3), padding='same',activation='relu'))\nmodel.add(layers.MaxPooling2D(pool_size=2)) \nmodel.add(layers.Dropout(0.2))\n\nmodel.add(layers.Conv2D(64,(3,3), padding='same',activation='relu'))\nmodel.add(layers.Conv2D(128,(3,3), padding='same',activation='relu'))\nmodel.add(layers.MaxPooling2D(pool_size=2)) \nmodel.add(layers.Dropout(0.2))\n\nmodel.add(layers.Conv2D(64,(3,3), padding='same',activation='relu'))\nmodel.add(layers.Conv2D(64,(3,3), padding='same',activation='relu'))\nmodel.add(layers.MaxPooling2D(pool_size=2)) \nmodel.add(layers.Dropout(0.2))\n\n#Classification layers\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(512,activation='relu'))\nmodel.add(layers.Dropout(0.2))\nmodel.add(layers.Dense(64,activation='relu'))\nmodel.add(layers.Dropout(0.2))\nmodel.add(layers.Dense(10,activation='softmax'))  # this is the actual output layer\n\n# initiate Adam optimizer\nopt = optimizers.Adam(learning_rate=1e-4, decay=1e-6)\n\n# Let's train the model \nmodel.compile(loss='categorical_crossentropy',\n              optimizer=opt,\n              metrics=['accuracy'])\n","metadata":{"id":"i3PGpIU7y4Hz","execution":{"iopub.status.busy":"2021-07-31T14:52:28.486805Z","iopub.execute_input":"2021-07-31T14:52:28.487682Z","iopub.status.idle":"2021-07-31T14:52:30.445666Z","shell.execute_reply.started":"2021-07-31T14:52:28.487635Z","shell.execute_reply":"2021-07-31T14:52:30.444766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Viewing the Model Summary","metadata":{"id":"lDXfsqPqoJkh"}},{"cell_type":"code","source":"model.summary()","metadata":{"id":"N4Nbn9mCmeuk","outputId":"1992d67f-6bc5-4978-cae3-f8cb546ae59e","execution":{"iopub.status.busy":"2021-07-31T14:52:30.447180Z","iopub.execute_input":"2021-07-31T14:52:30.447519Z","iopub.status.idle":"2021-07-31T14:52:30.463425Z","shell.execute_reply.started":"2021-07-31T14:52:30.447485Z","shell.execute_reply":"2021-07-31T14:52:30.462505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Fitting the Model","metadata":{"id":"zSE5aZIHoN1G"}},{"cell_type":"code","source":"start_time=timer(None)\nresult = model.fit(\n    xtrain,\n    ytrain,\n    validation_split=0.1,    \n    verbose=True,\n    epochs=125,\n    steps_per_epoch= 64,\n    batch_size=512\n    )\ntimer(start_time)","metadata":{"id":"uQkjg8bty4Ip","outputId":"fd1f7250-9d28-4723-e754-e4f8d110385d","execution":{"iopub.status.busy":"2021-07-31T14:52:30.464558Z","iopub.execute_input":"2021-07-31T14:52:30.464915Z","iopub.status.idle":"2021-07-31T14:56:21.529428Z","shell.execute_reply.started":"2021-07-31T14:52:30.464887Z","shell.execute_reply":"2021-07-31T14:56:21.528574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##Visualizing the performance","metadata":{"id":"D6HG7sWknx2A"}},{"cell_type":"code","source":"plt.figure(figsize=(20, 10))\nplt.subplot(1, 2, 1)\nplt.title(\"CNN Training and Validation Accuracy\")\nplt.plot(result.history[\"accuracy\"], label='Training Accuracy')\nplt.plot(result.history['val_accuracy'], label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.subplot(1, 2, 2)\nplt.plot( result.history[\"loss\"], label='Training Loss')\nplt.plot( result.history[\"val_loss\"], label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('CNN Training and Validation Loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.show()","metadata":{"id":"WkSAdxhWmeul","outputId":"6f5d2020-8832-4025-a9df-8574bcd7d9d7","execution":{"iopub.status.busy":"2021-07-31T14:56:21.530783Z","iopub.execute_input":"2021-07-31T14:56:21.531214Z","iopub.status.idle":"2021-07-31T14:56:21.877951Z","shell.execute_reply.started":"2021-07-31T14:56:21.531176Z","shell.execute_reply":"2021-07-31T14:56:21.877002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Check performance on test","metadata":{"id":"PHvUOKR2n27g"}},{"cell_type":"code","source":"# check performance on test\nscores = model.evaluate(xtest, ytest, verbose=1) \nprint('Test loss:', scores[0])\nprint('Test accuracy:', scores[1])","metadata":{"id":"03Ie9M5Ky4JS","outputId":"02fce565-abc7-499e-d97e-233acec4ae8a","execution":{"iopub.status.busy":"2021-07-31T14:56:21.879382Z","iopub.execute_input":"2021-07-31T14:56:21.879762Z","iopub.status.idle":"2021-07-31T14:56:23.077713Z","shell.execute_reply.started":"2021-07-31T14:56:21.879720Z","shell.execute_reply":"2021-07-31T14:56:23.076548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Result: ","metadata":{"id":"072onwh75dkX"}},{"cell_type":"markdown","source":"### So the model's final Training Accuracy is 79% ","metadata":{"id":"fAU8cDUmqF3P"}},{"cell_type":"markdown","source":"### Validation Accuracy is 77% ","metadata":{"id":"dwDRP4Cxp_03"}},{"cell_type":"markdown","source":"### Testing Accuracy is 76%","metadata":{"id":"W1JCnKJRqOi-"}}]}