{"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":"**Importing Libraries**","metadata":{"id":"-EVbyDLLZtyx"}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import datasets,layers,models\nimport matplotlib.pyplot as plt\nimport numpy as np","metadata":{"id":"4f9HS6yeRpdF","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(X_train,y_train),(X_test,y_test)=datasets.cifar10.load_data()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"id":"fZJsV21fSXjS","outputId":"dda24850-6fe8-464e-e1e9-044290b2d593","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"id":"D-cp0GINScZl","outputId":"cf1fe709-96ac-429b-8fcb-94c27b7ace08","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"id":"NmZBsX9qSz0Y","outputId":"db7f11fc-b6db-44b0-d227-020bc97cf616","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train[:5]","metadata":{"id":"na8Ahc5CSefy","outputId":"f5f44b04-4087-49a3-bca4-1b5875e88435","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\ny_train is a 2D array, for our classification having 1D array is good enough. so we will convert this to now 1D array","metadata":{"id":"ouzwHDRzS2jN"}},{"cell_type":"code","source":"y_train = y_train.reshape(-1,)\ny_train[:5]","metadata":{"id":"Pyiz9-AdSh3P","outputId":"d93c60e1-9b32-4068-dcee-1599920c432b","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = y_test.reshape(-1,)","metadata":{"id":"B4XMKPQ8S8Tz","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = [\"airplane\",\"automobile\",\"bird\",\"cat\",\"deer\",\"dog\",\"frog\",\"horse\",\"ship\",\"truck\"]","metadata":{"id":"VCi5HoCUS94s","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Let's plot some images to see what they are","metadata":{"id":"bL2g6FwyTBis"}},{"cell_type":"code","source":"def plot_sample(X, y, index):\n    plt.figure(figsize = (15,2))\n    plt.imshow(X[index])\n    plt.xlabel(classes[y[index]])","metadata":{"id":"GDrGy_NnS_ba","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_train, y_train, 0)","metadata":{"id":"jU8ES3XmTDrH","outputId":"ef779b9e-ae01-42ce-acbb-33d537161a88","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_train, y_train, 1)","metadata":{"id":"-nvWLs6lTH97","outputId":"3adc5042-04f3-4239-fc8e-71fed5be574c","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_train, y_train, 2)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(3,10):\n  plot_sample(X_train, y_train, i)","metadata":{"id":"8kB_TUC-TJiv","outputId":"7a27e4d5-9e7a-48e7-93f4-9f2f36b178f1","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Normalize the images to a number from 0 to 1. Image has 3 channels (R,G,B) and each value in the channel can range from 0 to 255. Hence to normalize in 0-->1 range, we need to divide it by 255","metadata":{"id":"r5AVXGO1T0hr"}},{"cell_type":"code","source":"X_train[0]","metadata":{"id":"g2GVeJnHTQFv","outputId":"865b8ed8-6f3d-4cba-de30-9702ae2b5aee","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Normalizing the training data**","metadata":{"id":"TCOKvET9UO0-"}},{"cell_type":"code","source":"X_train=X_train/255\nX_test=X_test/255","metadata":{"id":"aD5yGv2BUB47","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Build simple artificial neural network for image classification**","metadata":{"id":"pL5UksKrUjD5"}},{"cell_type":"code","source":"ann=models.Sequential([\n                      layers.Flatten(input_shape=(32,32,3)),\n                      layers.Dense(3000,activation='relu'),\n                      layers.Dense(1000,activation='relu'),\n                      layers.Dense(10,activation='softmax')\n])\n\nann.compile(optimizer='SGD',\n            loss='sparse_categorical_crossentropy',\n            metrics=['accuracy']\n            )\nann.fit(X_train,y_train,epochs=5)","metadata":{"id":"DbED30uOUJt3","outputId":"fd7dfbec-1915-4ab4-da0e-c95c3025b3b9","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**You can see that at the end of 5 epochs, accuracy is at around 49%**","metadata":{"id":"qpw1r3jkVbRz"}},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix , classification_report\nimport numpy as np\ny_pred = ann.predict(X_test)\ny_pred_classes = [np.argmax(element) for element in y_pred]\n\nprint(\"Classification Report: \\n\", classification_report(y_test, y_pred_classes))","metadata":{"id":"8jU8iZ51VP3X","outputId":"0f118c82-e37d-4eef-b20e-0903841643eb","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Now let us build a convolutional neural network to train our images**","metadata":{"id":"vm-3FWSVVjxM"}},{"cell_type":"code","source":"cnn=models.Sequential([\n                       #cnn\n                      layers.Conv2D(filters=32,kernel_size=(3,3),activation='relu',input_shape=(32,32,3)),\n                      layers.MaxPool2D((2,2)),\n                       \n                      layers.Conv2D(filters=64,kernel_size=(3,3),activation='relu',input_shape=(32,32,3)),\n                      layers.MaxPool2D((2,2)),\n\n\n                      layers.Flatten(),\n                      layers.Dense(64,activation='relu'),\n                      layers.Dense(10,activation='softmax')\n])","metadata":{"id":"3KXklBBwVgXc","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.compile(optimizer='adam',\n            loss='sparse_categorical_crossentropy',\n            metrics=['accuracy']\n            )\ncnn.fit(X_train,y_train,epochs=15)","metadata":{"id":"uJ2W9T_XXT9J","outputId":"2e9bbc51-b37b-4f1b-d455-48ac7344ebba","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**With CNN, at the end 5 epochs, accuracy was at around 70% which is a significant improvement over ANN. CNN's are best for image classification and gives superb accuracy. Also computation is much less compared to simple ANN as maxpooling reduces the image dimensions while still preserving the features**","metadata":{"id":"ekg13QYfYNMU"}},{"cell_type":"code","source":"cnn.evaluate(X_test,y_test)","metadata":{"id":"rxINoAIYXd8r","outputId":"d1a09cab-f164-49a0-92c6-72255afdcaa4","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = cnn.predict(X_test)\ny_pred[:5]","metadata":{"id":"OqD0nGtQXrSJ","outputId":"1a2f2872-a140-468d-88df-6e04a8dc1109","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_classes = [np.argmax(element) for element in y_pred]\ny_classes[:5]","metadata":{"id":"W-b0prakYTpf","outputId":"ec4e973c-8142-42da-99b7-8368fa1be83c","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test[:5]","metadata":{"id":"3wPWnH_fYWUH","outputId":"8dc2b93d-e7e3-4f8a-bf50-321e6aad1f76","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_test, y_test,3)","metadata":{"id":"h984zYXKYZGo","outputId":"b2738ea2-4fcf-40c0-c51c-db6672855be5","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes[y_classes[3]]","metadata":{"id":"TGYB083CYZ4F","outputId":"46b9bd4f-d0cb-4b17-eceb-b5805025bc99","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_sample(X_test, y_test,10)","metadata":{"id":"_J_r_9CvYlSS","outputId":"6f8ea265-f718-4c63-c988-458cab7a6845","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes[y_classes[10]]","metadata":{"id":"XoC1UKbTY_Jd","outputId":"3dc5b582-e3f9-478c-8a2c-a8baaf3bff4e","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix , classification_report\nimport numpy as np\ny_pred = cnn.predict(X_test)\ny_pred_classes = [np.argmax(element) for element in y_pred]\n\nprint(\"Classification Report: \\n\", classification_report(y_test, y_pred_classes))","metadata":{"id":"tXa49ziqZBQI","outputId":"f4e2b378-073b-4a5a-d344-a043f41c2ecb","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"id":"lAMJ16cAZh0n"},"execution_count":null,"outputs":[]}]}