{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.9","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":30066,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\n#Importing Libraries\n!pip install --quiet efficientnet\n\nimport seaborn as sns\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport cv2 as cv\nimport random,warnings,math\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import optimizers,applications,Sequential,losses\nimport efficientnet.keras as efn\nfrom kaggle_datasets import KaggleDatasets\n\n\nwarnings.filterwarnings('ignore')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:22:07.402901Z","iopub.execute_input":"2025-09-19T20:22:07.403243Z","iopub.status.idle":"2025-09-19T20:22:20.398986Z","shell.execute_reply.started":"2025-09-19T20:22:07.403214Z","shell.execute_reply":"2025-09-19T20:22:20.398028Z"}},"outputs":[],"execution_count":null},{"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.experimental.TPUStrategy(tpu)\nelse:\n    strategy = tf.distribute.get_strategy() \n\nprint(\"REPLICA: \", strategy.num_replicas_in_sync)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:22:25.739761Z","iopub.execute_input":"2025-09-19T20:22:25.740135Z","iopub.status.idle":"2025-09-19T20:22:25.746368Z","shell.execute_reply.started":"2025-09-19T20:22:25.740100Z","shell.execute_reply":"2025-09-19T20:22:25.745473Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"Classes = ['pink primrose',    'hard-leaved pocket orchid', 'canterbury bells', 'sweet pea',     'wild geranium',     'tiger lily',           'moon orchid',              'bird of paradise', 'monkshood',        'globe thistle',         # 00 - 09\n           'snapdragon',       \"colt's foot\",               'king protea',      'spear thistle', 'yellow iris',       'globe-flower',         'purple coneflower',        'peruvian lily',    'balloon flower',   'giant white arum lily', # 10 - 19\n           'fire lily',        'pincushion flower',         'fritillary',       'red ginger',    'grape hyacinth',    'corn poppy',           'prince of wales feathers', 'stemless gentian', 'artichoke',        'sweet william',         # 20 - 29\n           'carnation',        'garden phlox',              'love in the mist', 'cosmos',        'alpine sea holly',  'ruby-lipped cattleya', 'cape flower',              'great masterwort', 'siam tulip',       'lenten rose',           # 30 - 39\n           'barberton daisy',  'daffodil',                  'sword lily',       'poinsettia',    'bolero deep blue',  'wallflower',           'marigold',                 'buttercup',        'daisy',            'common dandelion',      # 40 - 49\n           'petunia',          'wild pansy',                'primula',          'sunflower',     'lilac hibiscus',    'bishop of llandaff',   'gaura',                    'geranium',         'orange dahlia',    'pink-yellow dahlia',    # 50 - 59\n           'cautleya spicata', 'japanese anemone',          'black-eyed susan', 'silverbush',    'californian poppy', 'osteospermum',         'spring crocus',            'iris',             'windflower',       'tree poppy',            # 60 - 69\n           'gazania',          'azalea',                    'water lily',       'rose',          'thorn apple',       'morning glory',        'passion flower',           'lotus',            'toad lily',        'anthurium',             # 70 - 79\n           'frangipani',       'clematis',                  'hibiscus',         'columbine',     'desert-rose',       'tree mallow',          'magnolia',                 'cyclamen ',        'watercress',       'canna lily',            # 80 - 89\n           'hippeastrum ',     'bee balm',                  'pink quill',       'foxglove',      'bougainvillea',     'camellia',             'mallow',                   'mexican petunia',  'bromelia',         'blanket flower',        # 90 - 99\n           'trumpet creeper',  'blackberry lily',           'common tulip',     'wild rose']   \nlen(Classes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:22:28.707770Z","iopub.execute_input":"2025-09-19T20:22:28.708152Z","iopub.status.idle":"2025-09-19T20:22:28.719245Z","shell.execute_reply.started":"2025-09-19T20:22:28.708115Z","shell.execute_reply":"2025-09-19T20:22:28.718380Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ndata_path = KaggleDatasets().get_gcs_path()\n!gsutil ls $data_path\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:22:31.095101Z","iopub.execute_input":"2025-09-19T20:22:31.095463Z","iopub.status.idle":"2025-09-19T20:22:35.198647Z","shell.execute_reply.started":"2025-09-19T20:22:31.095429Z","shell.execute_reply":"2025-09-19T20:22:35.197593Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_path = data_path + '/tfrecords-jpeg-512x512/train/'\ntest_path = data_path + '/tfrecords-jpeg-512x512/test/'\nval_path = data_path + '/tfrecords-jpeg-512x512/val/'\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:22:39.211674Z","iopub.execute_input":"2025-09-19T20:22:39.212065Z","iopub.status.idle":"2025-09-19T20:22:39.216560Z","shell.execute_reply.started":"2025-09-19T20:22:39.212025Z","shell.execute_reply":"2025-09-19T20:22:39.215703Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_files = tf.io.gfile.glob(train_path+'*.tfrec')\ntest_files = tf.io.gfile.glob(test_path+'*.tfrec')\nval_files = tf.io.gfile.glob(val_path+'*.tfrec')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:22:42.438445Z","iopub.execute_input":"2025-09-19T20:22:42.438814Z","iopub.status.idle":"2025-09-19T20:22:42.637517Z","shell.execute_reply.started":"2025-09-19T20:22:42.438777Z","shell.execute_reply":"2025-09-19T20:22:42.636735Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"image_size = [512,512]\nimg_height = image_size[0]\nimg_width = image_size[1]\nEpochs = 20\n# channel = 3\nNUM_TEST_IMAGES = 7382\nbatch_size = 16 * strategy.num_replicas_in_sync\nAUTO = tf.data.experimental.AUTOTUNE","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:22:45.040104Z","iopub.execute_input":"2025-09-19T20:22:45.040419Z","iopub.status.idle":"2025-09-19T20:22:45.045475Z","shell.execute_reply.started":"2025-09-19T20:22:45.040392Z","shell.execute_reply":"2025-09-19T20:22:45.044343Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def _parse_image_function(example_proto):\n    Labeled_tfrec_format = {\n        'image': tf.io.FixedLenFeature([],tf.string),\n        'class' : tf.io.FixedLenFeature([],tf.int64)\n    }\n    features = tf.io.parse_single_example(example_proto, Labeled_tfrec_format)\n    image = tf.image.decode_jpeg(features['image'],3)\n#     image.set_shape([*image_size,3])\n    image = tf.cast(image,tf.float32) / 255.0\n    image = tf.reshape(image,[*image_size,3])\n\n    label = tf.cast(features['class'], tf.int32)\n#     label = tf.one_hot(label, 10)\n\n    return image, label","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:22:51.745237Z","iopub.execute_input":"2025-09-19T20:22:51.745595Z","iopub.status.idle":"2025-09-19T20:22:51.751743Z","shell.execute_reply.started":"2025-09-19T20:22:51.745560Z","shell.execute_reply":"2025-09-19T20:22:51.750673Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def _parse_unlabled_fun(example):\n    UNLABELED_TFREC_FORMAT = {\n        'image': tf.io.FixedLenFeature([],tf.string),\n        'id' : tf.io.FixedLenFeature([],tf.string)\n    }\n    example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)\n    image = tf.image.decode_jpeg(example['image'],3)\n    image = tf.cast(image,tf.float32) / 255.0\n    image = tf.reshape(image,[*image_size,3])\n    idnum = example['id']\n    \n    return image,idnum\n    \n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:22:54.342694Z","iopub.execute_input":"2025-09-19T20:22:54.343027Z","iopub.status.idle":"2025-09-19T20:22:54.348459Z","shell.execute_reply.started":"2025-09-19T20:22:54.343000Z","shell.execute_reply":"2025-09-19T20:22:54.347552Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def read_test_data(channel, channel_name):\n    dataset = tf.data.TFRecordDataset(channel)\n\n    image_feature_description = {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'id': tf.io.FixedLenFeature([], tf.int64),\n    }\n\n    dataset = dataset.map(_parse_unlabled_fun)\n    dataset = dataset.prefetch(AUTO)\n    dataset = dataset.batch(batch_size)\n    \n    return dataset","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:22:57.573294Z","iopub.execute_input":"2025-09-19T20:22:57.573605Z","iopub.status.idle":"2025-09-19T20:22:57.579082Z","shell.execute_reply.started":"2025-09-19T20:22:57.573577Z","shell.execute_reply":"2025-09-19T20:22:57.577968Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:23:00.075583Z","iopub.execute_input":"2025-09-19T20:23:00.075956Z","iopub.status.idle":"2025-09-19T20:23:00.080253Z","shell.execute_reply.started":"2025-09-19T20:23:00.075925Z","shell.execute_reply":"2025-09-19T20:23:00.079320Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def read_dataset(channel, channel_name):\n\n#     filenames = [os.path.join(channel, channel_name + '.tfrecords')]\n    dataset = tf.data.TFRecordDataset(channel)\n\n    image_feature_description = {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'label': tf.io.FixedLenFeature([], tf.int64),\n    }\n\n    dataset = dataset.map(_parse_image_function, num_parallel_calls=10)\n    dataset = dataset.prefetch(AUTO)\n#     dataset = dataset.repeat(epochs)\n    dataset = dataset.shuffle(buffer_size=10 * batch_size)\n    dataset = dataset.batch(batch_size, drop_remainder=True)\n\n    return dataset","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:23:02.781357Z","iopub.execute_input":"2025-09-19T20:23:02.781705Z","iopub.status.idle":"2025-09-19T20:23:02.787067Z","shell.execute_reply.started":"2025-09-19T20:23:02.781673Z","shell.execute_reply":"2025-09-19T20:23:02.786233Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ntrain_dataset = read_dataset(train_files, 'train')\nvalidation_dataset = read_dataset(val_files, 'validation')\n\n# def show_batch(image_batch):\n#     plt.figure(figsize=(255,255))\n#     for n in range(1):\n#         ax = plt.subplot(1, 1, n+1)\n#         plt.imshow(image_batch[n] / 255.0)\n#         plt.axis(\"off\")\n\n        \n# image_batch, label_batch = next(train_dataset.unbatch().as_numpy_iterator())\n\n# show_batch(image_batch)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:23:10.404814Z","iopub.execute_input":"2025-09-19T20:23:10.405080Z","iopub.status.idle":"2025-09-19T20:23:10.449482Z","shell.execute_reply.started":"2025-09-19T20:23:10.405054Z","shell.execute_reply":"2025-09-19T20:23:10.448659Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for img, label in train_dataset.take(1):\n    data = [img[0:16,:,:,:].numpy(),label[0:16].numpy()]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:23:11.868272Z","iopub.execute_input":"2025-09-19T20:23:11.868591Z","iopub.status.idle":"2025-09-19T20:23:13.700712Z","shell.execute_reply.started":"2025-09-19T20:23:11.868564Z","shell.execute_reply":"2025-09-19T20:23:13.699963Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data[0].shape,data[1].shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:23:15.908411Z","iopub.execute_input":"2025-09-19T20:23:15.908777Z","iopub.status.idle":"2025-09-19T20:23:15.915161Z","shell.execute_reply.started":"2025-09-19T20:23:15.908744Z","shell.execute_reply":"2025-09-19T20:23:15.914094Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_imgs, sample_lbls = next(iter(train_dataset))\nsample_imgs.shape, sample_lbls[:10].numpy()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:23:20.621056Z","iopub.execute_input":"2025-09-19T20:23:20.621424Z","iopub.status.idle":"2025-09-19T20:23:21.412558Z","shell.execute_reply.started":"2025-09-19T20:23:20.621387Z","shell.execute_reply":"2025-09-19T20:23:21.411733Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Show first 25 images with labels.\ndef plot_batch(imgs, lbls, n=25):\n    n = min(n, imgs.shape[0])\n    rows = cols = int(np.ceil(np.sqrt(n)))\n    plt.figure(figsize=(10,10))\n    for i in range(n):\n        ax = plt.subplot(rows, cols, i+1)\n        plt.imshow(imgs[i])\n        lbl = int(lbls[i])\n        title = Classes[lbl] if 0 <= lbl < len(Classes) else str(lbl)\n        plt.title(title, fontsize=9)\n        plt.axis('off')\n    plt.tight_layout()\n    plt.show()\n\nplot_batch(sample_imgs.numpy(), sample_lbls.numpy(), n=25)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:23:23.275832Z","iopub.execute_input":"2025-09-19T20:23:23.276210Z","iopub.status.idle":"2025-09-19T20:23:25.120697Z","shell.execute_reply.started":"2025-09-19T20:23:23.276177Z","shell.execute_reply":"2025-09-19T20:23:25.119308Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"num_classes = len(Classes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:23:28.524176Z","iopub.execute_input":"2025-09-19T20:23:28.524485Z","iopub.status.idle":"2025-09-19T20:23:28.528037Z","shell.execute_reply.started":"2025-09-19T20:23:28.524459Z","shell.execute_reply":"2025-09-19T20:23:28.527107Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Tiny baseline: Flatten -> Dense(softmax). Auto-detect input shape/classes.\nimport numpy as np, tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\n\n# Pick your dataset vars; adapt if names differ.\n_ds_train = globals().get('train_ds') or globals().get('train_dataset') or globals().get('training_dataset')\n_ds_val   = globals().get('val_ds')   or globals().get('validation_dataset') or globals().get('valid_dataset')\nassert _ds_train is not None and _ds_val is not None, \"Can't find train/val datasets.\"\n\nxb, yb = next(iter(_ds_train))\ninput_shape = tuple(xb.shape[1:])  # (H,W,3)\n\n# Infer num classes safely from a few batches.\nnum_classes = int(max([int(y.numpy().max()) for _, y in _ds_train.take(5)]) + 1)\n\ndef build_flatten_dense(input_shape, num_classes):\n    x = keras.Input(shape=input_shape)\n    z = layers.Flatten()(x)\n    y = layers.Dense(num_classes, activation='softmax')(z)\n    m = keras.Model(x, y, name='baseline_flatten_dense')\n    m.compile(optimizer=keras.optimizers.Adam(1e-3),\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n    return m\n\nbaseline = build_flatten_dense(input_shape, num_classes)\nbaseline.summary()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:23:30.924873Z","iopub.execute_input":"2025-09-19T20:23:30.925203Z","iopub.status.idle":"2025-09-19T20:23:33.078567Z","shell.execute_reply.started":"2025-09-19T20:23:30.925175Z","shell.execute_reply":"2025-09-19T20:23:33.077609Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tf.keras.utils.plot_model(baseline,show_shapes=True,show_layer_names=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:57:25.394527Z","iopub.execute_input":"2025-09-19T20:57:25.394888Z","iopub.status.idle":"2025-09-19T20:57:27.018859Z","shell.execute_reply.started":"2025-09-19T20:57:25.394857Z","shell.execute_reply":"2025-09-19T20:57:27.017692Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Training a few epochs; plot train/val accuracy and loss.\nearly = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=2, restore_best_weights=True)\n\nhist_base = baseline.fit(\n    _ds_train,\n    validation_data=_ds_val,\n    epochs=10,\n    verbose=1,\n    callbacks=[early]\n)\n\nimport matplotlib.pyplot as plt\nplt.figure(figsize=(6,4)); plt.plot(hist_base.history['accuracy'], label='train_acc'); \nplt.plot(hist_base.history['val_accuracy'], label='val_acc'); plt.xlabel('epoch'); plt.ylabel('acc'); plt.legend(); plt.title('Baseline Accuracy'); plt.show()\nplt.figure(figsize=(6,4)); plt.plot(hist_base.history['loss'], label='train_loss'); \nplt.plot(hist_base.history['val_loss'], label='val_loss'); plt.xlabel('epoch'); plt.ylabel('loss'); plt.legend(); plt.title('Baseline Loss'); plt.show()\n\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-19T20:34:28.788125Z","iopub.execute_input":"2025-09-19T20:34:28.788488Z","iopub.status.idle":"2025-09-19T20:47:51.061544Z","shell.execute_reply.started":"2025-09-19T20:34:28.788455Z","shell.execute_reply":"2025-09-19T20:47:51.060483Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}