{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":21154,"databundleVersionId":1243559,"sourceType":"competition"}],"dockerImageVersionId":30827,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<div style=\"text-align: center; margin: 40px 0; padding: 30px; border-radius: 10px; background-color: #f0f4f8; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);\">\n    <h1 style=\"color: #2c3e50; font-size: 42px;\">Image Classification with TPU | EDA | Transfer Learning | Deep Learning</h1>\n    <h3 style=\"color: #4caf50; font-size: 26px; margin-bottom: 10px;\">🚀 High-Performance Image Classification Using TPU</h3>\n    <p style=\"font-size: 18px; color: #555;\">\n        This notebook demonstrates an end-to-end machine learning pipeline for large-scale image classification.\n        We'll explore TPU acceleration, transfer learning with VGG16/ResNet50, and optimized data processing.\n    </p>\n    <p style=\"font-size: 20px; color: #e67e22;\">\n        If you find this implementation helpful for your work,\n        <strong>please consider upvoting! 🙌</strong>\n    </p>\n    <p style=\"color: #34495e;\">Your support helps in creating more detailed technical content! ❤️</p>\n</div>","metadata":{"_uuid":"a17afa15-49a7-418b-8a06-1378e73bb5e4","_cell_guid":"329efe41-4b69-40cd-8f08-9803397d656d","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"markdown","source":"# Code Overview and Structure\n\nThis notebook implements a complete machine learning pipeline for image classification using TensorFlow. Here's what each section covers:\n\n## 1.Initial Setup\nThe \"Imports\" section brings in essential libraries:\n- NumPy for linear algebra operations\n- Pandas for data processing\n- TensorFlow for machine learning operations\n- Kaggle utilities for dataset access\n\n## 2.Hardware and Data Configuration\n- \"Detecting tpu\" configures TPU acceleration if available\n- \"Getting data path\" sets up access to the TPU-getting-started dataset\n- \"Set some Parameters\" configures training hyperparameters\n\n## 3.Data Pipeline\nThe TensorFlow data processing pipeline includes:\n- Loading and processing TFRecord files\n- Image decoding and normalization\n- Dataset optimization with batching and caching\n\n## 4.Dataset Processing Sections\nThree main dataset pipelines are configured:\n- Training data with shuffling and repeating\n- Validation data with caching\n- Test data with ID tracking\n\n## 5.Model Development\nThe final sections cover:\n- Model architecture building\n- Compilation and training\n- Test data processing\n- Submission file generation","metadata":{"_uuid":"adace570-563e-4689-bbc2-fd6feebfe6e4","_cell_guid":"b6703487-e637-486e-b3a1-20cdb93ac710","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"markdown","source":"# Imports","metadata":{"_uuid":"6d13e590-ccdf-4b09-a7e9-086963323b08","_cell_guid":"6158cc94-dab5-47c8-b82a-9617c9e2c5b9","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport tensorflow as tf\nfrom kaggle_datasets import KaggleDatasets","metadata":{"_uuid":"d8a720f7-3e58-419c-b89e-6ac97777e77b","_cell_guid":"9fac39e8-0b93-442b-bf5a-2bee89a70053","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-07T14:48:06.973390Z","iopub.execute_input":"2025-01-07T14:48:06.973732Z","iopub.status.idle":"2025-01-07T14:48:15.984233Z","shell.execute_reply.started":"2025-01-07T14:48:06.973702Z","shell.execute_reply":"2025-01-07T14:48:15.983553Z"}},"outputs":[],"execution_count":null},{"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# 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":"bf247a50-ab1e-4bed-a822-ebee3cae6ef0","_cell_guid":"b4948894-6c42-4c0c-b18c-a630949fac65","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-07T14:48:41.295970Z","iopub.execute_input":"2025-01-07T14:48:41.296540Z","iopub.status.idle":"2025-01-07T14:48:41.488733Z","shell.execute_reply.started":"2025-01-07T14:48:41.296513Z","shell.execute_reply":"2025-01-07T14:48:41.487926Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Detecting tpu","metadata":{"_uuid":"20599f29-67e5-44ab-8d59-f3e150d0abe6","_cell_guid":"42180bf1-d9b1-451e-b532-92fb487673f7","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print('Running on TPU ', tpu.master())\nexcept ValueError:\n    tpu = None\n\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.TPUStrategy(tpu)\nelse:\n    strategy = tf.distribute.get_strategy()\n\nprint(\"REPLICAS: \", strategy.num_replicas_in_sync)","metadata":{"_uuid":"9a86d7bc-0400-40d5-9dff-4971e4390c51","_cell_guid":"f682da8a-907b-4def-873c-ade2a450d67f","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-07T14:48:41.489644Z","iopub.execute_input":"2025-01-07T14:48:41.489883Z","iopub.status.idle":"2025-01-07T14:48:41.495940Z","shell.execute_reply.started":"2025-01-07T14:48:41.489852Z","shell.execute_reply":"2025-01-07T14:48:41.495162Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Getting data path","metadata":{"_uuid":"3a176ff7-12e8-4c8f-b975-719895a52535","_cell_guid":"d9256b27-f241-4674-81c1-6feebfb44e8e","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"gcs_path = KaggleDatasets().get_gcs_path('tpu-getting-started')","metadata":{"_uuid":"936fe084-764d-49b2-91c2-3e0a56ad6567","_cell_guid":"3a1e26ea-da7f-4a37-9202-6dab269fa7dd","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-07T14:48:41.497325Z","iopub.execute_input":"2025-01-07T14:48:41.497522Z","iopub.status.idle":"2025-01-07T14:48:41.968778Z","shell.execute_reply.started":"2025-01-07T14:48:41.497504Z","shell.execute_reply":"2025-01-07T14:48:41.968086Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Set some Parameters","metadata":{"_uuid":"f53b83b9-3c95-4b6c-8e8b-49a0ece24478","_cell_guid":"10f01b37-43a0-4506-8cc6-8d2c4af30a89","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"image_size = [192, 192]\nbatch_size = 16 * strategy.num_replicas_in_sync\nepochs = 50\n\nno_training = 12753\nno_testing = 7382\nsteps_per_epoch = no_training // batch_size","metadata":{"_uuid":"2ceaa791-c084-4b5c-a0fe-1ae454b412cf","_cell_guid":"f40af8c4-ad5b-411d-a4f3-8db73d7c7377","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-07T14:48:41.969791Z","iopub.execute_input":"2025-01-07T14:48:41.970032Z","iopub.status.idle":"2025-01-07T14:48:41.973757Z","shell.execute_reply.started":"2025-01-07T14:48:41.970000Z","shell.execute_reply":"2025-01-07T14:48:41.973062Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Loading and processing the data","metadata":{"_uuid":"120d5a62-ff99-4fa7-b575-e5fd89992927","_cell_guid":"563b5c62-395e-4f1d-a2ef-aa35b74183aa","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"markdown","source":"# TensorFlow Data Processing Pipeline\nThis notebook processes image data using TensorFlow's data pipeline for training, validation, and testing.\n- Loads TFRecord files containing image data and labels\n- Processes images: decoding, reshaping, and normalizing\n- Creates optimized datasets with batching, shuffling, and caching","metadata":{"_uuid":"fc8df591-a59f-4d0d-9076-f1d636370dc8","_cell_guid":"0bd0877d-e35b-4f61-bb45-c8a798c8b73d","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"markdown","source":"## Data Format Definitions\nDefine the structure of labeled and unlabeled TFRecord files","metadata":{"_uuid":"7b7d2a5f-d812-46d6-944f-23a6668a57a1","_cell_guid":"3d8f2671-ab0a-49ed-a481-da6903cf6d48","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"LABELED_TFREC_FORMAT = {\n    \"image\": tf.io.FixedLenFeature([], tf.string),\n    \"class\": tf.io.FixedLenFeature([], tf.int64),\n}\n\nUNLABELED_TFREC_FORMAT = {\n    \"image\": tf.io.FixedLenFeature([], tf.string),\n    \"id\": tf.io.FixedLenFeature([], tf.string),\n}","metadata":{"_uuid":"029f4227-8d0b-4e68-abd5-911bc706e1e5","_cell_guid":"8ff76d02-26dd-4261-98b5-9be6f5c211ba","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-07T14:48:41.974536Z","iopub.execute_input":"2025-01-07T14:48:41.974832Z","iopub.status.idle":"2025-01-07T14:48:41.988595Z","shell.execute_reply.started":"2025-01-07T14:48:41.974803Z","shell.execute_reply":"2025-01-07T14:48:41.988023Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Dataset Configuration\nSet up dataset options and locate TFRecord files","metadata":{"_uuid":"46135c8f-5b21-4d22-947c-0ec63a700dd4","_cell_guid":"84ba1267-6d27-4473-bed1-b2af671b2820","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"ignore_order = tf.data.Options()\nignore_order.experimental_deterministic = False  # disable order, increase speed\n\ntrain_filenames = tf.io.gfile.glob(gcs_path + '/tfrecords-jpeg-192x192/train/*.tfrec')\nval_filenames = tf.io.gfile.glob(gcs_path + '/tfrecords-jpeg-192x192/val/*.tfrec')\ntest_filenames = tf.io.gfile.glob(gcs_path + '/tfrecords-jpeg-192x192/test/*.tfrec')","metadata":{"_uuid":"2208792f-75b5-454d-ad28-00ef5345d0d5","_cell_guid":"abd6317a-2e8f-4607-9db1-6ad1a2e60f20","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-07T14:48:41.989425Z","iopub.execute_input":"2025-01-07T14:48:41.989672Z","iopub.status.idle":"2025-01-07T14:48:42.148659Z","shell.execute_reply.started":"2025-01-07T14:48:41.989643Z","shell.execute_reply":"2025-01-07T14:48:42.147840Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Training Dataset Processing\nProcess training data with shuffling and repeating for model training","metadata":{"_uuid":"74dbe6a7-81b4-44e6-b698-936cbcd37de3","_cell_guid":"33b62799-7287-4f30-8d53-9230ad4e30c9","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"train_dataset = tf.data.TFRecordDataset(train_filenames)\ntrain_dataset = train_dataset.with_options(ignore_order)\n\nparsed_train_dataset = train_dataset.map(lambda example: (\n    tf.image.decode_jpeg(tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)['image'], channels=3),\n    tf.cast(tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)['class'], tf.int32)\n))\n\nparsed_train_dataset = parsed_train_dataset.map(lambda image, label: (\n    tf.reshape(tf.cast(image, tf.float32) / 255.0, [*image_size, 3]),\n    label\n))\n\nparsed_train_dataset = parsed_train_dataset.repeat()\nparsed_train_dataset = parsed_train_dataset.shuffle(2048)\nparsed_train_dataset = parsed_train_dataset.batch(batch_size)","metadata":{"_uuid":"c62ce456-16ca-4fcd-8fe2-dbe5e198bbb5","_cell_guid":"8aa50993-ccb2-42e5-bb87-13cf1056bc86","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-07T14:48:42.149901Z","iopub.execute_input":"2025-01-07T14:48:42.150186Z","iopub.status.idle":"2025-01-07T14:48:43.101502Z","shell.execute_reply.started":"2025-01-07T14:48:42.150157Z","shell.execute_reply":"2025-01-07T14:48:43.100861Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Validation Dataset Processing\nProcess validation data with caching for efficient evaluation","metadata":{"_uuid":"f728706c-e10d-495f-b0bd-02f6778a8529","_cell_guid":"478bde2d-7169-4289-984a-385ac27901cc","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"val_dataset = tf.data.TFRecordDataset(val_filenames)\nval_dataset = val_dataset.with_options(ignore_order)\n\nparsed_val_dataset = val_dataset.map(lambda example: (\n    tf.image.decode_jpeg(tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)['image'], channels=3),\n    tf.cast(tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)['class'], tf.int32)\n))\n\nparsed_val_dataset = parsed_val_dataset.map(lambda image, label: (\n    tf.reshape(tf.cast(image, tf.float32) / 255.0, [*image_size, 3]),\n    label\n))\n\nparsed_val_dataset = parsed_val_dataset.batch(batch_size)\nparsed_val_dataset = parsed_val_dataset.cache()","metadata":{"_uuid":"6c409f86-5f5e-414d-b91c-f6a60a674526","_cell_guid":"2c426b50-38d0-4230-8ae6-0f1d2bb6225c","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-07T14:48:43.103317Z","iopub.execute_input":"2025-01-07T14:48:43.103526Z","iopub.status.idle":"2025-01-07T14:48:43.142332Z","shell.execute_reply.started":"2025-01-07T14:48:43.103508Z","shell.execute_reply":"2025-01-07T14:48:43.141787Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Test Dataset Processing\nProcess test data with unique IDs instead of labels","metadata":{"_uuid":"cacbf499-bb5e-4f4a-a05d-f69ec13db835","_cell_guid":"85f67640-c065-451d-975e-79e9cfa1ff50","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"test_dataset = tf.data.TFRecordDataset(test_filenames)\ntest_dataset = test_dataset.with_options(ignore_order)\n\nparsed_test_dataset = test_dataset.map(lambda example: (\n    tf.image.decode_jpeg(tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)['image'], channels=3),\n    tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)['id']\n))\n\nparsed_test_dataset = parsed_test_dataset.map(lambda image, idnum: (\n    tf.reshape(tf.cast(image, tf.float32) / 255.0, [*image_size, 3]),\n    idnum\n))\n\nparsed_test_dataset = parsed_test_dataset.batch(batch_size)","metadata":{"_uuid":"ce2e0248-c07f-4261-871f-c42eb4608b62","_cell_guid":"f77ff8c8-42b0-4b46-bbae-4ce4de10598b","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-07T14:48:43.143234Z","iopub.execute_input":"2025-01-07T14:48:43.143519Z","iopub.status.idle":"2025-01-07T14:48:43.213522Z","shell.execute_reply.started":"2025-01-07T14:48:43.143497Z","shell.execute_reply":"2025-01-07T14:48:43.212878Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Model building","metadata":{"_uuid":"51e38462-0f14-4565-ac26-4d78f7da3017","_cell_guid":"3eeaa148-977b-4ebb-82b7-318904b47c32","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"with strategy.scope():\n    # model using ResNet50\n    model = tf.keras.applications.ResNet50(\n        include_top=True,\n        weights=None,\n        input_shape=[*image_size, 3],\n        classes=104\n    )\n    # model using VGG16\n    model_pre = tf.keras.applications.VGG16(weights='imagenet', include_top=False, input_shape=[*image_size, 3])\n    model_pre.trainable = False\n    model2=tf.keras.models.Sequential([\n        model_pre,\n        tf.keras.layers.GlobalAveragePooling2D(),\n        tf.keras.layers.Dense(104, activation='softmax')\n    ])","metadata":{"_uuid":"f764f07b-a336-4853-b3f9-881a113c942f","_cell_guid":"f734cb86-9785-4235-9be3-b784c608eba4","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-07T14:48:43.214228Z","iopub.execute_input":"2025-01-07T14:48:43.214431Z","iopub.status.idle":"2025-01-07T14:48:44.775491Z","shell.execute_reply.started":"2025-01-07T14:48:43.214412Z","shell.execute_reply":"2025-01-07T14:48:44.774572Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Compiling","metadata":{"_uuid":"f19c9c36-26bc-491a-a7c9-32353ead83ff","_cell_guid":"f92e9403-b779-481d-aecf-c36f787e2e38","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"    model2.compile(\n        optimizer='adam',\n        loss='sparse_categorical_crossentropy',\n        metrics=['sparse_categorical_accuracy']\n    )","metadata":{"_uuid":"52c2f589-51f5-4f8e-9faf-777ab5793f0c","_cell_guid":"402fc4fd-d90e-4911-8d0c-52434d405eb0","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-07T14:48:44.776377Z","iopub.execute_input":"2025-01-07T14:48:44.776698Z","iopub.status.idle":"2025-01-07T14:48:44.788787Z","shell.execute_reply.started":"2025-01-07T14:48:44.776668Z","shell.execute_reply":"2025-01-07T14:48:44.788061Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Fitting model","metadata":{"_uuid":"8976e312-012b-4129-b965-341c883cd76d","_cell_guid":"09c33a67-212d-4368-93c5-33f10d2563a9","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"history = model2.fit(\n    parsed_train_dataset,\n    validation_data=parsed_val_dataset,\n    steps_per_epoch=steps_per_epoch,\n    epochs=epochs\n)","metadata":{"_uuid":"30af5eb6-236a-4b32-b018-692039535bb0","_cell_guid":"ef80fc94-f893-4ce6-a858-5b054a353715","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-07T14:48:44.789715Z","iopub.execute_input":"2025-01-07T14:48:44.789912Z","iopub.status.idle":"2025-01-07T15:30:54.231107Z","shell.execute_reply.started":"2025-01-07T14:48:44.789894Z","shell.execute_reply":"2025-01-07T15:30:54.230401Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Computing dataset with test deta","metadata":{"_uuid":"21e8964f-bcc2-4018-ad56-fff3194f3151","_cell_guid":"0dc1fe42-f7ff-494e-85c5-40186be3ec24","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"ordered_test_dataset = tf.data.TFRecordDataset(test_filenames)\nordered_test_dataset = ordered_test_dataset.map(lambda example: (\n    tf.image.decode_jpeg(tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)['image'], channels=3),\n    tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)['id']\n))\n\nordered_test_dataset = ordered_test_dataset.map(lambda image, idnum: (\n    tf.reshape(tf.cast(image, tf.float32) / 255.0, [*image_size, 3]),\n    idnum\n))\n\nordered_test_dataset = ordered_test_dataset.batch(batch_size)","metadata":{"_uuid":"5ae5c4a4-0fba-4dc8-a8bd-e7466b89e953","_cell_guid":"08fc71b5-40b2-4df6-ad5a-ed2952499b4e","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-07T15:30:54.232043Z","iopub.execute_input":"2025-01-07T15:30:54.232282Z","iopub.status.idle":"2025-01-07T15:30:54.303513Z","shell.execute_reply.started":"2025-01-07T15:30:54.232261Z","shell.execute_reply":"2025-01-07T15:30:54.302904Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print('Computing predictions...')\ntest_images_ds = ordered_test_dataset.map(lambda image, idnum: image)\nprobabilities = model2.predict(test_images_ds)\npredictions = np.argmax(probabilities, axis=-1)\nprint(predictions)","metadata":{"_uuid":"ca2a71e6-1638-4974-9448-a4c25bef474e","_cell_guid":"85b46dde-9de8-40d9-9be4-b6a2e5ac5f04","trusted":true,"collapsed":false,"execution":{"iopub.status.busy":"2025-01-07T15:30:54.304329Z","iopub.execute_input":"2025-01-07T15:30:54.304657Z","iopub.status.idle":"2025-01-07T15:31:25.244563Z","shell.execute_reply.started":"2025-01-07T15:30:54.304608Z","shell.execute_reply":"2025-01-07T15:31:25.243515Z"},"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":" # Generate Submission File","metadata":{"_uuid":"340abc15-e840-4d4a-8f25-9e8f39818c75","_cell_guid":"50a7dc3f-5469-4b88-8e52-456eba724eea","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}}},{"cell_type":"code","source":"print('Generating submission.csv file...')\ntest_ids_ds = ordered_test_dataset.map(lambda image, idnum: idnum).unbatch()\ntest_ids = next(iter(test_ids_ds.batch(no_testing))).numpy().astype('U')\n\nnp.savetxt('submission.csv', \n           np.rec.fromarrays([test_ids, predictions]), \n           fmt=['%s', '%d'], \n           delimiter=',', \n           header='id,label', \n           comments='')","metadata":{"_uuid":"d4eefdb9-cacf-47cc-8b1f-53881274ad5c","_cell_guid":"44294bc8-85b8-4bbb-8d43-2f3f66c04726","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2025-01-07T15:31:25.245552Z","iopub.execute_input":"2025-01-07T15:31:25.245814Z","iopub.status.idle":"2025-01-07T15:31:31.599294Z","shell.execute_reply.started":"2025-01-07T15:31:25.245791Z","shell.execute_reply":"2025-01-07T15:31:31.598643Z"}},"outputs":[],"execution_count":null}]}