{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.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":42118,"databundleVersionId":4581569,"sourceType":"competition"}],"dockerImageVersionId":30733,"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\n#for 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-06-09T22:40:50.873177Z","iopub.execute_input":"2024-06-09T22:40:50.873881Z","iopub.status.idle":"2024-06-09T22:40:50.878746Z","shell.execute_reply.started":"2024-06-09T22:40:50.873849Z","shell.execute_reply":"2024-06-09T22:40:50.877713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore', category=FutureWarning)\nwarnings.filterwarnings('ignore', category=UserWarning)\n\nimport numpy as np\nimport pandas as pd\nimport os\nimport cv2\nfrom sklearn.model_selection import train_test_split\nfrom keras.models import Sequential \nfrom keras.layers import Dense, Conv2D, Flatten, Input, MaxPooling2D, Dropout, BatchNormalization, Reshape","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:29:40.691981Z","iopub.execute_input":"2024-06-09T22:29:40.692455Z","iopub.status.idle":"2024-06-09T22:29:53.278624Z","shell.execute_reply.started":"2024-06-09T22:29:40.692422Z","shell.execute_reply":"2024-06-09T22:29:53.277784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_path = \"/kaggle/input/5-flowers-image-classification/train/\"\nlabels = ['daisy', 'dandelion','rose','sunflower','tulip']","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:29:53.280102Z","iopub.execute_input":"2024-06-09T22:29:53.280651Z","iopub.status.idle":"2024-06-09T22:29:53.285834Z","shell.execute_reply.started":"2024-06-09T22:29:53.280612Z","shell.execute_reply":"2024-06-09T22:29:53.284886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_list = []\nlabel_list = []\n\nfor label in labels:\n    dir_path = os.path.join(base_path, label)\n    for img_file in os.listdir(dir_path):\n        if not img_file.startswith('.'):  # Skip files starting with '.', this will prevent .DS_Store to show(macos specific protocol)\n            img_path = os.path.join(dir_path, img_file)\n            img_list.append(img_path)\n            label_list.append(label)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:29:53.287073Z","iopub.execute_input":"2024-06-09T22:29:53.287423Z","iopub.status.idle":"2024-06-09T22:29:53.621769Z","shell.execute_reply.started":"2024-06-09T22:29:53.28739Z","shell.execute_reply":"2024-06-09T22:29:53.6209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.DataFrame({'img':img_list,'label':label_list})","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:29:53.62349Z","iopub.execute_input":"2024-06-09T22:29:53.623788Z","iopub.status.idle":"2024-06-09T22:29:53.631394Z","shell.execute_reply.started":"2024-06-09T22:29:53.623761Z","shell.execute_reply":"2024-06-09T22:29:53.630469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:29:53.632636Z","iopub.execute_input":"2024-06-09T22:29:53.632963Z","iopub.status.idle":"2024-06-09T22:29:53.655885Z","shell.execute_reply.started":"2024-06-09T22:29:53.632933Z","shell.execute_reply":"2024-06-09T22:29:53.655016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"d= {'daisy':0, 'dandelion':1,'rose':2,'sunflower':3,'tulip':4}\ndf['encode_label']=df['label'].map(d)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:29:56.652139Z","iopub.execute_input":"2024-06-09T22:29:56.652808Z","iopub.status.idle":"2024-06-09T22:29:56.661286Z","shell.execute_reply.started":"2024-06-09T22:29:56.652776Z","shell.execute_reply":"2024-06-09T22:29:56.660357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = []\nfor img in df['img']:\n    img = cv2.imread(img)\n    img = cv2.resize(img,(170,170))\n    img = img/255.0\n    x.append(img)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:29:58.224975Z","iopub.execute_input":"2024-06-09T22:29:58.225667Z","iopub.status.idle":"2024-06-09T22:30:24.041311Z","shell.execute_reply.started":"2024-06-09T22:29:58.225623Z","shell.execute_reply":"2024-06-09T22:30:24.040449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.array(x)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:30:24.042965Z","iopub.execute_input":"2024-06-09T22:30:24.043287Z","iopub.status.idle":"2024-06-09T22:30:24.828993Z","shell.execute_reply.started":"2024-06-09T22:30:24.043262Z","shell.execute_reply":"2024-06-09T22:30:24.827923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=df['encode_label']","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:30:24.83065Z","iopub.execute_input":"2024-06-09T22:30:24.831003Z","iopub.status.idle":"2024-06-09T22:30:24.83582Z","shell.execute_reply.started":"2024-06-09T22:30:24.830969Z","shell.execute_reply":"2024-06-09T22:30:24.834601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test, y_train, y_test = train_test_split(x,y,test_size=0.1,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:30:24.838581Z","iopub.execute_input":"2024-06-09T22:30:24.838927Z","iopub.status.idle":"2024-06-09T22:30:25.400147Z","shell.execute_reply.started":"2024-06-09T22:30:24.838895Z","shell.execute_reply":"2024-06-09T22:30:25.399375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Input(shape=[170,170, 3]))\nmodel.add(Conv2D(32,kernel_size=(3,3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Conv2D(64,kernel_size=(3,3), activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Flatten())\nmodel.add(Dense(128))\nmodel.add(Dense(5, activation='softmax'))\n\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:30:25.401395Z","iopub.execute_input":"2024-06-09T22:30:25.401745Z","iopub.status.idle":"2024-06-09T22:30:26.110059Z","shell.execute_reply.started":"2024-06-09T22:30:25.401714Z","shell.execute_reply":"2024-06-09T22:30:26.109279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=20, verbose=1)\nmodel.save('my_cnn_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:30:26.111153Z","iopub.execute_input":"2024-06-09T22:30:26.111446Z","iopub.status.idle":"2024-06-09T22:31:19.762429Z","shell.execute_reply.started":"2024-06-09T22:30:26.11142Z","shell.execute_reply":"2024-06-09T22:31:19.76158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore', category=FutureWarning)\nwarnings.filterwarnings('ignore', category=UserWarning)\n\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import VGG16\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Flatten, Dense, BatchNormalization\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\n\n# Directory containing image data\ndata_dir = \"/kaggle/input/5-flowers-image-classification/train/\"\n\n# Image dimensions\nimg_width, img_height = 224, 224\n\n# Image data generator for training data\ntrain_datagen = ImageDataGenerator(rescale=1/255, validation_split=0.20)\ntrain_generator = train_datagen.flow_from_directory(\n    directory=data_dir, target_size=(img_width, img_height), class_mode='categorical', subset='training'\n)\n\n# Image data generator for validation data\nvalidation_datagen = ImageDataGenerator(rescale=1/255, validation_split=0.20)\nvalidation_generator = validation_datagen.flow_from_directory(\n    directory=data_dir, target_size=(img_width, img_height), class_mode='categorical', subset='validation'\n)\n\n# Load the VGG16 model pre-trained on ImageNet\nbase_model = VGG16(weights='imagenet', input_shape=(img_width, img_height, 3), include_top=False)\n\n# Create the model\nmodel = Sequential()\nmodel.add(base_model)\n\n# Unfreeze the last few layers of the base model\nfor layer in base_model.layers[-4:]:\n    layer.trainable = True\n\n# Add custom layers on top of the base model\nmodel.add(Flatten())\nmodel.add(Dense(128, activation='relu'))\nmodel.add(Dense(512, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.3))\nmodel.add(Dense(5, activation='softmax'))\n\n# Compile the model with a lower learning rate\noptimizer = Adam(learning_rate=1e-4)\nmodel.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])\n\n# Callbacks\nlr_scheduler = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1)\nearly_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)\n\n# Train the model\nmodel.fit(train_generator, epochs=50, validation_data=validation_generator, callbacks=[lr_scheduler, early_stopping])\n\nmodel.save('flower_VGG16_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:31:23.545497Z","iopub.execute_input":"2024-06-09T22:31:23.545873Z","iopub.status.idle":"2024-06-09T22:35:30.256827Z","shell.execute_reply.started":"2024-06-09T22:31:23.545842Z","shell.execute_reply":"2024-06-09T22:35:30.255991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nfrom tensorflow.keras.models import load_model\n\n# Load the saved model\nmodel = load_model('flower_VGG16_model.h5')\n\n# Define the test image path\ntest_path = \"/kaggle/input/5-flowers-image-classification/test/\"\nimg_width, img_height = 224, 224\n\n# Class labels\nclass_labels = ['daisy', 'dandelion', 'rose', 'sunflower', 'tulip']\n\n# Prepare the test data generator\ntest_datagen = ImageDataGenerator(rescale=1/255)\n\n# Function to load and preprocess test images\ndef preprocess_image(img_path):\n    img = load_img(img_path, target_size=(img_width, img_height))\n    img_array = img_to_array(img)\n    img_array = np.expand_dims(img_array, axis=0)\n    img_array /= 255.0\n    return img_array\n\n# Initialize a list to hold the results\nresults = []\n\n# Loop through test images, make predictions, and save to results list\nfor i in range(1, 401):\n    img_path = os.path.join(test_path, f\"{i}.jpg\")\n    img_array = preprocess_image(img_path)\n    prediction = model.predict(img_array)\n    predicted_class = class_labels[np.argmax(prediction)]\n    results.append({'id': i, 'label': predicted_class})\n\n# Convert results list to a DataFrame\nsubmission = pd.DataFrame(results)\n\n# Save the submission dataframe to a CSV file\nsubmission.to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:44:49.9701Z","iopub.execute_input":"2024-06-09T22:44:49.970845Z","iopub.status.idle":"2024-06-09T22:45:15.346531Z","shell.execute_reply.started":"2024-06-09T22:44:49.970809Z","shell.execute_reply":"2024-06-09T22:45:15.345737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv(\"/kaggle/input/5-flowers-image-classification/Sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:40:58.841871Z","iopub.execute_input":"2024-06-09T22:40:58.842243Z","iopub.status.idle":"2024-06-09T22:40:58.852193Z","shell.execute_reply.started":"2024-06-09T22:40:58.842202Z","shell.execute_reply":"2024-06-09T22:40:58.851272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:41:08.176076Z","iopub.execute_input":"2024-06-09T22:41:08.176455Z","iopub.status.idle":"2024-06-09T22:41:08.188386Z","shell.execute_reply.started":"2024-06-09T22:41:08.176425Z","shell.execute_reply":"2024-06-09T22:41:08.187412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:45:48.235526Z","iopub.execute_input":"2024-06-09T22:45:48.235945Z","iopub.status.idle":"2024-06-09T22:45:48.243252Z","shell.execute_reply.started":"2024-06-09T22:45:48.235914Z","shell.execute_reply":"2024-06-09T22:45:48.242319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2024-06-09T22:45:52.058974Z","iopub.execute_input":"2024-06-09T22:45:52.05936Z","iopub.status.idle":"2024-06-09T22:45:52.074116Z","shell.execute_reply.started":"2024-06-09T22:45:52.05933Z","shell.execute_reply":"2024-06-09T22:45:52.073348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import os\nimport warnings\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport tensorflow_hub as hub\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau, EarlyStopping\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\n\nwarnings.filterwarnings('ignore', category=FutureWarning)\nwarnings.filterwarnings('ignore', category=UserWarning)\n\n# Load the BiT model from TensorFlow Hub\nmodel_url = \"https://tfhub.dev/google/bit/m-r50x1/1\"\nmodule = hub.KerasLayer(model_url)\n\nclass MyBiTModel(tf.keras.Model):\n    def __init__(self, num_classes, module):\n        super().__init__()\n        self.num_classes = num_classes\n        self.head = Dense(num_classes, kernel_initializer='zero')\n        self.bit_model = module\n\n    def call(self, images):\n        bit_embedding = self.bit_model(images)\n        return self.head(bit_embedding)\n\nmodel = MyBiTModel(num_classes=5, module=module)\n\n# Constants\nimg_width, img_height = 224, 224\nbatch_size = 32\nresize_to = 256\ncrop_to = 224\nauto = tf.data.AUTOTUNE\n\n# Directory containing image data\ndata_dir = \"/kaggle/input/5-flowers-image-classification/train/\"\ntest_path = \"/kaggle/input/5-flowers-image-classification/test/\"\n\n# Image data generator for training data\ntrain_datagen = ImageDataGenerator(\n    rescale=1/255, \n    validation_split=0.20,\n    horizontal_flip=True\n)\n\n# Train generator\ntrain_generator = train_datagen.flow_from_directory(\n    directory=data_dir, \n    target_size=(img_width, img_height), \n    class_mode='categorical', \n    subset='training', \n    batch_size=batch_size,\n    shuffle=True\n)\n\n# Validation generator\nvalidation_datagen = ImageDataGenerator(rescale=1/255, validation_split=0.20)\nvalidation_generator = validation_datagen.flow_from_directory(\n    directory=data_dir, \n    target_size=(img_width, img_height), \n    class_mode='categorical', \n    subset='validation', \n    batch_size=batch_size,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-10T08:08:45.920149Z","iopub.execute_input":"2024-06-10T08:08:45.920527Z","iopub.status.idle":"2024-06-10T08:09:01.136887Z","shell.execute_reply.started":"2024-06-10T08:08:45.920486Z","shell.execute_reply":"2024-06-10T08:09:01.135943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Learning rate schedule and optimizer\nlearning_rate = 0.003 * batch_size / 512\nschedule_boundaries = [10000, 20000, 30000]\nlr_schedule = tf.keras.optimizers.schedules.PiecewiseConstantDecay(\n    boundaries=schedule_boundaries,\n    values=[\n        learning_rate,\n        learning_rate * 0.1,\n        learning_rate * 0.01,\n        learning_rate * 0.001,\n    ],\n)\noptimizer = Adam(learning_rate=1e-4)\nloss_fn = tf.keras.losses.CategoricalCrossentropy(from_logits=True)\n\nmodel.compile(optimizer=optimizer, loss=loss_fn, metrics=[\"accuracy\"])\n\n# Callbacks\ntrain_callbacks = [\n    EarlyStopping(monitor=\"val_accuracy\", patience=10, restore_best_weights=True),\n    ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=3, verbose=1)\n]\n\n# Train the model\nhistory = model.fit(\n    train_generator,\n    epochs=50,\n    validation_data=validation_generator,\n    callbacks=train_callbacks\n)\n\nmodel.save('flower_BiT_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-06-10T08:09:06.726978Z","iopub.execute_input":"2024-06-10T08:09:06.727749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to load and preprocess test images\ndef preprocess_image(img_path):\n    img = load_img(img_path, target_size=(img_width, img_height))\n    img_array = img_to_array(img)\n    img_array = np.expand_dims(img_array, axis=0)\n    img_array /= 255.0\n    return img_array\n\n# Initialize a list to hold the results\nresults = []\n\n# Loop through test images, make predictions, and save to results list\nfor i in range(1, 401):\n    img_path = os.path.join(test_path, f\"{i}.jpg\")\n    img_array = preprocess_image(img_path)\n    prediction = model.predict(img_array)\n    predicted_class_index = np.argmax(prediction)  # Get the index of the predicted class\n    results.append({'id': i, 'label': predicted_class_index})  # Save the index instead of the label\n\n# Convert results list to a DataFrame\nsubmission = pd.DataFrame(results)\n\n# Save the submission dataframe to a CSV file\nsubmission.to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-06-10T08:03:54.520473Z","iopub.execute_input":"2024-06-10T08:03:54.520831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model\nmodel.save('flower_BiT_model.h5')\n\n# Preprocess test images\ndef preprocess_image(img_path):\n    img = tf.keras.preprocessing.image.load_img(img_path, target_size=(IMG_WIDTH, IMG_HEIGHT))\n    img_array = tf.keras.preprocessing.image.img_to_array(img)\n    img_array = np.expand_dims(img_array, axis=0)\n    img_array /= 255.0\n    return img_array\n\n# Prepare the submission dataframe\nsubmission = pd.DataFrame(columns=['id', 'label'])\n\n# Class labels\nclass_labels = ['daisy', 'dandelion', 'rose', 'sunflower', 'tulip']\n\n# Loop through test images, make predictions, and save to submission dataframe\nfor i in range(1, 401):\n    img_path = os.path.join(TEST_PATH, f\"{i}.jpg\")\n    img_array = preprocess_image(img_path)\n    prediction = model.predict(img_array)\n    predicted_class = class_labels[np.argmax(prediction)]\n    submission = submission.append({'id': i, 'label': predicted_class}, ignore_index=True)\n\n# Save the submission dataframe to a CSV file\nsubmission.to_csv('submission.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]}]}