{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","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":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-04-05T11:05:47.484533Z","iopub.execute_input":"2025-04-05T11:05:47.484979Z","iopub.status.idle":"2025-04-05T11:05:47.492982Z","shell.execute_reply.started":"2025-04-05T11:05:47.484948Z","shell.execute_reply":"2025-04-05T11:05:47.491909Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## **Object Recognition in Images**","metadata":{}},{"cell_type":"markdown","source":"## Introduction","metadata":{}},{"cell_type":"markdown","source":"### Introduction\n\nIn this project, we aim to build a Convolutional Neural Network (CNN) model to classify images from the CIFAR-10 dataset, which contains 60,000 32x32 color images across 10 different categories. The categories include airplanes, automobiles, birds, cats, deer, dogs, frogs, horses, ships, and trucks. The goal is to train a deep learning model using Keras and TensorFlow to recognize these categories based on the image data. We apply data augmentation to improve the model's generalization and prevent overfitting, and use various layers such as convolution, max-pooling, dropout, and dense layers to build the CNN model. The model is trained and evaluated using a validation set, and the performance is assessed based on test accuracy and a classification report.","metadata":{}},{"cell_type":"markdown","source":"## Requirements","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport cv2\nimport random\n\nimport tensorflow as tf\nfrom keras.preprocessing import image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout, BatchNormalization\nfrom keras.utils import to_categorical\nfrom keras.applications.vgg19 import VGG19\nfrom tensorflow.keras.optimizers import RMSprop\nfrom keras.datasets import cifar10\nfrom sklearn.metrics import classification_report, confusion_matrix\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\nimport os\nimport warnings\n\nwarnings.filterwarnings('ignore')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T12:08:12.287069Z","iopub.execute_input":"2025-04-05T12:08:12.287389Z","iopub.status.idle":"2025-04-05T12:08:12.296681Z","shell.execute_reply.started":"2025-04-05T12:08:12.287365Z","shell.execute_reply":"2025-04-05T12:08:12.295621Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Data Load","metadata":{}},{"cell_type":"code","source":"(X_train, y_train), (X_test, y_test) = cifar10.load_data()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T12:15:00.315083Z","iopub.execute_input":"2025-04-05T12:15:00.315470Z","iopub.status.idle":"2025-04-05T12:15:01.309678Z","shell.execute_reply.started":"2025-04-05T12:15:00.315437Z","shell.execute_reply":"2025-04-05T12:15:01.308665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T11:05:48.471617Z","iopub.execute_input":"2025-04-05T11:05:48.471936Z","iopub.status.idle":"2025-04-05T11:05:48.477970Z","shell.execute_reply.started":"2025-04-05T11:05:48.471909Z","shell.execute_reply":"2025-04-05T11:05:48.476976Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T11:05:48.478928Z","iopub.execute_input":"2025-04-05T11:05:48.479204Z","iopub.status.idle":"2025-04-05T11:05:48.498672Z","shell.execute_reply.started":"2025-04-05T11:05:48.479178Z","shell.execute_reply":"2025-04-05T11:05:48.497353Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Set a random seed for reproducibility\nrandom.seed(42)\n\n# Select random indices\nrandom_indices = random.sample(range(len(X_train)), 100)  \n\n# Create a figure for displaying images\nplt.figure(figsize=(12, 12))\nplt.suptitle('Random Samples from Training Data', fontsize=16)\n\nfor i, idx in enumerate(random_indices):\n    plt.subplot(10, 10, i + 1)\n    plt.imshow(X_train[idx], cmap='gray' if X_train[idx].ndim == 2 else None)\n    plt.axis(\"off\")\n\n# Adjust layout to prevent overlap\nplt.tight_layout(rect=[0, 0, 1, 0.96])  # Leave space for the title\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T11:05:48.499697Z","iopub.execute_input":"2025-04-05T11:05:48.500071Z","iopub.status.idle":"2025-04-05T11:05:52.086431Z","shell.execute_reply.started":"2025-04-05T11:05:48.500041Z","shell.execute_reply":"2025-04-05T11:05:52.084867Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Preprocess the data\ndef preprocess_data(X, y):\n    X = X.astype('float32') / 255.0  # Normalize to [0, 1]\n    y = to_categorical(y, num_classes=10)  # One-hot encode labels\n    return X, y","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T12:15:07.417310Z","iopub.execute_input":"2025-04-05T12:15:07.417727Z","iopub.status.idle":"2025-04-05T12:15:07.422640Z","shell.execute_reply.started":"2025-04-05T12:15:07.417690Z","shell.execute_reply":"2025-04-05T12:15:07.421564Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load and preprocess data\nX_train, y_train = preprocess_data(X_train, y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T12:15:08.327551Z","iopub.execute_input":"2025-04-05T12:15:08.328000Z","iopub.status.idle":"2025-04-05T12:15:09.033478Z","shell.execute_reply.started":"2025-04-05T12:15:08.327964Z","shell.execute_reply":"2025-04-05T12:15:09.032342Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Training the model","metadata":{}},{"cell_type":"code","source":"# Create data generator for augmentation\ndatagen = ImageDataGenerator(\n    rotation_range=15,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    horizontal_flip=True\n)\ndatagen.fit(X_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T12:15:11.283266Z","iopub.execute_input":"2025-04-05T12:15:11.283894Z","iopub.status.idle":"2025-04-05T12:15:11.558231Z","shell.execute_reply.started":"2025-04-05T12:15:11.283828Z","shell.execute_reply":"2025-04-05T12:15:11.557079Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(64, (3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Flatten(),\n    Dropout(0.5),\n    Dense(128, activation='relu'),\n    Dense(10, activation='softmax')\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T12:15:16.404877Z","iopub.execute_input":"2025-04-05T12:15:16.405215Z","iopub.status.idle":"2025-04-05T12:15:16.458072Z","shell.execute_reply.started":"2025-04-05T12:15:16.405188Z","shell.execute_reply":"2025-04-05T12:15:16.457110Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T12:15:25.270284Z","iopub.execute_input":"2025-04-05T12:15:25.270736Z","iopub.status.idle":"2025-04-05T12:15:25.280301Z","shell.execute_reply.started":"2025-04-05T12:15:25.270699Z","shell.execute_reply":"2025-04-05T12:15:25.279257Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Create validation set\nval_size = int(0.1 * len(X_train))  # 10% for validation\ntrain_size = len(X_train) - val_size","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T12:15:35.558941Z","iopub.execute_input":"2025-04-05T12:15:35.559376Z","iopub.status.idle":"2025-04-05T12:15:35.564080Z","shell.execute_reply.started":"2025-04-05T12:15:35.559344Z","shell.execute_reply":"2025-04-05T12:15:35.562853Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the model with data augmentation\nmodel.fit(datagen.flow(X_train[:train_size], y_train[:train_size], batch_size=32), \n          epochs=10,  # Limit epochs\n          validation_data=(X_train[train_size:], y_train[train_size:]),\n          steps_per_epoch=len(X_train) // 32)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T12:15:43.309996Z","iopub.execute_input":"2025-04-05T12:15:43.310329Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Preprocess the test data\nX_test = X_test.astype('float32') / 255.0  # Normalize to [0, 1]\ny_test = to_categorical(y_test, num_classes=10)  # One-hot encode labels","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T12:03:47.807714Z","iopub.execute_input":"2025-04-05T12:03:47.808072Z","iopub.status.idle":"2025-04-05T12:03:47.882775Z","shell.execute_reply.started":"2025-04-05T12:03:47.808045Z","shell.execute_reply":"2025-04-05T12:03:47.881903Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluate the model\nloss, accuracy = model.evaluate(X_test, y_test, verbose=0)\nprint(f'Test Loss: {loss:.4f}')\nprint(f'Test Accuracy: {accuracy:.4f}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T12:03:48.850245Z","iopub.execute_input":"2025-04-05T12:03:48.850613Z","iopub.status.idle":"2025-04-05T12:04:52.654061Z","shell.execute_reply.started":"2025-04-05T12:03:48.850559Z","shell.execute_reply":"2025-04-05T12:04:52.652821Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Make predictions\ny_pred = model.predict(X_test)\ny_pred_classes = np.argmax(y_pred, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T11:06:34.608623Z","iopub.status.idle":"2025-04-05T11:06:34.609018Z","shell.execute_reply":"2025-04-05T11:06:34.608862Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Flatten the true labels for the classification report\ny_test_flat = np.argmax(y_test, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T11:06:34.610190Z","iopub.status.idle":"2025-04-05T11:06:34.610540Z","shell.execute_reply":"2025-04-05T11:06:34.610412Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Generate classification report\nreport = classification_report(y_test_flat, y_pred_classes, target_names=[\n    'airplane', 'automobile', 'bird', 'cat', 'deer', \n    'dog', 'frog', 'horse', 'ship', 'truck'\n])\nprint(\"Classification Report:\\n\", report)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T11:06:34.611414Z","iopub.status.idle":"2025-04-05T11:06:34.611798Z","shell.execute_reply":"2025-04-05T11:06:34.611659Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Generate confusion matrix\nconf_matrix = confusion_matrix(y_test_flat, y_pred_classes)\nprint(\"Confusion Matrix:\\n\", conf_matrix)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T11:06:34.612986Z","iopub.status.idle":"2025-04-05T11:06:34.613429Z","shell.execute_reply":"2025-04-05T11:06:34.613236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save('cifar10_model.h5')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-04-05T11:06:34.614463Z","iopub.status.idle":"2025-04-05T11:06:34.614939Z","shell.execute_reply":"2025-04-05T11:06:34.614748Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Conclusion\n\nThe model trained on the CIFAR-10 dataset achieved a test accuracy of approximately 67.87% and a test loss of 0.9227. While the performance is reasonable, especially considering the complexity of the dataset, the model's accuracy could be further improved. The classification report shows varying precision and recall across different classes, with some categories like airplanes and automobiles performing better, while others like cats and birds show lower performance. The confusion matrix further highlights areas where the model struggles, especially with misclassifications between visually similar classes. To improve the model's performance, future work could involve experimenting with more advanced architectures like deeper CNNs, fine-tuning hyperparameters, or employing transfer learning using pre-trained models.","metadata":{}},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}