{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.15","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"tpu1vmV38","dataSources":[{"sourceId":21154,"databundleVersionId":1243559,"sourceType":"competition"}],"dockerImageVersionId":30786,"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","execution":{"iopub.status.busy":"2024-11-03T07:42:46.819983Z","iopub.execute_input":"2024-11-03T07:42:46.820260Z","iopub.status.idle":"2024-11-03T07:42:48.664657Z","shell.execute_reply.started":"2024-11-03T07:42:46.820230Z","shell.execute_reply":"2024-11-03T07:42:48.663957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Importing the necessary libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom kaggle_datasets import KaggleDatasets","metadata":{"execution":{"iopub.status.busy":"2024-11-03T07:43:05.706579Z","iopub.execute_input":"2024-11-03T07:43:05.707373Z","iopub.status.idle":"2024-11-03T07:43:18.846483Z","shell.execute_reply.started":"2024-11-03T07:43:05.707334Z","shell.execute_reply":"2024-11-03T07:43:18.845661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# The below code is used to set up TensorFlow for training on a TPU (Tensor Processing Unit).","metadata":{}},{"cell_type":"code","source":"#The code attempts to create a TPUClusterResolver, which is a TensorFlow utility that helps to locate and connect to a TPU.\n#If a TPU is detected, it prints the TPU's master address. If not, it sets tpu to None.\n\ntry:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()  # Detect TPU\n    print('Running on TPU:', tpu.master())\nexcept ValueError:\n    tpu = None\n\n#If a TPU is detected, it connects to the TPU cluster and initializes the TPU system.\n#Then, it creates a TPUStrategy, which is a distribution strategy for running your model on the TPU.\n#If no TPU is detected, it falls back to using the default distribution strategy available, which typically allows for CPU and GPU training.\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\n#This line prints the number of replicas (workers) in sync, which indicates how many TPU cores or devices will be used for training.\n    \nprint(\"REPLICAS: \", strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T07:43:23.902165Z","iopub.execute_input":"2024-11-03T07:43:23.903119Z","iopub.status.idle":"2024-11-03T07:43:32.803916Z","shell.execute_reply.started":"2024-11-03T07:43:23.903076Z","shell.execute_reply":"2024-11-03T07:43:32.802831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading the Dataset \nHere data are stored in tfrecords format. There are seperate directories for train, test and validation, Train and validation dataset does have image and labels but for the test dataset only image and image id is there. For these test images we have to predict the label. Here in each directory multiple images files are present. As an example 00-192*192-798-tfrec here there are 798 individual image files are present in each directory and there are 16 directories present in train and validation folders.","metadata":{}},{"cell_type":"code","source":"# Define constants\nGCS_PATH = KaggleDatasets().get_gcs_path('tpu-getting-started')\nIMAGE_SIZE = (224, 224)  # Adjust according to your dataset\nBATCH_SIZE = 64  # Set your batch size\nAUTOTUNE = tf.data.AUTOTUNE\n\n'''\nBy using AUTOTUNE, TensorFlow can automatically adjust the number of parallel threads used for data loading and preprocessing, which can lead to faster data processing and improved training times. This is especially helpful when working with large datasets.\nIt helps in managing CPU resources efficiently by finding a balance between data loading and model training. This can prevent bottlenecks where the model is waiting for data to be loaded.\nThe optimization is dynamic, meaning that TensorFlow can adapt to different system loads and adjust the parallelism accordingly. If your system has more available resources, it can increase the number of parallel calls, and vice versa.\n'''","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:03.550946Z","iopub.execute_input":"2024-11-03T08:21:03.551443Z","iopub.status.idle":"2024-11-03T08:21:03.559770Z","shell.execute_reply.started":"2024-11-03T08:21:03.551405Z","shell.execute_reply":"2024-11-03T08:21:03.558777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reading TFRecord filenames\n#here we are considering the images having lowest resolution (192*192)\ndef get_tfrec_paths(folder):\n    path = GCS_PATH + '/tfrecords-jpeg-224x224/' + folder\n    return tf.io.gfile.glob(f'{path}/*.tfrec')","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:13.153549Z","iopub.execute_input":"2024-11-03T08:21:13.154755Z","iopub.status.idle":"2024-11-03T08:21:13.159391Z","shell.execute_reply.started":"2024-11-03T08:21:13.154709Z","shell.execute_reply":"2024-11-03T08:21:13.158233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get file paths for train, validation, and test datasets\ntrain_file_paths = get_tfrec_paths('train')\nval_file_paths = get_tfrec_paths('val')\ntest_file_paths = get_tfrec_paths('test')","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:16.442447Z","iopub.execute_input":"2024-11-03T08:21:16.442880Z","iopub.status.idle":"2024-11-03T08:21:16.462748Z","shell.execute_reply.started":"2024-11-03T08:21:16.442845Z","shell.execute_reply":"2024-11-03T08:21:16.461593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print the number of directories or records in each folder\nprint(f'''Number of TFRecord files:\nTrain: {len(train_file_paths)}\nValidation: {len(val_file_paths)}\nTest: {len(test_file_paths)}''')","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:17.579523Z","iopub.execute_input":"2024-11-03T08:21:17.579929Z","iopub.status.idle":"2024-11-03T08:21:17.584785Z","shell.execute_reply.started":"2024-11-03T08:21:17.579895Z","shell.execute_reply":"2024-11-03T08:21:17.583818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to decode images\n#The decode function decodes a JPEG image from the byte string provided in image_data. The channels=3 argument specifies that the image should be decoded as a three-channel (RGB) image.\n#It transforms the raw byte data into a usable image format that TensorFlow can work with.\n#It also perform normalization of the images pixel to 0 to 1 range for training stability and better convergence\ndef decode_image(image_data):\n    image = tf.image.decode_jpeg(image_data, channels=3)\n    image = tf.cast(image, tf.float32) / 255.0  # Normalize to [0, 1]\n    image = tf.reshape(image, IMAGE_SIZE + (3,))  # Reshape to (H, W, C)\n    return image","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:19.046522Z","iopub.execute_input":"2024-11-03T08:21:19.046940Z","iopub.status.idle":"2024-11-03T08:21:19.052617Z","shell.execute_reply.started":"2024-11-03T08:21:19.046904Z","shell.execute_reply":"2024-11-03T08:21:19.051576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#this function is to load the test dataset where label is not present \n# Read unlabeled TFRecord example\ndef read_unlabeled_tfrec(example):\n    the_format = {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'id': tf.io.FixedLenFeature([], tf.string)\n    }\n    example = tf.io.parse_single_example(example, the_format)\n    image = decode_image(example['image'])\n    idnum = example['id']\n    return image, idnum","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:20.283160Z","iopub.execute_input":"2024-11-03T08:21:20.284318Z","iopub.status.idle":"2024-11-03T08:21:20.289878Z","shell.execute_reply.started":"2024-11-03T08:21:20.284275Z","shell.execute_reply":"2024-11-03T08:21:20.288599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#this function is to load train and validation images having both label and images\n# Read labeled TFRecord example\ndef read_labeled_tfrec(example):\n    the_format = {\n        'image': tf.io.FixedLenFeature([], tf.string),\n        'class': tf.io.FixedLenFeature([], tf.int64)\n    }\n    example = tf.io.parse_single_example(example, the_format)\n    image = decode_image(example['image'])\n    label = tf.cast(example['class'], tf.int32)\n    return image, label","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:21.577593Z","iopub.execute_input":"2024-11-03T08:21:21.578156Z","iopub.status.idle":"2024-11-03T08:21:21.583884Z","shell.execute_reply.started":"2024-11-03T08:21:21.578110Z","shell.execute_reply":"2024-11-03T08:21:21.582770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load dataset\n#The load_data function is responsible for loading data from TFRecords files and preparing it for further processing\n#file_paths: A list of paths to TFRecords files to be read.\n#labeled: A boolean indicating whether the dataset contains labeled data (default is True). This determines which reading function will be used to parse the TFRecords.\n#ordered: A boolean indicating whether the dataset should maintain a deterministic (ordered) sequence. Default is False, which means the order is not guaranteed.\ndef load_data(file_paths, labeled=True, ordered=False):\n    ignore_order = tf.data.Options()\n    if not ordered:\n        ignore_order.experimental_deterministic = False  # Disable deterministic ordering\n    data = tf.data.TFRecordDataset(file_paths, num_parallel_reads=AUTOTUNE)\n    data = data.with_options(ignore_order)\n    data = data.map(read_labeled_tfrec if labeled else read_unlabeled_tfrec, num_parallel_calls=AUTOTUNE)\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:23.003289Z","iopub.execute_input":"2024-11-03T08:21:23.003707Z","iopub.status.idle":"2024-11-03T08:21:23.010011Z","shell.execute_reply.started":"2024-11-03T08:21:23.003668Z","shell.execute_reply":"2024-11-03T08:21:23.008909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Augmentation","metadata":{}},{"cell_type":"code","source":"#this is for augmentation. Different augmentation is applied here. As this is a classfication problem augmentation is just applied\n#in the images not in the labels. For segmentation task augmentation should be applied both in image and mask.\ndef augment(image, label):\n    # Randomly flip the image and label horizontally\n    image = tf.image.random_flip_left_right(image)\n    \n    # Randomly flip the image and label vertically\n    image = tf.image.random_flip_up_down(image)\n    \n    # Randomly rotate the image by up to 20 degrees\n    #angles = tf.random.uniform(shape=[], minval=-0.2, maxval=0.2)  # Radians\n    image = tf.image.rot90(image)\n    \n    # Randomly adjust brightness\n    image = tf.image.random_brightness(image, max_delta=0.1)  # 10% brightness change\n    \n    # Randomly adjust contrast\n    image = tf.image.random_contrast(image, lower=0.9, upper=1.1)\n    \n    # Optionally add other augmentations like zoom, cropping, etc.\n\n    return image, label\n","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:31.058955Z","iopub.execute_input":"2024-11-03T08:21:31.059375Z","iopub.status.idle":"2024-11-03T08:21:31.065655Z","shell.execute_reply.started":"2024-11-03T08:21:31.059340Z","shell.execute_reply":"2024-11-03T08:21:31.064285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing Train, Test and Validation Data Loader","metadata":{}},{"cell_type":"code","source":"# Getting train datasets and setting some configurations and augmentations\ndef get_train():\n    data = load_data(train_file_paths)\n    data = data.map(augment, num_parallel_calls=tf.data.AUTOTUNE)  # Apply augmentations\n    data = data.repeat()  # Repeat for multiple epochs\n    data = data.shuffle(101)  # Shuffle the dataset\n    data = data.batch(BATCH_SIZE)  # Batch the dataset\n    data = data.prefetch(AUTOTUNE)  # Prefetch for better performance\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:33.239936Z","iopub.execute_input":"2024-11-03T08:21:33.240989Z","iopub.status.idle":"2024-11-03T08:21:33.245930Z","shell.execute_reply.started":"2024-11-03T08:21:33.240948Z","shell.execute_reply":"2024-11-03T08:21:33.244816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#loading the validation dataset\ndef get_val():\n    data = load_data(val_file_paths)\n    data = data.batch(BATCH_SIZE)  # Batch the validation dataset\n    #data = data.cache()  # Cache for efficiency\n    data = data.prefetch(AUTOTUNE)  # Prefetch\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:34.213530Z","iopub.execute_input":"2024-11-03T08:21:34.213939Z","iopub.status.idle":"2024-11-03T08:21:34.219074Z","shell.execute_reply.started":"2024-11-03T08:21:34.213907Z","shell.execute_reply":"2024-11-03T08:21:34.217991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Loading the test dataset\ndef get_test():\n    data = load_data(test_file_paths,labeled=False,ordered=True)\n    data = data.batch(BATCH_SIZE)\n    data = data.prefetch(AUTOTUNE)\n    return data","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:35.587946Z","iopub.execute_input":"2024-11-03T08:21:35.588302Z","iopub.status.idle":"2024-11-03T08:21:35.593044Z","shell.execute_reply.started":"2024-11-03T08:21:35.588271Z","shell.execute_reply":"2024-11-03T08:21:35.592064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#get all the datasets\ntrain_dataset = get_train()\nval_dataset = get_val()\ntest_dataset = get_test()","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:36.998856Z","iopub.execute_input":"2024-11-03T08:21:36.999699Z","iopub.status.idle":"2024-11-03T08:21:37.282978Z","shell.execute_reply.started":"2024-11-03T08:21:36.999659Z","shell.execute_reply":"2024-11-03T08:21:37.281649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Print first batch of training dataset\nfor images, labels in train_dataset.take(1):  # Take the first batch\n    print('First batch images shape:', images.shape)\n    print('First batch labels shape:', labels.shape)\n    break  # Break after first batch\n","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:38.570738Z","iopub.execute_input":"2024-11-03T08:21:38.571156Z","iopub.status.idle":"2024-11-03T08:21:38.737754Z","shell.execute_reply.started":"2024-11-03T08:21:38.571121Z","shell.execute_reply":"2024-11-03T08:21:38.736430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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']\n\nprint (len(CLASSES))","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:40.320622Z","iopub.execute_input":"2024-11-03T08:21:40.321029Z","iopub.status.idle":"2024-11-03T08:21:40.329515Z","shell.execute_reply.started":"2024-11-03T08:21:40.320995Z","shell.execute_reply":"2024-11-03T08:21:40.328436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Comuting the length of Train, Test and Validation Set ","metadata":{}},{"cell_type":"code","source":"#to extract the number of images in each file. suppose the file name is 00-192*192-462.tfrec. here 462 images are present in \n# a single file \nimport re\ndef count_data_items(filenames):\n    n = [int(re.compile(r\"-([0-9]*)\\.\").search(filename).group(1)) for filename in filenames]\n    return np.sum(n)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:41.765588Z","iopub.execute_input":"2024-11-03T08:21:41.765978Z","iopub.status.idle":"2024-11-03T08:21:41.771084Z","shell.execute_reply.started":"2024-11-03T08:21:41.765944Z","shell.execute_reply":"2024-11-03T08:21:41.770076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print the total train, test and validation images\ntotal_train_image = count_data_items(train_file_paths)\ntotal_test_image = count_data_items(test_file_paths)\ntotal_vali_image = count_data_items(val_file_paths)\n\nprint(f'train : {total_train_image}\\ntest : {total_test_image}\\nvalidation : {total_vali_image}')","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:43.077190Z","iopub.execute_input":"2024-11-03T08:21:43.077634Z","iopub.status.idle":"2024-11-03T08:21:43.083758Z","shell.execute_reply.started":"2024-11-03T08:21:43.077597Z","shell.execute_reply":"2024-11-03T08:21:43.082391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Defining the number of steps in each epoch because we have selected data.repeat() for continuos iteration \n#on the train dataset\ntrain_steps_per_epoch = total_train_image // BATCH_SIZE\nval_steps_per_epoch = total_vali_image // BATCH_SIZE","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:44.852565Z","iopub.execute_input":"2024-11-03T08:21:44.852974Z","iopub.status.idle":"2024-11-03T08:21:44.858037Z","shell.execute_reply.started":"2024-11-03T08:21:44.852941Z","shell.execute_reply":"2024-11-03T08:21:44.856894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Deployment and Training (Fine tuning and Transfer Learning)","metadata":{}},{"cell_type":"code","source":"\n#Model Xception \nfrom tensorflow import keras\nfrom keras.applications.xception import Xception,preprocess_input\nfrom keras import Sequential,layers,Input,Model,callbacks\nwith strategy.scope():\n    base = Xception(weights='imagenet',include_top=False,input_shape=(224,224,3))\n    base.trainable = True\n    for layer in base.layers[-15:]:\n        layer.trainable = True\n    model_xception = Sequential([\n        base,\n        layers.GlobalAveragePooling2D(),\n        #layers.Dense(512,activation='relu'),\n        layers.Dropout(0.2),\n        layers.Dense(len(CLASSES),activation='softmax')\n    ])\n    model_xception.compile(\n        optimizer = keras.optimizers.Adam(learning_rate=0.00001),\n        loss = 'sparse_categorical_crossentropy',\n        metrics = ['sparse_categorical_accuracy']\n    )\n","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:10:38.844396Z","iopub.execute_input":"2024-11-03T09:10:38.844820Z","iopub.status.idle":"2024-11-03T09:10:43.380976Z","shell.execute_reply.started":"2024-11-03T09:10:38.844785Z","shell.execute_reply":"2024-11-03T09:10:43.379610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Train the model\nhistory_xception = model_xception.fit(train_dataset, steps_per_epoch=train_steps_per_epoch, validation_data=val_dataset, epochs=50)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:10:51.008723Z","iopub.execute_input":"2024-11-03T09:10:51.009189Z","iopub.status.idle":"2024-11-03T09:26:20.595335Z","shell.execute_reply.started":"2024-11-03T09:10:51.009150Z","shell.execute_reply":"2024-11-03T09:26:20.594126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n#Model Resnet50\nfrom tensorflow import keras\nfrom keras.applications import ResNet50\nfrom keras import Sequential, layers, Input, Model, callbacks\n\n# Assuming IMAGE_SIZE and CLASSES are defined\n# IMAGE_SIZE = (height, width)\n# CLASSES = number of classes\n\nwith strategy.scope():\n    # Load the ResNet-50 model pre-trained on ImageNet without the top layer\n    base = ResNet50(weights='imagenet', include_top=False, input_shape=(512,512,3))\n    \n    base.trainable = True\n    \n    # Unfreeze the last few layers for fine-tuning\n    for layer in base.layers[-30:]:\n        layer.trainable = True\n    \n    # Build the model\n    model_ResNet50= Sequential([\n        base,\n        layers.GlobalAveragePooling2D(),  # Global Average Pooling layer\n        layers.Dense(512, activation='relu'),  # Fully connected layer with ReLU activation\n        layers.Dropout(0.2),  # Dropout for regularization\n        layers.Dense(len(CLASSES), activation='softmax')  # Output layer for multiclass classification\n    ])\n    \n    # Compile the model with Adam optimizer and a learning rate schedule\n    model_ResNet50.compile(\n        optimizer=keras.optimizers.SGD(learning_rate=1e-4, momentum=0.9),  # Using Adam optimizer with a lower learning rate\n        loss='sparse_categorical_crossentropy',\n        metrics=['sparse_categorical_accuracy']\n    )\n\n'''\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Train the model\n#history_ResNet50= model_ResNet50.fit(train_dataset, steps_per_epoch=train_steps_per_epoch,validation_steps = val_steps_per_epoch, validation_data=val_dataset, epochs=50)\n\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n#MODEL EfficientNet\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\n\n# Assuming IMAGE_SIZE and CLASSES are defined\n# IMAGE_SIZE = (192, 192)\n# CLASSES = number of classes\n\n# Define the strategy for distribution if needed\n#strategy = tf.distribute.MirroredStrategy()\n\nwith strategy.scope():\n    # Load the EfficientNetB7 model pre-trained on ImageNet without the top layer\n    base = keras.applications.EfficientNetB7(\n        include_top=False, \n        weights='imagenet', \n        input_shape=(512, 512, 3)\n    )\n    \n    # Freeze the base model initially\n    base.trainable = False\n    # Unfreeze the last few layers for fine-tuning\n    #for layer in base.layers[-200:]:\n        #layer.trainable = True\n    \n    # Build the model\n    model_efficient = keras.Sequential([\n        base,\n        layers.GlobalAveragePooling2D(),\n        #layers.Dense(512, activation='relu'),\n        #layers.Dropout(0.2),\n        layers.Dense(104, activation='softmax')\n    ])\n    \n    # Compile the model\n    model_efficient.compile(\n        optimizer=keras.optimizers.Adam(),\n        loss='sparse_categorical_crossentropy',\n        metrics=['sparse_categorical_accuracy']\n    )\n\n'''\n","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:05:18.386836Z","iopub.execute_input":"2024-11-03T08:05:18.387281Z","iopub.status.idle":"2024-11-03T08:05:37.773348Z","shell.execute_reply.started":"2024-11-03T08:05:18.387246Z","shell.execute_reply":"2024-11-03T08:05:37.772270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#history_efficient= model_efficient.fit(train_dataset, steps_per_epoch=train_steps_per_epoch, validation_data=val_dataset, epochs=30)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:05:37.774915Z","iopub.execute_input":"2024-11-03T08:05:37.775197Z","iopub.status.idle":"2024-11-03T08:19:21.832209Z","shell.execute_reply.started":"2024-11-03T08:05:37.775168Z","shell.execute_reply":"2024-11-03T08:19:21.830529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n#compute the number of steps in each epoch \nsteps_per_epoch = train_count // BATCH_SIZE\nprint (steps_per_epoch)\nprint (BATCH_SIZE)\n'''\nprint(len(CLASSES))","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:21:56.918180Z","iopub.execute_input":"2024-11-03T08:21:56.918629Z","iopub.status.idle":"2024-11-03T08:21:56.924016Z","shell.execute_reply.started":"2024-11-03T08:21:56.918593Z","shell.execute_reply":"2024-11-03T08:21:56.922849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\n#MobileNet V2\nwith strategy.scope():\n\n    # Define preprocess input and prediction layer\n    preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input\n    prediction_layer = tf.keras.layers.Dense(len(CLASSES), activation='softmax')\n\n    # Load the MobileNetV2 model pre-trained on ImageNet without the top layer\n    base_model = tf.keras.applications.MobileNetV2(\n        include_top=False, \n        weights='imagenet', \n        input_shape=(224, 224, 3)\n    )\n    # Let's take a look to see how many layers are in the base model\n    print(\"Number of layers in the base model: \", len(base_model.layers))\n\n    # Fine-tune from this layer onwards\n    #fine_tune_at = 80\n    \n    #base_model.trainable = True  # Freeze the base model initially\n    # Freeze all the layers before the `fine_tune_at` layer\n    #for layer in base_model.layers[:fine_tune_at]:\n        #layer.trainable = False\n\n    # Define the input and build the model\n    inputs = tf.keras.Input(shape=(224, 224, 3))\n    #x = data_augmentation(inputs)\n    #x = preprocess_input(x)\n    x = base_model(inputs, training=False)\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n    x= tf.keras.layers.Dense(512, activation='relu')(x)\n    x = tf.keras.layers.Dropout(0.2)(x)\n    outputs = prediction_layer(x)\n    model_mobilenet = tf.keras.Model(inputs, outputs)\n\n    # Compile the model\n    base_learning_rate = 0.0001\n    model_mobilenet.compile(\n        optimizer=tf.keras.optimizers.Adam(learning_rate=base_learning_rate),\n        loss='sparse_categorical_crossentropy',\n        metrics=['sparse_categorical_accuracy']\n    )\n'''","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:31:20.166553Z","iopub.execute_input":"2024-11-03T08:31:20.167072Z","iopub.status.idle":"2024-11-03T08:31:24.594966Z","shell.execute_reply.started":"2024-11-03T08:31:20.167014Z","shell.execute_reply":"2024-11-03T08:31:24.593743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#history_mobilenet= model_mobilenet.fit(train_dataset, steps_per_epoch=train_steps_per_epoch, validation_data=val_dataset, epochs=50)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-03T08:31:28.628754Z","iopub.execute_input":"2024-11-03T08:31:28.629214Z","iopub.status.idle":"2024-11-03T08:42:52.566613Z","shell.execute_reply.started":"2024-11-03T08:31:28.629174Z","shell.execute_reply":"2024-11-03T08:42:52.564804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nimport matplotlib.pyplot as plt\nacc = history_mobilenet.history['sparse_categorical_accuracy']\nval_acc = history_mobilenet.history['val_sparse_categorical_accuracy']\n\nloss = history_mobilenet.history['loss']\nval_loss = history_mobilenet.history['val_loss']\n\nplt.figure(figsize=(8, 8))\nplt.subplot(2, 1, 1)\nplt.plot(acc, label='Training Accuracy')\nplt.plot(val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.ylabel('Accuracy')\nplt.ylim([min(plt.ylim()),1])\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 1, 2)\nplt.plot(loss, label='Training Loss')\nplt.plot(val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.ylabel('Cross Entropy')\n#plt.ylim([0,1.0])\nplt.title('Training and Validation Loss')\nplt.xlabel('epoch')\nplt.show()\n\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nwith strategy.scope():\n    #fine tuning \n    base_model.trainable = True\n\n    # Let's take a look to see how many layers are in the base model\n    print(\"Number of layers in the base model: \", len(base_model.layers))\n\n    # Fine-tune from this layer onwards\n    #fine_tune_at = 110\n\n    # Freeze all the layers before the `fine_tune_at` layer\n    #for layer in base_model.layers[:fine_tune_at]:\n        #layer.trainable = False\n    \n    base_learning_rate = 0.0001/10\n    model_mobilenet.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=base_learning_rate),\n              loss='sparse_categorical_crossentropy',\n        metrics=['sparse_categorical_accuracy'])\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''\nfine_tune_epochs = 20\ninitial_epochs=30\ntotal_epochs =  initial_epochs + fine_tune_epochs\n\nhistory_fine_mobile = model_mobilenet.fit(train_dataset, steps_per_epoch=train_steps_per_epoch,validation_data=val_dataset,\n                         epochs=total_epochs,\n                         initial_epoch=len(history_mobilenet.epoch))\n'''","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Visualization of Train and Validation Performance Metrics","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n# Plotting the training history\ndef plot_training_history(history):\n    # Plot training & validation accuracy values\n    plt.figure(figsize=(14, 5))\n\n    plt.subplot(1, 2, 1)\n    plt.plot(history.history['sparse_categorical_accuracy'], label='Train Accuracy')\n    plt.plot(history.history['val_sparse_categorical_accuracy'], label='Validation Accuracy')\n    plt.title('Model Accuracy')\n    plt.xlabel('Epochs')\n    plt.ylabel('Accuracy')\n    plt.legend(loc='upper left')\n\n    # Plot training & validation loss values\n    plt.subplot(1, 2, 2)\n    plt.plot(history.history['loss'], label='Train Loss')\n    plt.plot(history.history['val_loss'], label='Validation Loss')\n    plt.title('Model Loss')\n    plt.xlabel('Epochs')\n    plt.ylabel('Loss')\n    plt.legend(loc='upper left')\n\n    plt.show()\n\n# Call the plotting function\nplot_training_history(history_xception)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:26:42.356831Z","iopub.execute_input":"2024-11-03T09:26:42.357324Z","iopub.status.idle":"2024-11-03T09:26:43.506834Z","shell.execute_reply.started":"2024-11-03T09:26:42.357282Z","shell.execute_reply":"2024-11-03T09:26:43.505894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing Data for Submission","metadata":{}},{"cell_type":"code","source":"test_images = test_dataset.map(lambda image, idnum: image)\nprobabilities = model_xception.predict(test_images)\npredictions = np.argmax(probabilities, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:26:47.376879Z","iopub.execute_input":"2024-11-03T09:26:47.377875Z","iopub.status.idle":"2024-11-03T09:27:10.393718Z","shell.execute_reply.started":"2024-11-03T09:26:47.377831Z","shell.execute_reply":"2024-11-03T09:27:10.392584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the directory where you want to save the submission file\noutput_directory = '/kaggle/working/'  # Replace with your desired directory path\n\n# Create the directory if it doesn't exist\nos.makedirs(output_directory, exist_ok=True)\n\n# Extract test IDs from the test dataset\ntest_images = test_dataset.map(lambda image, idnum: idnum).unbatch()\ntest_ids = next(iter(test_images.batch(total_test_image))).numpy().astype('U')\n\n# Define the full path for the submission file\nsubmission_file_path = os.path.join(output_directory, 'submission.csv')\n\n# Save the predictions to the specified directory\nnp.savetxt(\n    submission_file_path,\n    np.rec.fromarrays([test_ids, predictions]),\n    fmt=['%s', '%d'],\n    delimiter=',',\n    header='id,label',\n    comments='',\n)","metadata":{"execution":{"iopub.status.busy":"2024-11-03T09:28:14.046495Z","iopub.execute_input":"2024-11-03T09:28:14.046945Z","iopub.status.idle":"2024-11-03T09:28:15.042733Z","shell.execute_reply.started":"2024-11-03T09:28:14.046909Z","shell.execute_reply":"2024-11-03T09:28:15.041453Z"},"trusted":true},"execution_count":null,"outputs":[]}]}