{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":18278,"databundleVersionId":968043,"sourceType":"competition"}],"dockerImageVersionId":31154,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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":"2025-10-24T02:52:12.990591Z","iopub.execute_input":"2025-10-24T02:52:12.991143Z","iopub.status.idle":"2025-10-24T02:52:13.035080Z","shell.execute_reply.started":"2025-10-24T02:52:12.991123Z","shell.execute_reply":"2025-10-24T02:52:13.034358Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\n\nGCS_PATH = '/kaggle/input/flower-classification-with-tpus/tfrecords-jpeg-192x192'\nTRAINING_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/train/*.tfrec')\nVALIDATION_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/val/*.tfrec')\nTEST_FILENAMES = tf.io.gfile.glob(GCS_PATH + '/test/*.tfrec')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T02:52:13.036239Z","iopub.execute_input":"2025-10-24T02:52:13.036475Z","iopub.status.idle":"2025-10-24T02:52:13.073217Z","shell.execute_reply.started":"2025-10-24T02:52:13.036459Z","shell.execute_reply":"2025-10-24T02:52:13.072483Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"IMAGE_SIZE = [192, 192]\nAUTO = tf.data.AUTOTUNE\n\ndef decode_image(image_data):\n    image = tf.image.decode_jpeg(image_data, channels=3)\n    image = tf.image.resize(image, IMAGE_SIZE)\n    image = tf.cast(image, tf.float32) / 255.0\n    return image\n\ndef read_tfrecord(example, labeled):\n    if labeled:\n        tfrecord_format = {\n            \"image\": tf.io.FixedLenFeature([], tf.string),\n            \"class\": tf.io.FixedLenFeature([], tf.int64),\n            \"id\": tf.io.FixedLenFeature([], tf.string)\n        }\n    else:\n        tfrecord_format = {\n            \"image\": tf.io.FixedLenFeature([], tf.string),\n            \"id\": tf.io.FixedLenFeature([], tf.string)\n        }\n\n    example = tf.io.parse_single_example(example, tfrecord_format)\n    image = decode_image(example['image'])\n    idnum = example['id']\n\n    if labeled:\n        label = tf.cast(example['class'], tf.int32)\n        return image, label\n    else:\n        return image, idnum","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T02:52:13.073860Z","iopub.execute_input":"2025-10-24T02:52:13.074071Z","iopub.status.idle":"2025-10-24T02:52:13.079820Z","shell.execute_reply.started":"2025-10-24T02:52:13.074056Z","shell.execute_reply":"2025-10-24T02:52:13.079234Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_dataset(filenames, labeled=True):\n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)\n    dataset = dataset.map(lambda x: read_tfrecord(x, labeled=labeled), num_parallel_calls=AUTO)\n    return dataset\n\ndef get_dataset(filenames, labeled=True, ordered=False):\n    ignore_order = tf.data.Options()\n    if not ordered:\n        ignore_order.experimental_deterministic = False\n\n    dataset = tf.data.TFRecordDataset(filenames)\n    dataset = dataset.with_options(ignore_order)\n    dataset = dataset.map(\n        lambda x: read_tfrecord(x, labeled),\n        num_parallel_calls=AUTO\n    )\n    return dataset","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T02:52:13.081281Z","iopub.execute_input":"2025-10-24T02:52:13.081472Z","iopub.status.idle":"2025-10-24T02:52:13.109539Z","shell.execute_reply.started":"2025-10-24T02:52:13.081457Z","shell.execute_reply":"2025-10-24T02:52:13.109000Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"strategy = tf.distribute.get_strategy()\nBATCH_SIZE = 16 * strategy.num_replicas_in_sync\n\ntrain_dataset = (\n    load_dataset(TRAINING_FILENAMES, labeled=True)\n    .shuffle(2048)\n    .repeat()\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)\n\nvalid_dataset = (\n    load_dataset(VALIDATION_FILENAMES, labeled=True)\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T02:52:13.110213Z","iopub.execute_input":"2025-10-24T02:52:13.110436Z","iopub.status.idle":"2025-10-24T02:52:13.282383Z","shell.execute_reply.started":"2025-10-24T02:52:13.110419Z","shell.execute_reply":"2025-10-24T02:52:13.281803Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.applications import EfficientNetB3\nfrom tensorflow.keras import layers, models\nfrom tensorflow.keras.optimizers import Adam\n\nwith strategy.scope():\n    base_model = EfficientNetB3(weights='imagenet', include_top=False, input_shape=(192,192,3))\n    model = models.Sequential([\n        base_model,\n        layers.GlobalAveragePooling2D(),\n        layers.Dense(104, activation='softmax')  \n    ])\n\n    model.compile(optimizer=Adam(learning_rate=1e-4, weight_decay=5e-4),\n                  loss='sparse_categorical_crossentropy',\n                  metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T03:57:39.933075Z","iopub.execute_input":"2025-10-24T03:57:39.933470Z","iopub.status.idle":"2025-10-24T03:57:41.639391Z","shell.execute_reply.started":"2025-10-24T03:57:39.933449Z","shell.execute_reply":"2025-10-24T03:57:41.638751Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ReduceLROnPlateau\nreduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,\n                              patience=3, min_lr=0.0000001)\n\nhistory = model.fit(\n    train_dataset,\n    epochs=35,\n    steps_per_epoch=100,\n    validation_data=valid_dataset,\n    validation_steps=20,\n    callbacks=[reduce_lr]\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T03:57:45.575160Z","iopub.execute_input":"2025-10-24T03:57:45.575790Z","iopub.status.idle":"2025-10-24T04:06:51.231809Z","shell.execute_reply.started":"2025-10-24T03:57:45.575767Z","shell.execute_reply":"2025-10-24T04:06:51.231090Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.plot(history.history['loss'], label='train loss')\nplt.plot(history.history['val_loss'], label='val loss')\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T04:09:36.757954Z","iopub.execute_input":"2025-10-24T04:09:36.758215Z","iopub.status.idle":"2025-10-24T04:09:36.898384Z","shell.execute_reply.started":"2025-10-24T04:09:36.758198Z","shell.execute_reply":"2025-10-24T04:09:36.897712Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.plot(history.history['accuracy'], label='train accuracy')\nplt.plot(history.history['val_accuracy'], label='val accuracy')\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T04:09:41.430056Z","iopub.execute_input":"2025-10-24T04:09:41.430318Z","iopub.status.idle":"2025-10-24T04:09:41.567153Z","shell.execute_reply.started":"2025-10-24T04:09:41.430298Z","shell.execute_reply":"2025-10-24T04:09:41.566511Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_ds = get_dataset(TEST_FILENAMES, labeled=False, ordered=True)\n\ntest_ids = []\nfor _, idnum in test_ds.as_numpy_iterator():\n    test_ids.append(idnum.decode('utf-8'))\n\npredict_ds = test_ds.map(lambda image, idnum: image, num_parallel_calls=AUTO)\npredict_ds = predict_ds.batch(BATCH_SIZE).prefetch(AUTO)\n\npreds = model.predict(predict_ds, verbose=1)\npred_labels = np.argmax(preds, axis=-1)\n\nimport pandas as pd\nsubmission = pd.DataFrame({'id': test_ids, 'label': pred_labels})\nsubmission.to_csv('/kaggle/working/submission.csv', index=False)\nsubmission.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T04:09:47.206238Z","iopub.execute_input":"2025-10-24T04:09:47.206491Z","iopub.status.idle":"2025-10-24T04:10:21.890720Z","shell.execute_reply.started":"2025-10-24T04:09:47.206472Z","shell.execute_reply":"2025-10-24T04:10:21.889890Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nprint(\"submission.csv exists:\", os.path.exists('submission.csv'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T02:58:53.100196Z","iopub.execute_input":"2025-10-24T02:58:53.100569Z","iopub.status.idle":"2025-10-24T02:58:53.105037Z","shell.execute_reply.started":"2025-10-24T02:58:53.100528Z","shell.execute_reply":"2025-10-24T02:58:53.104404Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls -lh /kaggle/working/","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-24T02:58:53.105674Z","iopub.execute_input":"2025-10-24T02:58:53.105990Z","iopub.status.idle":"2025-10-24T02:58:53.418237Z","shell.execute_reply.started":"2025-10-24T02:58:53.105966Z","shell.execute_reply":"2025-10-24T02:58:53.417491Z"}},"outputs":[],"execution_count":null}]}