{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":31041,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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-05-18T19:08:06.123259Z","iopub.execute_input":"2025-05-18T19:08:06.124117Z","iopub.status.idle":"2025-05-18T19:08:06.131643Z","shell.execute_reply.started":"2025-05-18T19:08:06.124062Z","shell.execute_reply":"2025-05-18T19:08:06.131043Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.datasets import cifar10\n\n# Load the CIFAR-10 dataset\n(X_train, y_train), (X_test, y_test) = cifar10.load_data()\n\n# Print the shape of the data\nprint(\"Training data shape:\", X_train.shape)\nprint(\"Training labels shape:\", y_train.shape)\nprint(\"Test data shape:\", X_test.shape)\nprint(\"Test labels shape:\", y_test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-18T19:08:06.132957Z","iopub.execute_input":"2025-05-18T19:08:06.133176Z","iopub.status.idle":"2025-05-18T19:08:08.567128Z","shell.execute_reply.started":"2025-05-18T19:08:06.133160Z","shell.execute_reply":"2025-05-18T19:08:08.566462Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Define class names for CIFAR-10\nclass_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']\n\n# Plot 25 sample images\nplt.figure(figsize=(10,10))\nfor i in range(25):\n    plt.subplot(5,5,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(X_train[i])\n    plt.xlabel(class_names[y_train[i][0]])\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-18T19:08:08.567785Z","iopub.execute_input":"2025-05-18T19:08:08.568054Z","iopub.status.idle":"2025-05-18T19:08:09.245841Z","shell.execute_reply.started":"2025-05-18T19:08:08.568037Z","shell.execute_reply":"2025-05-18T19:08:09.245130Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Normalize pixel values\nX_train = X_train.astype('float32') / 255.0\nX_test = X_test.astype('float32') / 255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-18T19:08:09.246616Z","iopub.execute_input":"2025-05-18T19:08:09.246819Z","iopub.status.idle":"2025-05-18T19:08:09.647928Z","shell.execute_reply.started":"2025-05-18T19:08:09.246804Z","shell.execute_reply":"2025-05-18T19:08:09.647098Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Create a data augmentation generator\ndatagen = ImageDataGenerator(\n    rotation_range=10,\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-05-18T19:08:09.650222Z","iopub.execute_input":"2025-05-18T19:08:09.650506Z","iopub.status.idle":"2025-05-18T19:08:09.865423Z","shell.execute_reply.started":"2025-05-18T19:08:09.650487Z","shell.execute_reply":"2025-05-18T19:08:09.864620Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout\n\n# Build the CNN model\nmodel = Sequential([\n    Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)),\n    MaxPooling2D((2,2)),\n    Conv2D(64, (3,3), activation='relu'),\n    MaxPooling2D((2,2)),\n    Conv2D(64, (3,3), activation='relu'),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')\n])\n\n# Compile the model\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\n# Display the model summary\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-02T20:35:01.449283Z","iopub.execute_input":"2025-06-02T20:35:01.449571Z","iopub.status.idle":"2025-06-02T20:35:19.371921Z","shell.execute_reply.started":"2025-06-02T20:35:01.449546Z","shell.execute_reply":"2025-06-02T20:35:19.371321Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(datagen.flow(X_train, y_train, batch_size=64),\n                    epochs=20,\n                    validation_data=(X_test, y_test))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-18T19:09:50.125737Z","iopub.execute_input":"2025-05-18T19:09:50.126436Z","iopub.status.idle":"2025-05-18T19:17:55.239632Z","shell.execute_reply.started":"2025-05-18T19:09:50.126414Z","shell.execute_reply":"2025-05-18T19:17:55.239019Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Plot training and validation accuracy\nplt.figure(figsize=(12,4))\nplt.subplot(1,2,1)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Accuracy over Epochs')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\n\n# Plot training and validation loss\nplt.subplot(1,2,2)\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Loss over Epochs')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-18T19:09:20.573582Z","iopub.status.idle":"2025-05-18T19:09:20.573827Z","shell.execute_reply.started":"2025-05-18T19:09:20.573717Z","shell.execute_reply":"2025-05-18T19:09:20.573728Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluate the model on the test set\ntest_loss, test_accuracy = model.evaluate(X_test, y_test)\nprint(f\"Test accuracy: {test_accuracy:.4f}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-18T19:09:20.575027Z","iopub.status.idle":"2025-05-18T19:09:20.575278Z","shell.execute_reply.started":"2025-05-18T19:09:20.575156Z","shell.execute_reply":"2025-05-18T19:09:20.575170Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Make predictions on test set\npredictions = model.predict(X_test)\npredicted_classes = predictions.argmax(axis=1)\n\n# Plot some test images with predicted and true labels\nplt.figure(figsize=(10,10))\nfor i in range(25):\n    plt.subplot(5,5,i+1)\n    plt.xticks([])\n    plt.yticks([])\n    plt.grid(False)\n    plt.imshow(X_test[i])\n    true_label = class_names[y_test[i][0]]\n    pred_label = class_names[predicted_classes[i]]\n    color = 'green' if true_label == pred_label else 'red'\n    plt.xlabel(f\"{pred_label} ({true_label})\", color=color)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-18T19:09:20.576391Z","iopub.status.idle":"2025-05-18T19:09:20.576707Z","shell.execute_reply.started":"2025-05-18T19:09:20.576551Z","shell.execute_reply":"2025-05-18T19:09:20.576566Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport seaborn as sns\n\n# Compute confusion matrix\ncm = confusion_matrix(y_test, predicted_classes)\n\n# Plot confusion matrix\nplt.figure(figsize=(10,8))\nsns.heatmap(cm, annot=True, fmt='d', xticklabels=class_names, yticklabels=class_names)\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-18T19:09:20.577778Z","iopub.status.idle":"2025-05-18T19:09:20.578110Z","shell.execute_reply.started":"2025-05-18T19:09:20.577950Z","shell.execute_reply":"2025-05-18T19:09:20.577964Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.save('/kaggle/working/cifar10_model.h5')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-18T19:09:20.579386Z","iopub.status.idle":"2025-05-18T19:09:20.580225Z","shell.execute_reply.started":"2025-05-18T19:09:20.580053Z","shell.execute_reply":"2025-05-18T19:09:20.580071Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import ResNet50\nfrom tensorflow.keras.layers import GlobalAveragePooling2D\n\nbase_model = ResNet50(weights='imagenet', include_top=False, input_shape=(32,32,3))\nbase_model.trainable = False  # Freeze pre-trained layers\n\nmodel = Sequential([\n    base_model,\n    GlobalAveragePooling2D(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-18T19:34:41.988304Z","iopub.execute_input":"2025-05-18T19:34:41.988633Z","iopub.status.idle":"2025-05-18T19:34:43.016260Z","shell.execute_reply.started":"2025-05-18T19:34:41.988602Z","shell.execute_reply":"2025-05-18T19:34:43.015704Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.datasets import cifar10\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport matplotlib.pyplot as plt\n\n# Load the CIFAR-10 dataset\n(X_train, y_train), (X_test, y_test) = cifar10.load_data()\n\n# Normalize pixel values\nX_train = X_train.astype('float32') / 255.0\nX_test = X_test.astype('float32') / 255.0\n\n# Data augmentation\ndatagen = ImageDataGenerator(rotation_range=10, width_shift_range=0.1, height_shift_range=0.1, horizontal_flip=True)\ndatagen.fit(X_train)\n\n# Define class names for CIFAR-10\nclass_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']\n\n# Build the baseline CNN model\nmodel = Sequential([\n    Conv2D(32, (7,7), activation='relu', input_shape=(32,32,3)),\n    MaxPooling2D((2,2)),\n    Conv2D(64, (3,3), activation='relu'),\n    MaxPooling2D((2,2)),\n    Conv2D(64, (3,3), activation='relu'),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')\n])\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model\nhistory = model.fit(datagen.flow(X_train, y_train, batch_size=64), epochs=10, validation_data=(X_test, y_test))\n\n# Evaluate the model\ntest_loss, test_accuracy = model.evaluate(X_test, y_test)\nprint(f\"Baseline Test Accuracy: {test_accuracy:.4f}\")\n\n# Plot training and validation accuracy/loss\nplt.figure(figsize=(12,4))\nplt.subplot(1,2,1)\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Accuracy over Epochs')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\n\nplt.subplot(1,2,2)\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Loss over Epochs')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-18T19:35:40.789717Z","iopub.execute_input":"2025-05-18T19:35:40.790422Z","iopub.status.idle":"2025-05-18T19:40:04.298423Z","shell.execute_reply.started":"2025-05-18T19:35:40.790398Z","shell.execute_reply":"2025-05-18T19:40:04.297624Z"}},"outputs":[],"execution_count":null}]}