{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":21154,"databundleVersionId":1243559,"isSourceIdPinned":false},{"sourceType":"modelInstanceVersion","sourceId":858079,"databundleVersionId":17060681,"modelInstanceId":652211,"modelId":664161,"isSourceIdPinned":false},{"sourceType":"modelInstanceVersion","sourceId":858082,"databundleVersionId":17060698,"modelInstanceId":652214,"modelId":664164,"isSourceIdPinned":false},{"sourceType":"modelInstanceVersion","sourceId":858081,"databundleVersionId":17060695,"modelInstanceId":652213,"modelId":664163,"isSourceIdPinned":false}],"dockerImageVersionId":31328,"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":"2026-05-04T18:23:23.294596Z","iopub.execute_input":"2026-05-04T18:23:23.294883Z","iopub.status.idle":"2026-05-04T18:23:23.754385Z","shell.execute_reply.started":"2026-05-04T18:23:23.294853Z","shell.execute_reply":"2026-05-04T18:23:23.753108Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"DATA_DIR = \"/kaggle/input/competitions/tpu-getting-started/tfrecords-jpeg-224x224\"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-04T18:23:23.755844Z","iopub.execute_input":"2026-05-04T18:23:23.756456Z","iopub.status.idle":"2026-05-04T18:23:23.762760Z","shell.execute_reply.started":"2026-05-04T18:23:23.756406Z","shell.execute_reply":"2026-05-04T18:23:23.761573Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\n\nfor root, dirs, files in os.walk(\"/kaggle/input\"):\n    for file in files:\n        print(os.path.join(root, file))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-04T18:23:23.764239Z","iopub.execute_input":"2026-05-04T18:23:23.764698Z","iopub.status.idle":"2026-05-04T18:23:23.829944Z","shell.execute_reply.started":"2026-05-04T18:23:23.764652Z","shell.execute_reply":"2026-05-04T18:23:23.828638Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\n\nMODEL_PATH = \"/kaggle/input/models/natewilliams123/resnet10/keras/default/1/resnet_10epochs.keras\"\n\noriginal_dense_from_config = tf.keras.layers.Dense.from_config\noriginal_bn_from_config = tf.keras.layers.BatchNormalization.from_config\n\n@classmethod\ndef patched_dense_from_config(cls, config):\n    config.pop(\"quantization_config\", None)\n    return original_dense_from_config(config)\n\n@classmethod\ndef patched_bn_from_config(cls, config):\n    config.pop(\"renorm\", None)\n    config.pop(\"renorm_clipping\", None)\n    config.pop(\"renorm_momentum\", None)\n    return original_bn_from_config(config)\n\ntf.keras.layers.Dense.from_config = patched_dense_from_config\ntf.keras.layers.BatchNormalization.from_config = patched_bn_from_config\n\nmodel = tf.keras.models.load_model(\n    MODEL_PATH,\n    compile=False,\n    safe_mode=False\n)\n\nprint(model.input_shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-04T18:23:23.831463Z","iopub.execute_input":"2026-05-04T18:23:23.831904Z","iopub.status.idle":"2026-05-04T18:23:35.504711Z","shell.execute_reply.started":"2026-05-04T18:23:23.831857Z","shell.execute_reply":"2026-05-04T18:23:35.503412Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom pathlib import Path\n\nIMAGE_SIZE = 64\nBATCH_SIZE = 32\n\nDATA_DIR = Path(\"/kaggle/input/competitions/tpu-getting-started/tfrecords-jpeg-224x224\")\nTEST_FILENAMES = tf.io.gfile.glob(str(DATA_DIR / \"test\" / \"*.tfrec\"))\n\nprint(\"Test files:\", len(TEST_FILENAMES))\n\ndef decode_image(image_data):\n    image = tf.image.decode_jpeg(image_data, channels=3)\n    image = tf.image.resize(image, [IMAGE_SIZE, IMAGE_SIZE])\n    image = tf.cast(image, tf.float32) / 255.0\n    return image\n\ndef read_unlabeled_tfrecord(example):\n    features = {\n        \"image\": tf.io.FixedLenFeature([], tf.string),\n        \"id\": tf.io.FixedLenFeature([], tf.string),\n    }\n    example = tf.io.parse_single_example(example, features)\n    image = decode_image(example[\"image\"])\n    image_id = example[\"id\"]\n    return image, image_id\n\ndef get_test_dataset():\n    dataset = tf.data.TFRecordDataset(TEST_FILENAMES)\n    dataset = dataset.map(read_unlabeled_tfrecord, num_parallel_calls=tf.data.AUTOTUNE)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(tf.data.AUTOTUNE)\n    return dataset\n\ntest_ds = get_test_dataset()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-04T18:23:35.508116Z","iopub.execute_input":"2026-05-04T18:23:35.509862Z","iopub.status.idle":"2026-05-04T18:23:35.643794Z","shell.execute_reply.started":"2026-05-04T18:23:35.509785Z","shell.execute_reply":"2026-05-04T18:23:35.642522Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Computing predictions...\")\n\ntest_images_ds = test_ds.map(lambda image, image_id: image)\ntest_ids_ds = test_ds.map(lambda image, image_id: image_id).unbatch()\n\nprobabilities = model.predict(test_images_ds)\npredictions = np.argmax(probabilities, axis=-1)\n\ntest_ids = next(iter(test_ids_ds.batch(len(predictions)))).numpy().astype(\"U\")\n\nsubmission = np.rec.fromarrays([test_ids, predictions])\n\nnp.savetxt(\n    \"submission.csv\",\n    submission,\n    fmt=[\"%s\", \"%d\"],\n    delimiter=\",\",\n    header=\"id,label\",\n    comments=\"\"\n)\n\nprint(\"Created submission.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-04T18:23:35.645116Z","iopub.execute_input":"2026-05-04T18:23:35.645420Z","iopub.status.idle":"2026-05-04T18:24:00.359278Z","shell.execute_reply.started":"2026-05-04T18:23:35.645391Z","shell.execute_reply":"2026-05-04T18:24:00.357834Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport pandas as pd\n\nprint(os.path.exists(\"/kaggle/working/submission.csv\"))\n\nsub = pd.read_csv(\"/kaggle/working/submission.csv\")\nprint(sub.head())\nprint(sub.shape)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-04T18:24:00.360868Z","iopub.execute_input":"2026-05-04T18:24:00.361258Z","iopub.status.idle":"2026-05-04T18:24:00.378831Z","shell.execute_reply.started":"2026-05-04T18:24:00.361215Z","shell.execute_reply":"2026-05-04T18:24:00.377281Z"}},"outputs":[],"execution_count":null}]}