{"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"},{"sourceId":312126,"sourceType":"datasetVersion","datasetId":130727}],"dockerImageVersionId":31090,"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":{"execution_failed":"2025-09-20T11:53:39.846Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install py7zr -q\n\nimport os\nimport math\nimport random\nimport glob\nimport warnings\n\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\n\nfrom tensorflow.keras import layers, models, regularizers\nfrom tensorflow.keras.datasets import cifar10\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, LearningRateScheduler\nfrom tensorflow.keras.optimizers import AdamW\n\nwarnings.filterwarnings(\"ignore\")\n\nprint(\"TF version:\", tf.__version__)\nprint(\"GPU available:\", tf.config.list_physical_devices('GPU'))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T13:06:12.223698Z","iopub.execute_input":"2025-09-20T13:06:12.223968Z","iopub.status.idle":"2025-09-20T13:06:15.427056Z","shell.execute_reply.started":"2025-09-20T13:06:12.223945Z","shell.execute_reply":"2025-09-20T13:06:15.425939Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Reproducibility\nSEED = 1337\nrandom.seed(SEED)\nnp.random.seed(SEED)\ntf.random.set_seed(SEED)\n\n# Paths (Kaggle)\nDATA_DIR = \"/kaggle/input/cifar10-object-recognition-in-images-zip-file\"\nSAMPLE_SUB_PATH = os.path.join(DATA_DIR, \"sampleSubmission.csv\")\n\n# Candidate test directories inside the zip\nTEST_DIR_CANDIDATES = [\n    os.path.join(DATA_DIR, \"train_test\"),\n    os.path.join(DATA_DIR, \"train_test\", \"test\"),\n    os.path.join(DATA_DIR, \"train_test\", \"test\", \"test\"),\n]\n\nCKPT_PATH = \"/kaggle/working/best_model_loss.h5\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T13:06:34.290524Z","iopub.execute_input":"2025-09-20T13:06:34.290824Z","iopub.status.idle":"2025-09-20T13:06:34.340835Z","shell.execute_reply.started":"2025-09-20T13:06:34.290798Z","shell.execute_reply":"2025-09-20T13:06:34.339987Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Load CIFAR-10 from Keras\n(x_train, y_train), (x_test, y_test) = cifar10.load_data()\n\n# Normalize images\nx_train = x_train.astype(\"float32\") / 255.0\nx_test  = x_test.astype(\"float32\") / 255.0\n\n# Convert labels to one-hot\ny_train = to_categorical(y_train, 10)\ny_test  = to_categorical(y_test, 10)\n\nprint(\"Train:\", x_train.shape, y_train.shape)\nprint(\"Val/Test:\", x_test.shape, y_test.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T13:06:36.932347Z","iopub.execute_input":"2025-09-20T13:06:36.932897Z","iopub.status.idle":"2025-09-20T13:06:39.703020Z","shell.execute_reply.started":"2025-09-20T13:06:36.932872Z","shell.execute_reply":"2025-09-20T13:06:39.702315Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def conv_block(x, filters, stride=1):\n    y = layers.Conv2D(filters, 3, strides=stride, padding='same',\n                      kernel_regularizer=regularizers.l2(1e-4),\n                      use_bias=False)(x)\n    y = layers.BatchNormalization()(y)\n    return layers.Activation('relu')(y)\n\ndef residual_block(x, filters, downsample=False):\n    stride = 2 if downsample else 1\n    y = conv_block(x, filters, stride)\n    y = conv_block(y, filters)\n    if downsample or x.shape[-1] != filters:\n        x = layers.Conv2D(filters, 1, strides=stride, padding='same',\n                          kernel_regularizer=regularizers.l2(1e-4),\n                          use_bias=False)(x)\n        x = layers.BatchNormalization()(x)\n    out = layers.add([x, y])\n    return layers.Activation('relu')(out)\n\ndef build_resnet18():\n    inputs = layers.Input(shape=(32, 32, 3))\n    x = conv_block(inputs, 64)\n    x = residual_block(x, 64)\n    x = residual_block(x, 64)\n\n    x = residual_block(x, 128, downsample=True)\n    x = residual_block(x, 128)\n\n    x = residual_block(x, 256, downsample=True)\n    x = residual_block(x, 256)\n\n    x = residual_block(x, 512, downsample=True)\n    x = residual_block(x, 512)\n\n    x = layers.GlobalAveragePooling2D()(x)\n    x = layers.Dropout(0.5)(x)\n    outputs = layers.Dense(10, activation='softmax')(x)\n    return models.Model(inputs, outputs)\n\nmodel = build_resnet18()\noptimizer = AdamW(learning_rate=3e-4, weight_decay=1e-5, clipnorm=1.0)\nmodel.compile(optimizer=optimizer, loss=\"categorical_crossentropy\", metrics=[\"accuracy\"])\nmodel.summary()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T13:06:42.431827Z","iopub.execute_input":"2025-09-20T13:06:42.432608Z","iopub.status.idle":"2025-09-20T13:06:42.815201Z","shell.execute_reply.started":"2025-09-20T13:06:42.432583Z","shell.execute_reply":"2025-09-20T13:06:42.814546Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Augmentation: shifts + horizontal flip\ndatagen = ImageDataGenerator(\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    horizontal_flip=True\n)\ndatagen.fit(x_train)\n\n# Callbacks\ncheckpoint_cb = ModelCheckpoint(CKPT_PATH, monitor=\"val_loss\", save_best_only=True, verbose=1)\nearly_stop_cb  = EarlyStopping(monitor=\"val_loss\", patience=12, restore_best_weights=True, verbose=1)\n\ndef cosine_annealing(epoch):\n    max_epochs = 60\n    initial_lr = 3e-4\n    return 0.5 * initial_lr * (1 + math.cos(math.pi * epoch / max_epochs))\n\nlr_scheduler_cb = LearningRateScheduler(cosine_annealing)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T13:06:50.458753Z","iopub.execute_input":"2025-09-20T13:06:50.459035Z","iopub.status.idle":"2025-09-20T13:06:50.673262Z","shell.execute_reply.started":"2025-09-20T13:06:50.459013Z","shell.execute_reply":"2025-09-20T13:06:50.672718Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BATCH_SIZE = 128\nEPOCHS = 60\n\nif not os.path.exists(CKPT_PATH):\n    history = model.fit(\n        datagen.flow(x_train, y_train, batch_size=BATCH_SIZE),\n        validation_data=(x_test, y_test),\n        steps_per_epoch=len(x_train)//BATCH_SIZE,\n        epochs=EPOCHS,\n        callbacks=[checkpoint_cb, early_stop_cb, lr_scheduler_cb],\n        verbose=1\n    )\nelse:\n    print(\"Checkpoint found; skipping training.\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T13:06:54.963556Z","iopub.execute_input":"2025-09-20T13:06:54.964071Z","iopub.status.idle":"2025-09-20T13:06:54.969029Z","shell.execute_reply.started":"2025-09-20T13:06:54.964047Z","shell.execute_reply":"2025-09-20T13:06:54.968179Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def robust_load_model():\n    candidates = [CKPT_PATH, \"best_model_loss.h5\", \"/kaggle/working/best_model.h5\"]\n    for p in candidates:\n        if os.path.exists(p):\n            return tf.keras.models.load_model(p)\n    savedmodels = glob.glob(\"/kaggle/working/**/saved_model.pb\", recursive=True)\n    if savedmodels:\n        return tf.keras.models.load_model(os.path.dirname(savedmodels[0]))\n    raise FileNotFoundError(\"Best model checkpoint not found.\")\n\nbest_model = robust_load_model()\nprint(\"Loaded best model.\")\n\n# Numeric evaluation on CIFAR-10 test split\nloss, accuracy = best_model.evaluate(x_test, y_test, verbose=0)\nprint(f\"Test Loss: {loss:.4f}\")\nprint(f\"Test Accuracy: {accuracy:.4f}\")\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T13:06:58.468296Z","iopub.execute_input":"2025-09-20T13:06:58.468899Z","iopub.status.idle":"2025-09-20T13:07:06.290057Z","shell.execute_reply.started":"2025-09-20T13:06:58.468874Z","shell.execute_reply":"2025-09-20T13:07:06.289003Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Read sample submission\nsample = pd.read_csv(SAMPLE_SUB_PATH)\nid_list = sample[\"id\"].tolist()\n\n# Resolve image path for each ID\ndef resolve_img_path(img_id):\n    for base in TEST_DIR_CANDIDATES:\n        for ext in (\".png\", \".jpg\"):\n            p = os.path.join(base, f\"{img_id}{ext}\")\n            if os.path.exists(p):\n                return p\n    return None\n\npaths = [resolve_img_path(i) for i in id_list]\nif any(p is None for p in paths):\n    missing = sum(1 for p in paths if p is None)\n    raise FileNotFoundError(f\"{missing} test images missing.\")\n\n# Create tf.data pipeline\ndef load_tf(path):\n    img = tf.io.read_file(path)\n    img = tf.io.decode_image(img, channels=3, expand_animations=False)\n    img = tf.image.convert_image_dtype(img, tf.float32)\n    return tf.image.resize(img, [32, 32])\n\nds = tf.data.Dataset.from_tensor_slices(paths)\nds = ds.map(load_tf, num_parallel_calls=tf.data.AUTOTUNE)\nds = ds.batch(1024).prefetch(tf.data.AUTOTUNE)\n\n# Predict and assemble submission\nprobs = best_model.predict(ds, verbose=1)\npred_idx = np.argmax(probs, axis=1)\nlabel_names = [\"airplane\",\"automobile\",\"bird\",\"cat\",\"deer\",\"dog\",\"frog\",\"horse\",\"ship\",\"truck\"]\npred_labels = [label_names[i] for i in pred_idx]\n\nsubmission = pd.DataFrame({\"id\": id_list, \"label\": pred_labels})\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(\"submission.csv saved:\", submission.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T13:15:00.133119Z","iopub.execute_input":"2025-09-20T13:15:00.133421Z","iopub.status.idle":"2025-09-20T13:18:28.132173Z","shell.execute_reply.started":"2025-09-20T13:15:00.133394Z","shell.execute_reply":"2025-09-20T13:18:28.131224Z"}},"outputs":[],"execution_count":null}]}