{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"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 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\n\n# Use the kagglehub client library to attach Kaggle resources like competitions, datasets, and models to your session\n# Learn more about kagglehub: https://github.com/Kaggle/kagglehub/blob/main/README.md\n\nimport kagglehub\n# kagglehub.dataset_download('<owner>/<dataset-slug>')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:10:09.910637Z","iopub.execute_input":"2026-09-25T11:10:09.910952Z","iopub.status.idle":"2026-09-25T11:10:12.76231Z","shell.execute_reply.started":"2026-09-25T11:10:09.910924Z","shell.execute_reply":"2026-09-25T11:10:12.761274Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\n\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\n\nprint(\"TensorFlow:\", tf.__version__)\n\n# Reproducibility\nSEED = 42\nnp.random.seed(SEED)\ntf.random.set_seed(SEED)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:10:12.764306Z","iopub.execute_input":"2026-09-25T11:10:12.764769Z","iopub.status.idle":"2026-09-25T11:10:35.739284Z","shell.execute_reply.started":"2026-09-25T11:10:12.76474Z","shell.execute_reply":"2026-09-25T11:10:35.738465Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"(x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()\n\nprint(\"Training:\", x_train.shape, y_train.shape)\nprint(\"Testing:\", x_test.shape, y_test.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:10:35.740403Z","iopub.execute_input":"2026-09-25T11:10:35.740882Z","iopub.status.idle":"2026-09-25T11:36:45.699842Z","shell.execute_reply.started":"2026-09-25T11:10:35.740856Z","shell.execute_reply":"2026-09-25T11:36:45.698623Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_names = [\n    \"airplane\",\n    \"automobile\",\n    \"bird\",\n    \"cat\",\n    \"deer\",\n    \"dog\",\n    \"frog\",\n    \"horse\",\n    \"ship\",\n    \"truck\"\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:36:45.701541Z","iopub.execute_input":"2026-09-25T11:36:45.701874Z","iopub.status.idle":"2026-09-25T11:36:45.706295Z","shell.execute_reply.started":"2026-09-25T11:36:45.701846Z","shell.execute_reply":"2026-09-25T11:36:45.705448Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train = y_train.squeeze()\ny_test = y_test.squeeze()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:36:45.707876Z","iopub.execute_input":"2026-09-25T11:36:45.708328Z","iopub.status.idle":"2026-09-25T11:36:45.74648Z","shell.execute_reply.started":"2026-09-25T11:36:45.708255Z","shell.execute_reply":"2026-09-25T11:36:45.745463Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\n\nfor i in range(25):\n    plt.subplot(5, 5, i + 1)\n    plt.imshow(x_train[i])\n    plt.title(class_names[y_train[i]])\n    plt.axis(\"off\")\n\nplt.tight_layout()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:36:45.747973Z","iopub.execute_input":"2026-09-25T11:36:45.748306Z","iopub.status.idle":"2026-09-25T11:36:46.999479Z","shell.execute_reply.started":"2026-09-25T11:36:45.74827Z","shell.execute_reply":"2026-09-25T11:36:46.998349Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_augmentation = keras.Sequential([\n    layers.RandomFlip(\"horizontal\"),\n    layers.RandomTranslation(0.1, 0.1),\n], name=\"data_augmentation\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:36:47.002722Z","iopub.execute_input":"2026-09-25T11:36:47.003047Z","iopub.status.idle":"2026-09-25T11:36:47.0739Z","shell.execute_reply.started":"2026-09-25T11:36:47.003019Z","shell.execute_reply":"2026-09-25T11:36:47.07284Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mean = np.mean(x_train, axis=(0, 1, 2))\nstd = np.std(x_train, axis=(0, 1, 2))\n\nprint(\"Mean:\", mean)\nprint(\"Std:\", std)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:36:47.075068Z","iopub.execute_input":"2026-09-25T11:36:47.075537Z","iopub.status.idle":"2026-09-25T11:36:48.320942Z","shell.execute_reply.started":"2026-09-25T11:36:47.075509Z","shell.execute_reply":"2026-09-25T11:36:48.319712Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"mean = tf.constant(mean, dtype=tf.float32)\nstd = tf.constant(std, dtype=tf.float32)\n\ndef normalize(x):\n    x = tf.cast(x, tf.float32)\n    return (x - mean) / std","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:36:48.322178Z","iopub.execute_input":"2026-09-25T11:36:48.322578Z","iopub.status.idle":"2026-09-25T11:36:48.330552Z","shell.execute_reply.started":"2026-09-25T11:36:48.322547Z","shell.execute_reply":"2026-09-25T11:36:48.329533Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def residual_block(x, filters, stride=1):\n    shortcut = x\n\n    x = layers.Conv2D(\n        filters,\n        3,\n        strides=stride,\n        padding=\"same\",\n        use_bias=False,\n        kernel_initializer=\"he_normal\"\n    )(x)\n\n    x = layers.BatchNormalization()(x)\n    x = layers.ReLU()(x)\n\n    x = layers.Conv2D(\n        filters,\n        3,\n        padding=\"same\",\n        use_bias=False,\n        kernel_initializer=\"he_normal\"\n    )(x)\n\n    x = layers.BatchNormalization()(x)\n\n    # Adjust shortcut if dimensions change\n    if stride != 1 or shortcut.shape[-1] != filters:\n        shortcut = layers.Conv2D(\n            filters,\n            1,\n            strides=stride,\n            padding=\"same\",\n            use_bias=False,\n            kernel_initializer=\"he_normal\"\n        )(shortcut)\n\n        shortcut = layers.BatchNormalization()(shortcut)\n\n    x = layers.Add()([x, shortcut])\n    x = layers.ReLU()(x)\n\n    return x","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:36:48.332157Z","iopub.execute_input":"2026-09-25T11:36:48.33269Z","iopub.status.idle":"2026-09-25T11:36:48.373833Z","shell.execute_reply.started":"2026-09-25T11:36:48.332658Z","shell.execute_reply":"2026-09-25T11:36:48.371987Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inputs = keras.Input(shape=(32, 32, 3))\n\nx = data_augmentation(inputs)\nx = layers.Lambda(normalize)(x)\n\nx = layers.Conv2D(\n    64,\n    3,\n    padding=\"same\",\n    use_bias=False,\n    kernel_initializer=\"he_normal\"\n)(x)\n\nx = layers.BatchNormalization()(x)\nx = layers.ReLU()(x)\n\n# Stage 1\nx = residual_block(x, 64)\nx = residual_block(x, 64)\n\n# Stage 2\nx = residual_block(x, 128, stride=2)\nx = residual_block(x, 128)\n\n# Stage 3\nx = residual_block(x, 256, stride=2)\nx = residual_block(x, 256)\n\n# Stage 4\nx = residual_block(x, 512, stride=2)\nx = residual_block(x, 512)\n\nx = layers.GlobalAveragePooling2D()(x)\n\nx = layers.Dropout(0.3)(x)\n\noutputs = layers.Dense(\n    10,\n    activation=\"softmax\",\n    kernel_initializer=\"he_normal\"\n)(x)\n\nmodel = keras.Model(inputs, outputs)\n\nmodel.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:36:48.375492Z","iopub.execute_input":"2026-09-25T11:36:48.376167Z","iopub.status.idle":"2026-09-25T11:36:49.111528Z","shell.execute_reply.started":"2026-09-25T11:36:48.376123Z","shell.execute_reply":"2026-09-25T11:36:49.110572Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = keras.optimizers.AdamW(\n    learning_rate=1e-3,\n    weight_decay=5e-4\n)\n\nmodel.compile(\n    optimizer=optimizer,\n    loss=\"sparse_categorical_crossentropy\",\n    metrics=[\"accuracy\"]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:36:49.112806Z","iopub.execute_input":"2026-09-25T11:36:49.113184Z","iopub.status.idle":"2026-09-25T11:36:49.131744Z","shell.execute_reply.started":"2026-09-25T11:36:49.113142Z","shell.execute_reply":"2026-09-25T11:36:49.130629Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"callbacks = [\n    keras.callbacks.ReduceLROnPlateau(\n        monitor=\"val_accuracy\",\n        factor=0.5,\n        patience=5,\n        min_lr=1e-6,\n        verbose=1\n    ),\n\n    keras.callbacks.EarlyStopping(\n        monitor=\"val_accuracy\",\n        patience=15,\n        restore_best_weights=True,\n        verbose=1\n    ),\n\n    keras.callbacks.ModelCheckpoint(\n        \"best_model.keras\",\n        monitor=\"val_accuracy\",\n        save_best_only=True,\n        verbose=1\n    )\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:36:49.133473Z","iopub.execute_input":"2026-09-25T11:36:49.134006Z","iopub.status.idle":"2026-09-25T11:36:49.152099Z","shell.execute_reply.started":"2026-09-25T11:36:49.133975Z","shell.execute_reply":"2026-09-25T11:36:49.150793Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"optimizer = keras.optimizers.AdamW(\n    learning_rate=1e-3,\n    weight_decay=5e-4\n)\n\nmodel.compile(\n    optimizer=optimizer,\n    loss=\"sparse_categorical_crossentropy\",\n    metrics=[\"accuracy\"]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:36:49.153424Z","iopub.execute_input":"2026-09-25T11:36:49.153793Z","iopub.status.idle":"2026-09-25T11:36:49.1821Z","shell.execute_reply.started":"2026-09-25T11:36:49.153754Z","shell.execute_reply":"2026-09-25T11:36:49.180584Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"callbacks = [\n    keras.callbacks.ReduceLROnPlateau(\n        monitor=\"val_accuracy\",\n        factor=0.5,\n        patience=5,\n        min_lr=1e-6,\n        verbose=1\n    ),\n\n    keras.callbacks.EarlyStopping(\n        monitor=\"val_accuracy\",\n        patience=15,\n        restore_best_weights=True,\n        verbose=1\n    ),\n\n    keras.callbacks.ModelCheckpoint(\n        \"best_model.keras\",\n        monitor=\"val_accuracy\",\n        save_best_only=True,\n        verbose=1\n    )\n]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:36:49.183266Z","iopub.execute_input":"2026-09-25T11:36:49.183622Z","iopub.status.idle":"2026-09-25T11:36:49.211256Z","shell.execute_reply.started":"2026-09-25T11:36:49.183588Z","shell.execute_reply":"2026-09-25T11:36:49.209884Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    x_train,\n    y_train,\n    batch_size=128,\n    epochs=50,\n    validation_split=0.1,\n    callbacks=callbacks,\n    verbose=1\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-09-25T11:36:49.213056Z","iopub.execute_input":"2026-09-25T11:36:49.213592Z","execution_failed":"2026-09-25T23:10:07.42Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8, 5))\n\nplt.plot(history.history[\"accuracy\"], label=\"Training Accuracy\")\nplt.plot(history.history[\"val_accuracy\"], label=\"Validation Accuracy\")\n\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Accuracy\")\nplt.title(\"Training and Validation Accuracy\")\nplt.legend()\nplt.grid()\n\nplt.show()","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.42Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(8, 5))\n\nplt.plot(history.history[\"loss\"], label=\"Training Loss\")\nplt.plot(history.history[\"val_loss\"], label=\"Validation Loss\")\n\nplt.xlabel(\"Epoch\")\nplt.ylabel(\"Loss\")\nplt.title(\"Training and Validation Loss\")\nplt.legend()\nplt.grid()\n\nplt.show()","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.42Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_loss, test_accuracy = model.evaluate(\n    x_test,\n    y_test,\n    verbose=1\n)\n\nprint(f\"Official CIFAR-10 Test Accuracy: {test_accuracy:.4f}\")","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport glob\n\nfor root, dirs, files in os.walk(\"/kaggle/input\"):\n    for file in files:\n        if file.endswith(\".7z\"):\n            print(os.path.join(root, file))","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!mkdir -p /kaggle/working/test\n!7z x /kaggle/input/competitions/cifar-10/test.7z -o/kaggle/working/","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import glob\n\ntest_files = glob.glob(\"/kaggle/working/test/*.png\")\n    \nprint(len(test_files))","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_files = sorted(\n    test_files,\n    key=lambda x: int(os.path.basename(x).split(\".\")[0])\n)","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(test_files[:10])","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_image(path):\n    image = tf.io.read_file(path)\n    image = tf.image.decode_png(image, channels=3)\n    image = tf.cast(image, tf.float32)\n    image.set_shape([32, 32, 3])\n    return image\n\ntest_ds = tf.data.Dataset.from_tensor_slices(test_files)\n\ntest_ds = (\n    test_ds\n    .map(load_image, num_parallel_calls=tf.data.AUTOTUNE)\n    .batch(256)\n    .prefetch(tf.data.AUTOTUNE)\n)","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred_probs = model.predict(test_ds, verbose=1)\n\npred_classes = np.argmax(pred_probs, axis=1)\n\nprint(pred_classes.shape)","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class_names = np.array([\n    \"airplane\",\n    \"automobile\",\n    \"bird\",\n    \"cat\",\n    \"deer\",\n    \"dog\",\n    \"frog\",\n    \"horse\",\n    \"ship\",\n    \"truck\"\n])\n\npred_labels = class_names[pred_classes]","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pred_labels[:20]","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    \"id\": np.arange(1, len(pred_labels) + 1),\n    \"label\": pred_labels\n})\n\nsubmission.head()","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.shape","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv(\n    \"/kaggle/working/submission.csv\",\n    index=False\n)","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(submission.head(10))\nprint(submission.tail(10))\n\nprint(\"\\nShape:\", submission.shape)\n\nprint(\"\\nMissing values:\")\nprint(submission.isnull().sum())\n\nprint(\"\\nClasses:\")\nprint(submission[\"label\"].value_counts())","metadata":{"trusted":true,"execution":{"execution_failed":"2026-09-25T23:10:07.422Z"}},"outputs":[],"execution_count":null}]}