# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/ayushabhinav/FlowerClassification/blob/main/FlowerClassification.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + colab={"base_uri": "https://localhost:8080/"} id="Pv_R4mqoeaM2" outputId="f9e62c93-c0a5-406e-f04e-dbb9715d1136"
# !pip install kaggle

# + colab={"resources": {"http://localhost:8080/nbextensions/google.colab/files.js": {"data": "Ly8gQ29weXJpZ2h0IDIwMTcgR29vZ2xlIExMQwovLwovLyBMaWNlbnNlZCB1bmRlciB0aGUgQXBhY2hlIExpY2Vuc2UsIFZlcnNpb24gMi4wICh0aGUgIkxpY2Vuc2UiKTsKLy8geW91IG1heSBub3QgdXNlIHRoaXMgZmlsZSBleGNlcHQgaW4gY29tcGxpYW5jZSB3aXRoIHRoZSBMaWNlbnNlLgovLyBZb3UgbWF5IG9idGFpbiBhIGNvcHkgb2YgdGhlIExpY2Vuc2UgYXQKLy8KLy8gICAgICBodHRwOi8vd3d3LmFwYWNoZS5vcmcvbGljZW5zZXMvTElDRU5TRS0yLjAKLy8KLy8gVW5sZXNzIHJlcXVpcmVkIGJ5IGFwcGxpY2FibGUgbGF3IG9yIGFncmVlZCB0byBpbiB3cml0aW5nLCBzb2Z0d2FyZQovLyBkaXN0cmlidXRlZCB1bmRlciB0aGUgTGljZW5zZSBpcyBkaXN0cmlidXRlZCBvbiBhbiAiQVMgSVMiIEJBU0lTLAovLyBXSVRIT1VUIFdBUlJBTlRJRVMgT1IgQ09ORElUSU9OUyBPRiBBTlkgS0lORCwgZWl0aGVyIGV4cHJlc3Mgb3IgaW1wbGllZC4KLy8gU2VlIHRoZSBMaWNlbnNlIGZvciB0aGUgc3BlY2lmaWMgbGFuZ3VhZ2U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JvbUNoYXJDb2RlLmFwcGx5KG51bGwsIGNodW5rKSk7CiAgICAgIHlpZWxkIHsKICAgICAgICByZXNwb25zZTogewogICAgICAgICAgYWN0aW9uOiAnYXBwZW5kJywKICAgICAgICAgIGZpbGU6IGZpbGUubmFtZSwKICAgICAgICAgIGRhdGE6IGJhc2U2NCwKICAgICAgICB9LAogICAgICB9OwoKICAgICAgbGV0IHBlcmNlbnREb25lID0gZmlsZURhdGEuYnl0ZUxlbmd0aCA9PT0gMCA/CiAgICAgICAgICAxMDAgOgogICAgICAgICAgTWF0aC5yb3VuZCgocG9zaXRpb24gLyBmaWxlRGF0YS5ieXRlTGVuZ3RoKSAqIDEwMCk7CiAgICAgIHBlcmNlbnQudGV4dENvbnRlbnQgPSBgJHtwZXJjZW50RG9uZX0lIGRvbmVgOwoKICAgIH0gd2hpbGUgKHBvc2l0aW9uIDwgZmlsZURhdGEuYnl0ZUxlbmd0aCk7CiAgfQoKICAvLyBBbGwgZG9uZS4KICB5aWVsZCB7CiAgICByZXNwb25zZTogewogICAgICBhY3Rpb246ICdjb21wbGV0ZScsCiAgICB9CiAgfTsKfQoKc2NvcGUuZ29vZ2xlID0gc2NvcGUuZ29vZ2xlIHx8IHt9OwpzY29wZS5nb29nbGUuY29sYWIgPSBzY29wZS5nb29nbGUuY29sYWIgfHwge307CnNjb3BlLmdvb2dsZS5jb2xhYi5fZmlsZXMgPSB7CiAgX3VwbG9hZEZpbGVzLAogIF91cGxvYWRGaWxlc0NvbnRpbnVlLAp9Owp9KShzZWxmKTsK", "ok": true, "headers": [["content-type", "application/javascript"]], "status": 200, "status_text": ""}}, "base_uri": "https://localhost:8080/", "height": 89} id="JnFHmqCcefS8" outputId="9d76ce93-d5ac-4f51-b34e-eb0dfe1ce4f6"
from google.colab import files
files.upload()

# + colab={"base_uri": "https://localhost:8080/"} id="coW4Ab-tfEn9" outputId="20bcd9df-8865-4254-97dd-063dd60599de"
# !mkdir ~/.kaggle
# !cp kaggle.json ~/.kaggle
# !chmod 600 ~/.kaggle/kaggle.json
# ! kaggle datasets download -d alxmamaev/flowers-recognition
# ! ls -lrt

# + colab={"base_uri": "https://localhost:8080/"} id="apxOO-MSfkPu" outputId="0b605d48-590f-4519-ba99-faecd6876a60"
# !unzip -q flowers-recognition.zip
# ! ls -lrt flowers

# + id="UBBt-lA1hmi5"
# imports

import os
import glob
import random
import shutil

import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt

# + id="oyVoCghKhea8"
#setting up folder paths
dataset_path = os.path.join(os.getcwd(), 'flowers')

# + colab={"base_uri": "https://localhost:8080/"} id="PRvRaBF1Zrt0" outputId="8ebbee1d-5fd8-40ed-f3f4-0c4707e606cb"
# creating the test data

# ! mkdir flower_test
test_dataset = os.path.join(os.getcwd(), 'flowers_test')
test_data_size = 0.1
for folder in os.listdir(dataset_path):
  if not (os.path.exists(os.path.join(test_dataset, folder))):
    os.makedirs(os.path.join(test_dataset, folder))

  files = os.listdir(os.path.join(dataset_path, folder))
  
  k = int(np.floor(len(files) * test_data_size))
  test_files = random.sample(files, k)
  [shutil.move(os.path.join(dataset_path, folder, f), os.path.join(test_dataset, folder, f)) for f in test_files ]
  

# + colab={"base_uri": "https://localhost:8080/", "height": 204} id="4Ria90sxjFYO" outputId="8c9f9333-75f6-4472-b95d-87d0d632dba6"
#count of images in each class
flower_type_count = pd.DataFrame( [[c, len(os.listdir(os.path.join(dataset_path, c)))] for c in os.listdir(dataset_path)], columns=['name', 'count'])
flower_type_count


# + id="NEQdsXFHmWrS"
def plot_images(images, title):
  fig, axes = plt.subplots(1, len(images))
  fig.set_figwidth(10)
  for ax, img in zip(axes, images):
    ax.imshow(img)
    ax.axis('off')
  plt.suptitle(c, fontsize=20, fontweight='bold')
  plt.show()


# + colab={"base_uri": "https://localhost:8080/", "height": 1000} id="jVNKQRVjjj7_" outputId="48c7bf81-03e0-4e60-d6f6-41eee00aa283"
# visualizing some flowers from each class
for c in os.listdir(dataset_path):
  cpath = os.path.join(dataset_path, c)
  image_files = [random.choice(os.listdir(cpath))for i in range(5)]
  images = [plt.imread(os.path.join(cpath, image_file)) for image_file in image_files]
  plot_images(images, title=c)
  print()
    

# + [markdown] id="SI00lqA6kFhE"
# ### Preparing Data fro Binary Classification

# + colab={"base_uri": "https://localhost:8080/"} id="975M0Lk4kMGy" outputId="a4a69c17-3bd2-49c7-a861-eeb482d94bf7"
# !mkdir flowers_binary
# !ln -s /content/flowers/rose /content/flowers_binary/rose
# !ln -s /content/flowers/daisy /content/flowers_binary/daisy

# !mkdir flowers_binary_test
# !ln -s /content/flowers_test/rose /content/flowers_binary_test/rose
# !ln -s /content/flowers_test/daisy /content/flowers_binary_test/daisy

# + id="SZtuG-88k7Rk"
binary_dataset_path = os.path.join(os.getcwd(), 'flowers_binary')
binary_test_dataset_path = os.path.join(os.getcwd(), 'flowers_binary_test')

# + [markdown] id="cChxQrqwZnji"
# ### Custom Data Generator

# + id="uuTmXoApZqsn"
import tensorflow as tf
from tensorflow import keras

# + id="y26zXmyIZqoK"
# class MyDataGenerator(keras.utils.Sequence):

#   def __init__(self, dataset_path, mode='train', validation_perc=None, ablation=None, target_cls=None,
#                batch_size=32, dim=(256, 256), preprocessing_fn=None, n_channel=3, shuffle=True):
    
#     self.batch_size = batch_size
#     self.preprocessing_fn = preprocessing_fn
#     self.n_channel = n_channel
#     self.shuffle = shuffle
#     self.dim = dim
#     self.labels = {}
#     self.image_ID = []

#     if target_cls is None:
#       self.target_cls = os.listdir(dataset_path)
#     else:
#       self.target_cls = target_cls

#     self.n_classes = len(self.target_cls)


#     for i, cls in enumerate(self.target_cls):
#       paths = glob.glob(os.path.join(dataset_path, cls, '*'))
      
#       brk_point = len(paths)
#       if validation_perc is not None:
#         brk_point = int(len(paths) * (1- validation_perc))
      
#       if mode == 'train':
#         paths = paths[:brk_point]
#       else:
#         paths = paths[brk_point:]

#       if ablation is not None:
#         abl_point = int(len(paths) * ablation)
#         paths = paths[:abl_point]

#       self.image_ID += paths
#       self.labels.update({img:i for img in paths})

#     self.on_epoch_end()



#   def on_epoch_end(self):
#     self.indices = np.arange(len(self.image_ID))
#     if self.shuffle:
#       np.random.shuffle(self.indices)
      
#   def __len__(self):
#     return int(np.floor(len(self.image_ID)/self.batch_size ))


#   def __iter__(self):
#     for item in ( self.__getitem__(i) for i in range(len(self))):
#       yield item

#   def __getitem__(self, index):
#     print(index)
#     print(index * self.batch_size , (index +1) * self.batch_size)
#     indexes = self.indices[ index * self.batch_size : (index +1) * self.batch_size ]
#     ids = [ self.image_ID[idx] for idx in indexes]
#     X, y = self._generate_data(ids)
#     print(y.shape)
#     return X, y

#   def _generate_data(self, ids):

#     X = np.empty((self.batch_size, *self.dim, self.n_channel))
#     y = np.empty((self.batch_size), dtype=np.int)

#     for idx , id in enumerate(ids):
#       img = plt.imread(id)
#       img = np.resize(img, (*self.dim, self.n_channel))
#       # print(f' image shape :{img.shape}')
#       if self.preprocessing_fn is not None:
#         img = self.preprocessing_fn(img)
#         # print(f'processed image shape : f{img.shape}')
#       X[idx] = img
#       y[idx] = self.labels[id]

#     y = keras.utils.to_categorical(y, self.n_classes)
      
#     return X, y


# + id="p7wnDXObgxoU"
# train_datagen = MyDataGenerator(dataset_path, target_cls=['rose', 'daisy'], validation_perc=.2, ablation=0.1, mode='train', dim=(224, 224), preprocessing_fn=keras.applications.resnet50.preprocess_input)
# valid_datagen = MyDataGenerator(dataset_path, target_cls=['rose', 'daisy'], validation_perc=.2, mode='validation', dim=(224, 224) preprocessing_fn=keras.applications.resnet50.preprocess_input)

# + id="QanqUBrIAylD"
# for d in train_datagen:
#   print(d[0].shape)

# + [markdown] id="JXNQrBHUZjSO"
# ### Model Building for binary classification

# + [markdown] id="Q2DZUjoEllcE"
# #### Data Generator

# + colab={"base_uri": "https://localhost:8080/"} id="RlkLFKtelr_O" outputId="121aca36-721b-4666-9dfc-921cf100e944"
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.resnet50 import preprocess_input

prepro_fn = preprocess_input

# for ablation experiment
ablation_datagen = ImageDataGenerator(validation_split=0.9, preprocessing_function=prepro_fn )
ablation_train_data = ablation_datagen.flow_from_directory(binary_dataset_path, subset='training')
ablation_validation_data = ablation_datagen.flow_from_directory(binary_dataset_path, subset='validation')

# for final training
datagen  = ImageDataGenerator(validation_split=0.2,  preprocessing_function=prepro_fn )
train_data = datagen.flow_from_directory( binary_dataset_path, subset='training')
validation_data = datagen.flow_from_directory( binary_dataset_path, subset='validation')


test_datagen = ImageDataGenerator(preprocessing_function=prepro_fn )
test_data = test_datagen.flow_from_directory(binary_test_dataset_path)

# + id="_iyBHaGgvO4X"
# for d in ablation_train_data:
#   print(d[0].shape)

# + [markdown] id="cT7csMRxJa7C"
# #### Model

# + id="m9lwMARQllKT"
import tensorflow as tf
from tensorflow import keras

# + id="FYzyajZjsOx8"
base_model = tf.keras.applications.ResNet50(
    include_top=False,
    weights="imagenet",
)

# + id="T75OPk3as5Es"
# building the model

image_dim = (224, 224)

inputs = keras.layers.Input(shape=[256,256, 3])
X = keras.layers.Resizing(*image_dim)(inputs)
X = base_model(inputs, training=False)
X = keras.layers.GlobalAveragePooling2D()(X)
X = keras.layers.Dropout(0.2)(X)
X = keras.layers.Dense(128, activation='relu')(X)
X = keras.layers.Dropout(0.5)(X)
outputs = keras.layers.Dense(2, activation='softmax')(X)

model = keras.Model(inputs, outputs)


# + colab={"base_uri": "https://localhost:8080/"} id="za2JV-LBvXfC" outputId="ad1a107e-33b8-4ff5-e58d-eb12fe8857dc"
model.summary()

# + id="xeDJKmZZdLzE"
earlystopping    = tf.keras.callbacks.EarlyStopping(patience=3, monitor='val_accuracy', mode='max', restore_best_weights=True)

filepath = os.path.join(os.getcwd(), 'flower_model_binary')
if not os.path.exists(filepath):
  os.makedirs(filepath)
model_checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath, monitor='val_accuracy', verbose=0, save_best_only=True, mode='max')

# + id="9mtV-SNSs7Sy"
opt = tf.keras.optimizers.SGD(learning_rate=0.01)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy', tf.keras.metrics.AUC()])

# + colab={"base_uri": "https://localhost:8080/"} id="alu4vMC1u9Xs" outputId="42ad24b7-a1f8-4db7-d6a7-1366e6b88e3d"
history = model.fit(ablation_train_data, validation_data=validation_data, epochs=10, callbacks=[earlystopping, model_checkpoint])

# + colab={"base_uri": "https://localhost:8080/", "height": 265} id="6LC0H_9oAGsV" outputId="c8cb69b9-33ef-406f-c443-342b31882b23"
plt.plot(history.history['accuracy'], label='Accuracy')
plt.plot(history.history['val_accuracy'], label='Val_Acc')

plt.plot(history.history['loss'], label='Loss')
plt.plot(history.history['val_loss'], label='Val_loss')

plt.legend()
plt.show()


# + [markdown] id="cDlWD6YIAAvC"
# #### Running the model on complete data

# + id="fXySg_qtLncf" colab={"base_uri": "https://localhost:8080/"} outputId="4f7110fc-32b3-4561-a9c6-578c23436cee"
opt = tf.keras.optimizers.SGD(learning_rate=0.01)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy', tf.keras.metrics.AUC()])

history = model.fit(train_data, validation_data=validation_data, epochs=10, callbacks=[earlystopping, model_checkpoint])

# + colab={"base_uri": "https://localhost:8080/"} id="4BwkFfptGpLp" outputId="153ded84-2bc1-4059-f32d-f3dae6d08e25"
final_binary_model = keras.models.load_model(filepath)
final_binary_model.evaluate(test_data)

# + [markdown] id="9DcPePvDJibT"
# ### Model Building for Multi class classification 

# + [markdown] id="HGPr3D9xJ7XD"
# #### Data Generator

# + colab={"base_uri": "https://localhost:8080/"} id="ytgpneWyJ7XE" outputId="d924daf1-29ee-4665-b3c7-d6cd9ffe964d"
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.resnet50 import preprocess_input

prepro_fn = preprocess_input

# for ablation experiment
ablation_datagen = ImageDataGenerator(validation_split=0.9, preprocessing_function=prepro_fn )
ablation_train_data = ablation_datagen.flow_from_directory(dataset_path, subset='training')
ablation_validation_data = ablation_datagen.flow_from_directory(dataset_path, subset='validation')

# for final training
datagen  = ImageDataGenerator(validation_split=0.2,  preprocessing_function=prepro_fn )
train_data = datagen.flow_from_directory(dataset_path, subset='training')
validation_data = datagen.flow_from_directory(dataset_path, subset='validation')


test_datagen  = ImageDataGenerator(preprocessing_function=prepro_fn)
test_data = test_datagen.flow_from_directory(test_dataset)

# + [markdown] id="6RAD1n3zKTU9"
# #### Model

# + id="jy5Qx2hwKTU9"
import tensorflow as tf
from tensorflow import keras

# + id="qKZWHQm-KTU9"
base_model = tf.keras.applications.ResNet50(
    include_top=False,
    weights="imagenet",
)

# + id="zChORrfVKTU-"
# building the model

image_dim = (224, 224)

inputs = keras.layers.Input(shape=[256,256, 3])
X = keras.layers.Resizing(*image_dim)(inputs)
X = base_model(inputs, training=False)
X = keras.layers.GlobalAveragePooling2D()(X)
X = keras.layers.Dropout(0.2)(X)
X = keras.layers.Dense(128, activation='relu')(X)
X = keras.layers.Dropout(0.5)(X)
outputs = keras.layers.Dense(5, activation='softmax')(X)

flower_model = keras.Model(inputs, outputs)


# + colab={"base_uri": "https://localhost:8080/"} id="UfRsec5UKTU-" outputId="e7b17ce5-7a25-4623-e5ba-6c598ae12415"
flower_model.summary()

# + id="kM9Ox0DBQZ0k"
earlystopping    = tf.keras.callbacks.EarlyStopping(patience=3, monitor='val_accuracy', mode='max', restore_best_weights=True)

filepath = os.path.join(os.getcwd(), 'flower_model_multiclass')
if not os.path.exists(filepath):
  os.makedirs(filepath)
model_checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath, monitor='val_accuracy', verbose=0, save_best_only=True, mode='max')

# + id="r6r1_LthKTU-"
opt = tf.keras.optimizers.SGD(learning_rate=0.01)
flower_model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy', tf.keras.metrics.AUC(multi_label=True, num_labels=5)])

# + colab={"base_uri": "https://localhost:8080/"} id="lbyRHFUHKTU-" outputId="d3e77fbf-892d-4a5f-e396-f8d3a9b09754"
history = flower_model.fit(ablation_train_data, validation_data=validation_data, epochs=10, callbacks=[earlystopping, model_checkpoint])

# + colab={"base_uri": "https://localhost:8080/", "height": 265} id="8nV9MEFTKTU_" outputId="34bebfda-e805-4424-8da5-7b2d0f5b2d6f"
plt.plot(history.history['accuracy'], label='Accuracy')
plt.plot(history.history['val_accuracy'], label='Val_Acc')

plt.plot(history.history['loss'], label='Loss')
plt.plot(history.history['val_loss'], label='Val_loss')

plt.legend()
plt.show()


# + [markdown] id="1cwgKcCnMEsi"
# #### Running the model on complete data

# + colab={"base_uri": "https://localhost:8080/", "height": 1000} id="Mq9Uihm1MEsj" outputId="aaae925d-1224-4b33-f182-d54976a6372e"
opt = tf.keras.optimizers.SGD(learning_rate=0.01)
flower_model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy', tf.keras.metrics.AUC(multi_label=True, num_labels=5)])

history = flower_model.fit(train_data, validation_data=validation_data, epochs=10, callbacks=[earlystopping, model_checkpoint])

# + colab={"base_uri": "https://localhost:8080/"} id="TxaDH3z3YfVs" outputId="a1c18fc6-ac3b-44e5-80da-7f52ca158b79"
final_model = keras.models.load_model(filepath)
final_model.evaluate(test_data)

# + colab={"base_uri": "https://localhost:8080/"} id="-jwKMnPVYzkd" outputId="ca9dd317-2ed0-46ef-d065-c64d73764eb2"
prediction = final_model.predict(validation_data)
np.argmax(prediction, axis=1)

# + id="iF4vtVrsZaLh"


StarcoderdataJupyterScriptsDedupFiltered