{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"<a href=\"https://colab.research.google.com/github/vinnichowdary/ImageClassifier/blob/main/Petal_to_Metal_Image_Classification.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>","metadata":{"id":"view-in-github"}},{"cell_type":"code","source":"from google.colab import drive\ndrive.mount('/content/drive')","metadata":{"id":"CP4w0GWYQ-Er","outputId":"67d6cd6e-397d-4e19-df54-e91831fa516f"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport re\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense, BatchNormalization, ReLU, DepthwiseConv2D, Conv2D, Input\nfrom sklearn.metrics import confusion_matrix, accuracy_score\nfrom matplotlib import pyplot as plt","metadata":{"id":"Zvm_1kjsNDJP","execution":{"iopub.status.busy":"2023-11-13T04:28:53.707347Z","iopub.execute_input":"2023-11-13T04:28:53.707652Z","iopub.status.idle":"2023-11-13T04:29:05.900067Z","shell.execute_reply.started":"2023-11-13T04:28:53.707601Z","shell.execute_reply":"2023-11-13T04:29:05.899004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMAGE_SIZE = 192\nBATCH_SIZE = 32\nGCS_DS_PATH = '/kaggle/input/tpu-getting-started'\nGCS_PATH = GCS_DS_PATH + f\"/tfrecords-jpeg-{IMAGE_SIZE}x{IMAGE_SIZE}\"\nAUTO = tf.data.experimental.AUTOTUNE\n\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\")\n\nCLASSES = ['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']","metadata":{"id":"Pt4FHQVLQyPk","execution":{"iopub.status.busy":"2023-11-13T04:29:21.252208Z","iopub.execute_input":"2023-11-13T04:29:21.253049Z","iopub.status.idle":"2023-11-13T04:29:21.297468Z","shell.execute_reply.started":"2023-11-13T04:29:21.253015Z","shell.execute_reply":"2023-11-13T04:29:21.296642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"reference from https://www.kaggle.com/code/ryanholbrook/create-your-first-submission#Step-3:-Loading-the-Competition-Data","metadata":{"id":"Lv6wDLEAIlMz"}},{"cell_type":"code","source":"def decode_image(image_data):\n    image = tf.image.decode_jpeg(image_data, channels=3)\n    image = tf.cast(image, tf.float32) / 255.0  # convert image to floats in [0, 1] range\n    image = tf.reshape(image, [IMAGE_SIZE, IMAGE_SIZE, 3]) # explicit size needed for TPU\n    return image\n\n\ndef read_labeled_tfrecord(example):\n    LABELED_TFREC_FORMAT = {\n        \"image\": tf.io.FixedLenFeature([], tf.string), # tf.string means bytestring\n        \"class\": tf.io.FixedLenFeature([], tf.int64),  # shape [] means single element\n    }\n    example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)\n    image = decode_image(example['image'])\n    label = tf.cast(example['class'], tf.int32)\n    return image, tf.one_hot(label, len(CLASSES)) # returns a dataset of (image, label) pairs\n\n\ndef read_unlabeled_tfrecord(example):\n    UNLABELED_TFREC_FORMAT = {\n        \"image\": tf.io.FixedLenFeature([], tf.string), # tf.string means bytestring\n        \"id\": tf.io.FixedLenFeature([], tf.string),  # shape [] means single element\n    }\n    example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)\n    image = decode_image(example['image'])\n    idnum = example['id']\n    return image, idnum # returns a dataset of image(s)\n\n\ndef load_dataset(filenames, labeled: bool = True, ordered: bool = False):\n    # Read from TFRecords. For optimal performance, reading from multiple files at once and\n    # disregarding data order. Order does not matter since we will be shuffling the data anyway.\n\n    ignore_order = tf.data.Options()\n    if not ordered:\n        ignore_order.experimental_deterministic = False # disable order, increase speed\n\n    dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO) # automatically interleaves reads from multiple files\n    dataset = dataset.with_options(ignore_order) # uses data as soon as it streams in, rather than in its original order\n    dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)\n    # returns a dataset of (image, label) pairs if labeled=True or (image, id) pairs if labeled=False\n    return dataset\n\n\ndef data_augment(image, label):\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.random_flip_up_down(image)\n    image = tf.image.random_saturation(image, 0, 2)\n    image = tf.image.random_brightness(image, max_delta=0.5)\n    image = tf.image.random_contrast(image, lower=0.1, upper=0.9)\n    image = tf.image.rot90(image, k=tf.random.uniform([], 0, 4, dtype=tf.int32))\n    return image, label\n\n\ndef get_training_dataset():\n    dataset = load_dataset(TRAINING_FILENAMES, labeled=True)\n    dataset = dataset.map(data_augment, num_parallel_calls=AUTO)\n    dataset = dataset.repeat() # the training dataset must repeat for several epochs\n    dataset = dataset.shuffle(2048)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO) # prefetch next batch while training (autotune prefetch buffer size)\n    return dataset\n\n\ndef get_validation_dataset(ordered=False):\n    dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=ordered)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.cache()\n    dataset = dataset.prefetch(AUTO)\n    return dataset\n\n\ndef get_test_dataset(ordered=False):\n    dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)\n    dataset = dataset.batch(BATCH_SIZE)\n    dataset = dataset.prefetch(AUTO)\n    return dataset\n\n\ndef count_data_items(filenames):\n    # the number of data items is written in the name of the .tfrec\n    # files, i.e. flowers00-230.tfrec = 230 data items\n    return sum(int(re.search(r\"-([0-9]*)\\.\", x).group(1)) for x in filenames)\n","metadata":{"id":"t-Y07o62RCFj","execution":{"iopub.status.busy":"2023-11-13T04:29:40.443024Z","iopub.execute_input":"2023-11-13T04:29:40.443387Z","iopub.status.idle":"2023-11-13T04:29:40.461211Z","shell.execute_reply.started":"2023-11-13T04:29:40.443358Z","shell.execute_reply":"2023-11-13T04:29:40.460291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"https://www.kaggle.com/code/ryanholbrook/create-your-first-submission#Step-3:-Loading-the-Competition-Data","metadata":{"id":"8rpfw466G2jz"}},{"cell_type":"code","source":"#loading the models\ndef get_pretrained_model():\n    backbone = tf.keras.applications.MobileNetV2(  # Pretrained MobileNetV2\n        include_top=False,\n        weights=\"imagenet\",\n        input_shape=[IMAGE_SIZE, IMAGE_SIZE, 3],\n    )\n    model = tf.keras.models.Sequential(\n        [\n            backbone,\n            tf.keras.layers.GlobalAveragePooling2D(),\n            tf.keras.layers.Dense(len(CLASSES), activation=\"softmax\"),\n        ]\n    )\n    model.compile(\n        optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),\n        loss=\"categorical_crossentropy\",\n        metrics=[\"accuracy\"],\n    )\n    return model\n\ndef depthwise_separable_conv_block(inputs, pointwise_conv_filters, alpha=1.0, depth_multiplier=1, strides=1):\n    depthwise_conv_filters = int(pointwise_conv_filters * alpha)\n    x = DepthwiseConv2D((3, 3), padding='same', depth_multiplier=depth_multiplier, strides=strides)(inputs)\n    x = BatchNormalization()(x)\n    x = ReLU()(x)\n    x = Conv2D(pointwise_conv_filters, (1, 1), padding='same', strides=(1, 1))(x)\n    x = BatchNormalization()(x)\n    x = ReLU()(x)\n    return x\n\ndef create_mobilenetv2_model(input_shape, num_classes, alpha=1.0, depth_multiplier=1):\n    inputs = Input(shape=input_shape)\n    x = Conv2D(32, (3, 3), strides=(2, 2), padding='same')(inputs)\n    x = BatchNormalization()(x)\n    x = ReLU()(x)\n    x = depthwise_separable_conv_block(x, 64, alpha, depth_multiplier)\n    x = depthwise_separable_conv_block(x, 128, alpha, depth_multiplier, strides=2)\n    x = depthwise_separable_conv_block(x, 128, alpha, depth_multiplier)\n    x = GlobalAveragePooling2D()(x)\n    x = Dense(num_classes, activation='softmax')(x)\n    model = tf.keras.models.Model(inputs, x)\n    model.compile(\n        optimizer=tf.keras.optimizers.Adam(learning_rate=1e-4),\n        loss=\"categorical_crossentropy\",\n        metrics=[\"accuracy\"],\n    )\n    return model\n\ndef get_mobilenetv2_scratch_model():\n    input_shape = (IMAGE_SIZE, IMAGE_SIZE, 3)\n    num_classes = len(CLASSES)\n    alpha = 1.0\n    model = create_mobilenetv2_model(input_shape, num_classes, alpha)\n    return model\n","metadata":{"id":"1wldnh2iQ79S","execution":{"iopub.status.busy":"2023-11-13T04:29:58.269927Z","iopub.execute_input":"2023-11-13T04:29:58.270671Z","iopub.status.idle":"2023-11-13T04:29:58.282631Z","shell.execute_reply.started":"2023-11-13T04:29:58.270640Z","shell.execute_reply":"2023-11-13T04:29:58.281799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_training_images = count_data_items(TRAINING_FILENAMES)\nnum_validation_images = count_data_items(VALIDATION_FILENAMES)\nnum_test_images = count_data_items(TEST_FILENAMES)\n\nprint(f\"Number of training images:   {num_training_images:,d}.\")\nprint(f\"Number of validation images: {num_validation_images:,d}.\")\nprint(f\"Number of testing images:    {num_test_images:,d}.\")\n\ntrain_dataset = get_training_dataset()\nval_dataset = get_validation_dataset()\ntest_dataset = get_test_dataset(ordered=True)","metadata":{"id":"ooYNZvFaRCHr","outputId":"8af8ae0b-8c7a-4db6-cab2-95f390981f5a","execution":{"iopub.status.busy":"2023-11-13T04:30:04.973783Z","iopub.execute_input":"2023-11-13T04:30:04.974740Z","iopub.status.idle":"2023-11-13T04:30:05.325891Z","shell.execute_reply.started":"2023-11-13T04:30:04.974703Z","shell.execute_reply":"2023-11-13T04:30:05.325001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure, axes = plt.subplots(5, 5, figsize=(12, 12))\naxes = [y for x in axes for y in x]\n\nfor i, sample in enumerate(train_dataset.unbatch().take(25).as_numpy_iterator()):\n    axes[i].imshow(sample[0])\n\n    flower_type = CLASSES[tf.argmax(sample[1]).numpy()]\n    axes[i].set_title(flower_type)\n    axes[i].axis('off')\n","metadata":{"id":"Zf9tIH1-RCKU","outputId":"fe30b6ac-6f11-446d-c0a5-cb7462b6beb9","execution":{"iopub.status.busy":"2023-11-13T04:30:11.519557Z","iopub.execute_input":"2023-11-13T04:30:11.520408Z","iopub.status.idle":"2023-11-13T04:30:15.806250Z","shell.execute_reply.started":"2023-11-13T04:30:11.520374Z","shell.execute_reply":"2023-11-13T04:30:15.805176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Refrence from https://www.kaom/code/nickuzmenkov/petals-to-the-metal-tf-efficientnet-baselineggle.c","metadata":{"id":"5aK8IudEIw8W"}},{"cell_type":"code","source":"pretrained_model  = get_pretrained_model()\npretrained_model .summary()","metadata":{"id":"G-a_UgbbRCMr","outputId":"77c69f9d-7d76-4c9c-fada-3dc24b4b7c2c","execution":{"iopub.status.busy":"2023-11-13T04:30:25.943543Z","iopub.execute_input":"2023-11-13T04:30:25.944430Z","iopub.status.idle":"2023-11-13T04:30:28.418611Z","shell.execute_reply.started":"2023-11-13T04:30:25.944396Z","shell.execute_reply":"2023-11-13T04:30:28.417564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mobilenetv2_scratch_model   = get_mobilenetv2_scratch_model()\nmobilenetv2_scratch_model .summary()","metadata":{"id":"sK3ID_9U73vV","outputId":"8063396c-8d64-4485-abaa-8f8153bae213","execution":{"iopub.status.busy":"2023-11-13T04:30:30.562306Z","iopub.execute_input":"2023-11-13T04:30:30.562702Z","iopub.status.idle":"2023-11-13T04:30:30.830084Z","shell.execute_reply.started":"2023-11-13T04:30:30.562665Z","shell.execute_reply":"2023-11-13T04:30:30.829127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the MobileNetV2 model built from scratch\nmobilenetv2_scratch_history = mobilenetv2_scratch_model.fit(\n    get_training_dataset(),\n    validation_data=get_validation_dataset(),\n    epochs=40,\n    steps_per_epoch=num_training_images // BATCH_SIZE,\n    validation_steps=num_validation_images // BATCH_SIZE,\n    callbacks=[\n        tf.keras.callbacks.ModelCheckpoint(\n            \"mobilenetv2_scratch_model.h5\",\n            monitor=\"val_accuracy\",\n            mode=\"max\",\n            save_best_only=True,\n            save_weights_only=True,\n            verbose=1,\n        ),\n        tf.keras.callbacks.ReduceLROnPlateau(\n            monitor='val_accuracy',\n            mode='max',\n            patience=5,\n            min_lr=1e-6,\n            verbose=2,\n        ),\n    ],\n    verbose=2,\n).history","metadata":{"id":"GkXk-nn_83rt","outputId":"58c139e8-9ab5-49ad-f20b-afe1355c556b","execution":{"iopub.status.busy":"2023-11-13T04:30:41.491900Z","iopub.execute_input":"2023-11-13T04:30:41.492262Z","iopub.status.idle":"2023-11-13T04:39:53.137031Z","shell.execute_reply.started":"2023-11-13T04:30:41.492238Z","shell.execute_reply":"2023-11-13T04:39:53.136058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the pretrained MobileNetV2 model\npretrained_history = pretrained_model.fit(\n    get_training_dataset(),\n    validation_data=get_validation_dataset(),\n    epochs=40,\n    steps_per_epoch=num_training_images // BATCH_SIZE,\n    validation_steps=num_validation_images // BATCH_SIZE,\n    callbacks=[\n        tf.keras.callbacks.ModelCheckpoint(\n            \"pretrained_model.h5\",\n            monitor=\"val_accuracy\",\n            mode=\"max\",\n            save_best_only=True,\n            save_weights_only=True,\n            verbose=1,\n        ),\n        tf.keras.callbacks.ReduceLROnPlateau(\n            monitor='val_accuracy',\n            mode='max',\n            patience=5,\n            min_lr=1e-6,\n            verbose=2,\n        ),\n    ],\n    verbose=2,\n).history","metadata":{"id":"k0FY4ok18l-6","outputId":"182fb02e-97af-4601-c45e-d35df2bc702e","execution":{"iopub.status.busy":"2023-11-13T04:39:53.139126Z","iopub.execute_input":"2023-11-13T04:39:53.139492Z","iopub.status.idle":"2023-11-13T04:59:14.234764Z","shell.execute_reply.started":"2023-11-13T04:39:53.139460Z","shell.execute_reply":"2023-11-13T04:59:14.233946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the learning curves for the pretrained model\nfigure, axes = plt.subplots(1, 1, figsize=(8, 8))\nepochs = list(range(len(pretrained_history[\"loss\"])))\naxes.plot(epochs, pretrained_history[\"accuracy\"], label=\"train\")\naxes.plot(epochs, pretrained_history[\"val_accuracy\"], label=\"validation\")\naxes.set_title(\"Pretrained Model Learning Curves\")\naxes.set_xlabel(\"Epoch\")\naxes.set_ylabel(\"Accuracy\")\naxes.legend()\naxes.grid()","metadata":{"id":"6TofF6gDRTAu","outputId":"43b9e481-1470-46fb-8fb5-2eb6c371b659","execution":{"iopub.status.busy":"2023-11-13T04:59:25.696905Z","iopub.execute_input":"2023-11-13T04:59:25.697698Z","iopub.status.idle":"2023-11-13T04:59:26.061976Z","shell.execute_reply.started":"2023-11-13T04:59:25.697665Z","shell.execute_reply":"2023-11-13T04:59:26.061154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot the learning curves for the MobileNetV2 scratch model\nfigure, axes = plt.subplots(1, 1, figsize=(8, 8))\nepochs = list(range(len(mobilenetv2_scratch_history[\"loss\"])))\naxes.plot(epochs, mobilenetv2_scratch_history[\"accuracy\"], label=\"train\")\naxes.plot(epochs, mobilenetv2_scratch_history[\"val_accuracy\"], label=\"validation\")\naxes.set_title(\"MobileNetV2 Scratch Model Learning Curves\")\naxes.set_xlabel(\"Epoch\")\naxes.set_ylabel(\"Accuracy\")\naxes.legend()\naxes.grid()","metadata":{"id":"zNQIb0Lq9OdR","outputId":"3b85c7d4-14b0-4cad-ef40-3c1f3f6acb03","execution":{"iopub.status.busy":"2023-11-13T04:59:30.892894Z","iopub.execute_input":"2023-11-13T04:59:30.893633Z","iopub.status.idle":"2023-11-13T04:59:31.279282Z","shell.execute_reply.started":"2023-11-13T04:59:30.893590Z","shell.execute_reply":"2023-11-13T04:59:31.278357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"take_dataset = val_dataset.unbatch().take(25).batch(25)\npredicts = pretrained_model.predict(take_dataset, verbose=0)\n\nfigure, axes = plt.subplots(5, 5, figsize=(12, 12))\naxes = [y for x in axes for y in x]\n\nfor i, (sample, predict) in enumerate(zip(take_dataset.unbatch().as_numpy_iterator(), predicts)):\n    axes[i].imshow(sample[0])\n    true_label = tf.argmax(sample[1]).numpy()\n    predict_label = tf.argmax(predict).numpy()\n    correct = true_label == predict_label\n    axes[i].set_title(f\"{CLASSES[predict_label]} ({'correct' if correct else 'wrong'})\")\n    axes[i].axis(\"off\")","metadata":{"id":"Pg2wXhtfRTDF","outputId":"b6d5276a-2170-42b6-da2c-bbf2fdecca7d","execution":{"iopub.status.busy":"2023-11-13T04:59:36.437324Z","iopub.execute_input":"2023-11-13T04:59:36.437953Z","iopub.status.idle":"2023-11-13T04:59:40.729474Z","shell.execute_reply.started":"2023-11-13T04:59:36.437922Z","shell.execute_reply":"2023-11-13T04:59:40.727845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Reference from https://www.kaggle.com/code/nickuzmenkov/petals-to-the-metal-tf-efficientnet-baseline","metadata":{"id":"hze9CbhXI6Ov"}},{"cell_type":"code","source":"take_dataset = val_dataset.unbatch().take(25).batch(25)\npredicts = mobilenetv2_scratch_model.predict(take_dataset, verbose=0)\n\nfigure, axes = plt.subplots(5, 5, figsize=(12, 12))\naxes = [y for x in axes for y in x]\n\nfor i, (sample, predict) in enumerate(zip(take_dataset.unbatch().as_numpy_iterator(), predicts)):\n    axes[i].imshow(sample[0])\n    true_label = tf.argmax(sample[1]).numpy()\n    predict_label = tf.argmax(predict).numpy()\n    correct = true_label == predict_label\n    axes[i].set_title(f\"{CLASSES[predict_label]} ({'correct' if correct else 'wrong'})\")\n    axes[i].axis(\"off\")","metadata":{"id":"HMr2BHUs_uWa","outputId":"43ef7369-9068-4bc2-f2d4-beefab27507b","execution":{"iopub.status.busy":"2023-11-13T04:59:57.334226Z","iopub.execute_input":"2023-11-13T04:59:57.335118Z","iopub.status.idle":"2023-11-13T04:59:59.966497Z","shell.execute_reply.started":"2023-11-13T04:59:57.335083Z","shell.execute_reply":"2023-11-13T04:59:59.965534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the validation dataset for evaluation\nval_dataset_for_evaluation = get_validation_dataset(ordered=True)\nval_labels = np.concatenate([y.numpy() for x, y in val_dataset_for_evaluation])","metadata":{"id":"Pj-nkxeNAA3w","execution":{"iopub.status.busy":"2023-11-13T05:01:43.808676Z","iopub.execute_input":"2023-11-13T05:01:43.809040Z","iopub.status.idle":"2023-11-13T05:01:44.748772Z","shell.execute_reply.started":"2023-11-13T05:01:43.809011Z","shell.execute_reply":"2023-11-13T05:01:44.747887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the pretrained MobileNetV2 model\npretrained_model.load_weights(\"pretrained_model.h5\")\npretrained_predictions = pretrained_model.predict(val_dataset_for_evaluation, verbose=1)\npretrained_predictions_classes = np.argmax(pretrained_predictions, axis=1)\n","metadata":{"id":"HaFQak4TAA6Y","outputId":"f4d32b56-f068-4b86-8bdb-bdc696b936ae","execution":{"iopub.status.busy":"2023-11-13T05:01:45.824481Z","iopub.execute_input":"2023-11-13T05:01:45.824903Z","iopub.status.idle":"2023-11-13T05:01:48.260029Z","shell.execute_reply.started":"2023-11-13T05:01:45.824870Z","shell.execute_reply":"2023-11-13T05:01:48.259255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the MobileNetV2 scratch model\nmobilenetv2_scratch_model.load_weights(\"mobilenetv2_scratch_model.h5\")\nmobilenetv2_scratch_predictions = mobilenetv2_scratch_model.predict(val_dataset_for_evaluation, verbose=1)\nmobilenetv2_scratch_predictions_classes = np.argmax(mobilenetv2_scratch_predictions, axis=1)","metadata":{"id":"BAa6AUUfAA9I","outputId":"04ec6d92-ccfd-41fc-a70d-dabc294f7dce","execution":{"iopub.status.busy":"2023-11-13T05:01:54.375922Z","iopub.execute_input":"2023-11-13T05:01:54.376829Z","iopub.status.idle":"2023-11-13T05:01:55.501510Z","shell.execute_reply.started":"2023-11-13T05:01:54.376796Z","shell.execute_reply":"2023-11-13T05:01:55.500603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def evaluate_and_compare(pretrained_predictions_classes, mobilenetv2_scratch_predictions_classes, val_labels):\n    # Convert one-hot encoded labels back to class indices\n    val_labels_indices = np.argmax(val_labels, axis=1)\n\n    # Evaluate the pretrained model\n    print(\"Pretrained Model:\")\n    evaluate_model(pretrained_predictions_classes, val_labels_indices)\n\n    # Evaluate the scratch model\n    print(\"\\nScratch Model:\")\n    evaluate_model(mobilenetv2_scratch_predictions_classes, val_labels_indices)\n\n    # Compare the models\n    compare_models(pretrained_predictions_classes, mobilenetv2_scratch_predictions_classes, val_labels_indices)","metadata":{"id":"InUen9eAAA_w","execution":{"iopub.status.busy":"2023-11-13T05:01:59.726435Z","iopub.execute_input":"2023-11-13T05:01:59.726818Z","iopub.status.idle":"2023-11-13T05:01:59.732700Z","shell.execute_reply.started":"2023-11-13T05:01:59.726786Z","shell.execute_reply":"2023-11-13T05:01:59.731663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def evaluate_model(predictions_classes, true_labels):\n    conf_mat = confusion_matrix(true_labels, predictions_classes)\n    accuracy = accuracy_score(true_labels, predictions_classes)\n\n    print(\"Confusion Matrix:\")\n    print(conf_mat)\n    print(f\"Accuracy: {accuracy * 100:.2f}%\")","metadata":{"id":"dvsrUc-DAKkA","execution":{"iopub.status.busy":"2023-11-13T05:02:05.566793Z","iopub.execute_input":"2023-11-13T05:02:05.567425Z","iopub.status.idle":"2023-11-13T05:02:05.572315Z","shell.execute_reply.started":"2023-11-13T05:02:05.567393Z","shell.execute_reply":"2023-11-13T05:02:05.571391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def compare_models(pretrained_predictions_classes, mobilenetv2_scratch_predictions_classes, val_labels_indices):\n    # Compare the models\n    print(\"\\nModel Comparison:\")\n\n    # You can add more comparison metrics or visualizations here\n    # For example, comparing confusion matrices, accuracy, etc.\n\n    # Example: Comparing accuracy\n    accuracy_pretrained = accuracy_score(val_labels_indices, pretrained_predictions_classes)\n    accuracy_scratch = accuracy_score(val_labels_indices, mobilenetv2_scratch_predictions_classes)\n\n    print(f\"Pretrained Model Accuracy: {accuracy_pretrained * 100:.2f}%\")\n    print(f\"Scratch Model Accuracy: {accuracy_scratch * 100:.2f}%\")","metadata":{"id":"c4IfP_Z5AMkO","execution":{"iopub.status.busy":"2023-11-13T05:02:09.826198Z","iopub.execute_input":"2023-11-13T05:02:09.826932Z","iopub.status.idle":"2023-11-13T05:02:09.832246Z","shell.execute_reply.started":"2023-11-13T05:02:09.826900Z","shell.execute_reply":"2023-11-13T05:02:09.831252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Call the evaluate_and_compare function\nevaluate_and_compare(pretrained_predictions_classes, mobilenetv2_scratch_predictions_classes, val_labels)","metadata":{"id":"RPOHEAGTAP-e","outputId":"234507f0-fa58-4e91-a0c9-941d5d32bf2f","execution":{"iopub.status.busy":"2023-11-13T05:02:20.374088Z","iopub.execute_input":"2023-11-13T05:02:20.374444Z","iopub.status.idle":"2023-11-13T05:02:20.387467Z","shell.execute_reply.started":"2023-11-13T05:02:20.374416Z","shell.execute_reply":"2023-11-13T05:02:20.386567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd \n\npredictions = pretrained_model.predict(test_dataset, verbose=0)\npredictions = tf.argmax(predictions, axis=-1)\nids = [id_.decode() for image, id_ in test_dataset.unbatch().as_numpy_iterator()]\n\nsubmission = pd.DataFrame(data={\"id\": ids, \"label\": predictions})\nsubmission.to_csv(\"submission.csv\", index=False)\nsubmission","metadata":{"execution":{"iopub.status.busy":"2023-11-13T05:02:34.837939Z","iopub.execute_input":"2023-11-13T05:02:34.838301Z","iopub.status.idle":"2023-11-13T05:02:42.329586Z","shell.execute_reply.started":"2023-11-13T05:02:34.838271Z","shell.execute_reply":"2023-11-13T05:02:42.328633Z"},"trusted":true},"execution_count":null,"outputs":[]}]}