{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","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":31192,"isInternetEnabled":false,"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 warnings\nwarnings.filterwarnings('ignore')\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-12-02T02:24:18.943868Z","iopub.execute_input":"2025-12-02T02:24:18.944941Z","iopub.status.idle":"2025-12-02T02:24:18.95286Z","shell.execute_reply.started":"2025-12-02T02:24:18.944891Z","shell.execute_reply":"2025-12-02T02:24:18.951594Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras.datasets import cifar10\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten, Dropout\nfrom tensorflow.keras.optimizers import Adam","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T02:24:18.954574Z","iopub.execute_input":"2025-12-02T02:24:18.954933Z","iopub.status.idle":"2025-12-02T02:24:18.980781Z","shell.execute_reply.started":"2025-12-02T02:24:18.954908Z","shell.execute_reply":"2025-12-02T02:24:18.979541Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ---------------------------------------------------------\n# 1. Load CIFAR-10 dataset\n# ---------------------------------------------------------\n(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n\n# ---------------------------------------------------------\n# 2. Normalize & reshape\n# ---------------------------------------------------------\nx_train = x_train.astype(\"float32\") / 255.0\nx_test  = x_test.astype(\"float32\") / 255.0\n\ny_train = to_categorical(y_train, 10)\ny_test  = to_categorical(y_test, 10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T02:24:18.981917Z","iopub.execute_input":"2025-12-02T02:24:18.982269Z","iopub.status.idle":"2025-12-02T02:24:22.693393Z","shell.execute_reply.started":"2025-12-02T02:24:18.982236Z","shell.execute_reply":"2025-12-02T02:24:22.692456Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nos.environ[\"CUDA_VISIBLE_DEVICES\"] = \"-1\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T02:24:22.695145Z","iopub.execute_input":"2025-12-02T02:24:22.695839Z","iopub.status.idle":"2025-12-02T02:24:22.701259Z","shell.execute_reply.started":"2025-12-02T02:24:22.695807Z","shell.execute_reply":"2025-12-02T02:24:22.69952Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ---------------------------------------------------------\n# 3. Build CNN model\n# ---------------------------------------------------------\nmodel = Sequential([\n    Conv2D(32, (3,3), activation='relu', padding='same', input_shape=(32, 32, 3)),\n    MaxPooling2D((2,2)),\n    Conv2D(64, (3,3), activation='relu', padding='same'),\n    MaxPooling2D((2,2)),\n    Conv2D(128, (3,3), activation='relu', padding='same'),\n    MaxPooling2D((2,2)),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')\n])\n\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T02:24:22.704913Z","iopub.execute_input":"2025-12-02T02:24:22.705382Z","iopub.status.idle":"2025-12-02T02:24:22.849496Z","shell.execute_reply.started":"2025-12-02T02:24:22.705339Z","shell.execute_reply":"2025-12-02T02:24:22.847791Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ---------------------------------------------------------\n# 4. Compile and train\n# ---------------------------------------------------------\nmodel.compile(\n    optimizer=Adam(),\n    loss='categorical_crossentropy',\n    metrics=['accuracy']\n)\n\nhistory = model.fit(\n    x_train, y_train,\n    validation_split=0.2,\n    epochs=5,\n    batch_size=64,\n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T02:24:22.850543Z","iopub.execute_input":"2025-12-02T02:24:22.850872Z","iopub.status.idle":"2025-12-02T02:28:27.82071Z","shell.execute_reply.started":"2025-12-02T02:24:22.850845Z","shell.execute_reply":"2025-12-02T02:28:27.819447Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# 5. Evaluate on test set\n# ---------------------------------------------------------\ntest_loss, test_acc = model.evaluate(x_test, y_test, verbose=0)\nprint(\"Test accuracy:\", test_acc)\n\n# ---------------------------------------------------------\n# 6. Visualize training curves\n# ---------------------------------------------------------\nplt.figure(figsize=(12,5))\n\nplt.subplot(1,2,1)\nplt.plot(history.history['loss'], label=\"Train Loss\")\nplt.plot(history.history['val_loss'], label=\"Val Loss\")\nplt.legend()\nplt.title(\"Loss Over Time\")\n\nplt.subplot(1,2,2)\nplt.plot(history.history['accuracy'], label=\"Train Acc\")\nplt.plot(history.history['val_accuracy'], label=\"Val Acc\")\nplt.legend()\nplt.title(\"Accuracy Over Time\")\n\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T02:28:27.822763Z","iopub.execute_input":"2025-12-02T02:28:27.823096Z","iopub.status.idle":"2025-12-02T02:28:32.744151Z","shell.execute_reply.started":"2025-12-02T02:28:27.823071Z","shell.execute_reply":"2025-12-02T02:28:32.742747Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# ---------------------------------------------------------\n# 7. Visualize predictions\n# ---------------------------------------------------------\nclass_names = ['airplane','automobile','bird','cat','deer',\n               'dog','frog','horse','ship','truck']\n\npreds = model.predict(x_test[:10])\npred_labels = np.argmax(preds, axis=1)\n\nplt.figure(figsize=(12,6))\nfor i in range(10):\n    plt.subplot(2,5,i+1)\n    plt.imshow(x_test[i])\n    plt.title(f\"Pred: {class_names[pred_labels[i]]}\")\n    plt.axis('off')\n\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T02:28:32.745375Z","iopub.execute_input":"2025-12-02T02:28:32.74573Z","iopub.status.idle":"2025-12-02T02:28:34.187863Z","shell.execute_reply.started":"2025-12-02T02:28:32.745705Z","shell.execute_reply":"2025-12-02T02:28:34.186485Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions=model.predict(x_test)\npredictions=predictions.argmax(axis=-1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T02:28:34.18896Z","iopub.execute_input":"2025-12-02T02:28:34.189269Z","iopub.status.idle":"2025-12-02T02:28:39.048164Z","shell.execute_reply.started":"2025-12-02T02:28:34.189248Z","shell.execute_reply":"2025-12-02T02:28:39.047118Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\npredictions=np.array(predictions)\npredictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T02:28:39.049201Z","iopub.execute_input":"2025-12-02T02:28:39.049448Z","iopub.status.idle":"2025-12-02T02:28:39.05697Z","shell.execute_reply.started":"2025-12-02T02:28:39.049427Z","shell.execute_reply":"2025-12-02T02:28:39.055771Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels=['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T02:28:39.058092Z","iopub.execute_input":"2025-12-02T02:28:39.058642Z","iopub.status.idle":"2025-12-02T02:28:39.078567Z","shell.execute_reply.started":"2025-12-02T02:28:39.058615Z","shell.execute_reply":"2025-12-02T02:28:39.077418Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nsubmission=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":"2025-12-02T02:28:39.080246Z","iopub.execute_input":"2025-12-02T02:28:39.080615Z","iopub.status.idle":"2025-12-02T02:28:39.107889Z","shell.execute_reply.started":"2025-12-02T02:28:39.080582Z","shell.execute_reply":"2025-12-02T02:28:39.106815Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('/kaggle/working/submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T02:28:39.108932Z","iopub.execute_input":"2025-12-02T02:28:39.109205Z","iopub.status.idle":"2025-12-02T02:28:39.14573Z","shell.execute_reply.started":"2025-12-02T02:28:39.109173Z","shell.execute_reply":"2025-12-02T02:28:39.14411Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#submission.to_csv('submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-12-02T02:28:39.148836Z","iopub.execute_input":"2025-12-02T02:28:39.149129Z","iopub.status.idle":"2025-12-02T02:28:39.154484Z","shell.execute_reply.started":"2025-12-02T02:28:39.149105Z","shell.execute_reply":"2025-12-02T02:28:39.153099Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}