{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30805,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# <font color='#F44B24'> Image Classification with CNN for CIFAR_10 Data","metadata":{}},{"cell_type":"markdown","source":"## <font color='#F44B24'> Overview\n\nIn this project, we implement a Convolutional Neural Network (CNN) for image classification using the CIFAR-10 dataset. The CIFAR-10 dataset consists of 60,000 32x32 color images in 10 different classes, with 6,000 images per class. These classes are:\n\n- **Airplane**\n- **Automobile**\n- **Bird**\n- **Cat**\n- **Deer**\n- **Dog**\n- **Frog**\n- **Horse**\n- **Ship**\n- **Truck**\n\nThe goal of this model is to classify images into one of the 10 categories. We trained a CNN model to learn patterns from these images and predict the corresponding class. The model is evaluated based on its accuracy, and the predictions are displayed along with their associated confidence levels.\n\nBy using techniques such as Convolutional Layers, Max-Pooling, and Batch Normalization, the model is able to classify images with high accuracy.\n\n### <font color='#F44B24'> Key Steps:\n1. **Data Preprocessing:** Loading and normalizing the CIFAR-10 dataset.\n2. **Model Architecture:** A deep CNN model designed for image classification.\n3. **Evaluation:** The model is evaluated on a test set to measure accuracy and prediction performance.\n\nThis project demonstrates the power of CNNs in handling image classification tasks and provides insights into the CIFAR-10 dataset.","metadata":{}},{"cell_type":"markdown","source":"## <font color='#F44B24'> Requirements","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom tensorflow.keras.datasets import cifar10\nimport cv2\nimport pandas as pd\nimport os \nimport numpy as np\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, Flatten, Input, MaxPooling2D,Dropout,BatchNormalization,Reshape\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T17:54:55.944626Z","iopub.execute_input":"2024-12-12T17:54:55.945025Z","iopub.status.idle":"2024-12-12T17:54:55.950655Z","shell.execute_reply.started":"2024-12-12T17:54:55.944989Z","shell.execute_reply":"2024-12-12T17:54:55.949579Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## <font color='#F44B24'>  Import Data","metadata":{}},{"cell_type":"code","source":"(x_train, y_train), (x_test, y_test) = cifar10.load_data()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T17:58:55.618461Z","iopub.execute_input":"2024-12-12T17:58:55.618890Z","iopub.status.idle":"2024-12-12T17:58:56.284699Z","shell.execute_reply.started":"2024-12-12T17:58:55.618857Z","shell.execute_reply":"2024-12-12T17:58:56.283638Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T17:58:56.286205Z","iopub.execute_input":"2024-12-12T17:58:56.286456Z","iopub.status.idle":"2024-12-12T17:58:56.293894Z","shell.execute_reply.started":"2024-12-12T17:58:56.286431Z","shell.execute_reply":"2024-12-12T17:58:56.292946Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## <font color='#F44B24'> EDA (Explaratory Data Analysis)","metadata":{}},{"cell_type":"code","source":"labels=['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']\n\nimages = [x_train[(y_train.flatten() == i).argmax()] for i in range(10)]\n\n\nplt.figure(figsize=(12, 6))\nfor i, image in enumerate(images):\n    plt.subplot(2, 5, i + 1)\n    plt.imshow(image)\n    plt.title(labels[i])\n    plt.axis('off')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T17:58:58.399125Z","iopub.execute_input":"2024-12-12T17:58:58.399848Z","iopub.status.idle":"2024-12-12T17:58:59.042503Z","shell.execute_reply.started":"2024-12-12T17:58:58.399814Z","shell.execute_reply":"2024-12-12T17:58:59.041596Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## <font color='#F44B24'> Preprocess Data","metadata":{}},{"cell_type":"code","source":"x_train=x_train/255.0\nx_test=x_test/255.0\nx_train=np.array(x_train)\nx_test=np.array(x_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T17:58:59.264055Z","iopub.execute_input":"2024-12-12T17:58:59.264743Z","iopub.status.idle":"2024-12-12T17:59:00.239474Z","shell.execute_reply.started":"2024-12-12T17:58:59.264709Z","shell.execute_reply":"2024-12-12T17:59:00.238592Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_train.shape,y_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T17:59:00.240865Z","iopub.execute_input":"2024-12-12T17:59:00.241134Z","iopub.status.idle":"2024-12-12T17:59:00.247028Z","shell.execute_reply.started":"2024-12-12T17:59:00.241108Z","shell.execute_reply":"2024-12-12T17:59:00.246103Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x_test.shape,y_test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T17:59:00.766121Z","iopub.execute_input":"2024-12-12T17:59:00.766491Z","iopub.status.idle":"2024-12-12T17:59:00.772562Z","shell.execute_reply.started":"2024-12-12T17:59:00.766459Z","shell.execute_reply":"2024-12-12T17:59:00.771576Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## <font color='#F44B24'> Modelling","metadata":{}},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Input(shape=(32,32,3)))\nmodel.add(Conv2D(64,kernel_size=(3,3),activation='relu',padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\nmodel.add(Conv2D(128,kernel_size=(3,3),activation='relu',padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\nmodel.add(Conv2D(256,kernel_size=(3,3),activation='relu',padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(256,activation='relu'))\nmodel.add(Dense(128,activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10,activation='softmax'))\nmodel.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T17:59:05.786749Z","iopub.execute_input":"2024-12-12T17:59:05.787080Z","iopub.status.idle":"2024-12-12T17:59:05.957228Z","shell.execute_reply.started":"2024-12-12T17:59:05.787050Z","shell.execute_reply":"2024-12-12T17:59:05.956475Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history=model.fit(x_train,y_train,validation_data=(x_test,y_test),epochs=40,batch_size=36,verbose=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T17:59:07.164425Z","iopub.execute_input":"2024-12-12T17:59:07.164816Z","iopub.status.idle":"2024-12-12T18:04:34.301210Z","shell.execute_reply.started":"2024-12-12T17:59:07.164784Z","shell.execute_reply":"2024-12-12T18:04:34.300084Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save('cnn_model.h5')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T18:09:33.388503Z","iopub.execute_input":"2024-12-12T18:09:33.388925Z","iopub.status.idle":"2024-12-12T18:09:33.479121Z","shell.execute_reply.started":"2024-12-12T18:09:33.388892Z","shell.execute_reply":"2024-12-12T18:09:33.478033Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## <font color='#F44B24'> Model Interpretation","metadata":{}},{"cell_type":"code","source":"history.history['accuracy'][-1] # model accuracy","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T18:28:10.192310Z","iopub.execute_input":"2024-12-12T18:28:10.192681Z","iopub.status.idle":"2024-12-12T18:28:10.198572Z","shell.execute_reply.started":"2024-12-12T18:28:10.192649Z","shell.execute_reply":"2024-12-12T18:28:10.197572Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(history.history['accuracy'],label='Accuracy')\nplt.plot(history.history['val_accuracy'],label='Val_Accuracy')\nplt.legend();","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T18:54:20.619298Z","iopub.execute_input":"2024-12-12T18:54:20.620083Z","iopub.status.idle":"2024-12-12T18:54:20.905542Z","shell.execute_reply.started":"2024-12-12T18:54:20.620044Z","shell.execute_reply":"2024-12-12T18:54:20.904628Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## <font color='#F44B24'> Preparing Submission File","metadata":{}},{"cell_type":"code","source":"predictions=model.predict(x_test)\npredictions=predictions.argmax(axis=-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T18:29:30.404510Z","iopub.execute_input":"2024-12-12T18:29:30.404899Z","iopub.status.idle":"2024-12-12T18:29:31.481399Z","shell.execute_reply.started":"2024-12-12T18:29:30.404869Z","shell.execute_reply":"2024-12-12T18:29:31.480428Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions=np.array(predictions)\npredictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T18:39:39.419387Z","iopub.execute_input":"2024-12-12T18:39:39.419774Z","iopub.status.idle":"2024-12-12T18:39:39.426785Z","shell.execute_reply.started":"2024-12-12T18:39:39.419742Z","shell.execute_reply":"2024-12-12T18:39:39.425908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission=pd.DataFrame({\n    'id':range(1,len(predictions)+1),\n    'label':[labels[pred] for pred in predictions]\n})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T18:40:27.504809Z","iopub.execute_input":"2024-12-12T18:40:27.505178Z","iopub.status.idle":"2024-12-12T18:40:27.512489Z","shell.execute_reply.started":"2024-12-12T18:40:27.505146Z","shell.execute_reply":"2024-12-12T18:40:27.511547Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T18:40:28.877898Z","iopub.execute_input":"2024-12-12T18:40:28.878249Z","iopub.status.idle":"2024-12-12T18:40:28.887128Z","shell.execute_reply.started":"2024-12-12T18:40:28.878220Z","shell.execute_reply":"2024-12-12T18:40:28.886087Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T18:41:17.227797Z","iopub.execute_input":"2024-12-12T18:41:17.228657Z","iopub.status.idle":"2024-12-12T18:41:17.243720Z","shell.execute_reply.started":"2024-12-12T18:41:17.228622Z","shell.execute_reply":"2024-12-12T18:41:17.243047Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## <font color='#F44B24'> Testing the Model","metadata":{}},{"cell_type":"code","source":"random_indices = np.random.choice(len(x_test), size=10, replace=False)\nx_test_resized = x_test[random_indices]  \n\npredictions = model.predict(x_test_resized)\n\npredicted_labels = np.argmax(predictions, axis=1)\n\nactual_labels = y_test[random_indices]\n\nplt.figure(figsize=(15, 8))\nfor i, idx in enumerate(random_indices):\n    plt.subplot(2, 5, i+1)\n    plt.imshow(x_test[idx])\n    plt.title(f\"Actual: {labels[actual_labels[i][0]]}\\nPredicted: {labels[predicted_labels[i]]}\")\n    plt.axis('off')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-12-12T18:51:37.292400Z","iopub.execute_input":"2024-12-12T18:51:37.293253Z","iopub.status.idle":"2024-12-12T18:51:38.058134Z","shell.execute_reply.started":"2024-12-12T18:51:37.293219Z","shell.execute_reply":"2024-12-12T18:51:38.057092Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## <font color='#F44B24'> Results\n\nThe model was trained for **40 epochs** on the CIFAR-10 dataset, and the final results are as follows:\n\n- **Training Accuracy**: 94.42%\n- **Training Loss**: 0.1734\n- **Validation Accuracy**: 80.54%\n- **Validation Loss**: 0.9290\n\n### Observations:\n- The **training accuracy** steadily increased throughout the training process, reaching 94.42% by the final epoch.\n- The **validation accuracy** also showed improvement, but with a noticeable gap compared to the training accuracy. This suggests that the model may be overfitting to the training data.\n- The **training loss** decreased consistently, while the **validation loss** showed some fluctuations, indicating that the model could potentially benefit from further adjustments to prevent overfitting.","metadata":{}}]}