{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3551029,"sourceType":"datasetVersion","datasetId":1946928}],"dockerImageVersionId":30132,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pickle \nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport random\nfrom tensorflow import keras\nfrom tensorflow.keras import layers, callbacks\nfrom tensorflow.keras.utils import normalize\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.applications.vgg16 import VGG16\nfrom sklearn.preprocessing import OneHotEncoder\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.layers.experimental import preprocessing \nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:29:10.523750Z","iopub.execute_input":"2024-02-09T09:29:10.524155Z","iopub.status.idle":"2024-02-09T09:29:22.440260Z","shell.execute_reply.started":"2024-02-09T09:29:10.524044Z","shell.execute_reply":"2024-02-09T09:29:22.439319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datadir = r'/kaggle/input/flowers/flowers'","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:29:22.441935Z","iopub.execute_input":"2024-02-09T09:29:22.442187Z","iopub.status.idle":"2024-02-09T09:29:22.446133Z","shell.execute_reply.started":"2024-02-09T09:29:22.442157Z","shell.execute_reply":"2024-02-09T09:29:22.445333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Categories = []\nfor cat in os.listdir(datadir):\n    Categories.append(cat)","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:29:22.447144Z","iopub.execute_input":"2024-02-09T09:29:22.447349Z","iopub.status.idle":"2024-02-09T09:29:22.465175Z","shell.execute_reply.started":"2024-02-09T09:29:22.447323Z","shell.execute_reply":"2024-02-09T09:29:22.464416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Reading Image","metadata":{}},{"cell_type":"code","source":"img_size=(100, 100)\nfor cat in Categories:\n    img_path = os.path.join(datadir, '/kaggle/input/flowers/flowers/bellflower')\n    for img_dir in os.listdir(img_path):\n        img = cv2.imread(os.path.join(img_path, img_dir), cv2.COLOR_BGR2RGB)\n        img_array = cv2.resize(img, img_size)\n        img_resized = cv2.resize(img_array, img_size)\n        plt.imshow(img_resized)\n        plt.show()\n        break\n    break","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:29:22.466938Z","iopub.execute_input":"2024-02-09T09:29:22.467190Z","iopub.status.idle":"2024-02-09T09:29:23.014880Z","shell.execute_reply.started":"2024-02-09T09:29:22.467161Z","shell.execute_reply":"2024-02-09T09:29:23.014099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating Dataset","metadata":{}},{"cell_type":"code","source":"flower_data = []\nimg_size = (100, 100)\nfor cat in Categories:\n    path = os.path.join(datadir, cat)\n    class_num = Categories.index(cat)\n    for img in os.listdir(path):\n        img_array = cv2.imread(os.path.join(path, img), cv2.COLOR_BGR2RGB)\n        img_array = cv2.resize(img_array, img_size)\n        flower_data.append([img_array, class_num])","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:29:23.015847Z","iopub.execute_input":"2024-02-09T09:29:23.016045Z","iopub.status.idle":"2024-02-09T09:31:36.120065Z","shell.execute_reply.started":"2024-02-09T09:29:23.016022Z","shell.execute_reply":"2024-02-09T09:31:36.119189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.shuffle(flower_data)","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:31:36.121233Z","iopub.execute_input":"2024-02-09T09:31:36.121500Z","iopub.status.idle":"2024-02-09T09:31:36.146947Z","shell.execute_reply.started":"2024-02-09T09:31:36.121454Z","shell.execute_reply":"2024-02-09T09:31:36.146169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = []\ny = []\nfor img, label in flower_data:\n    X.append(img)\n    y.append(label)","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:31:36.148036Z","iopub.execute_input":"2024-02-09T09:31:36.148267Z","iopub.status.idle":"2024-02-09T09:31:36.165463Z","shell.execute_reply.started":"2024-02-09T09:31:36.148239Z","shell.execute_reply":"2024-02-09T09:31:36.164816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def make_rgb(img):\n    if len(img.shape) == 3:\n        return img\n    img3 = np.empty(img.shape + (3,))\n    img3[:, :, :] = img[:, :, np.newaxis]\n    return img3\n\nX = [make_rgb(im) for im in X]","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:31:36.166418Z","iopub.execute_input":"2024-02-09T09:31:36.166668Z","iopub.status.idle":"2024-02-09T09:31:36.196266Z","shell.execute_reply.started":"2024-02-09T09:31:36.166641Z","shell.execute_reply":"2024-02-09T09:31:36.195543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Standardizing X and y","metadata":{}},{"cell_type":"code","source":"X = np.array(X).reshape(-1, 100, 100, 3)\ny = np.array(y)","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:31:36.197439Z","iopub.execute_input":"2024-02-09T09:31:36.198010Z","iopub.status.idle":"2024-02-09T09:31:37.119559Z","shell.execute_reply.started":"2024-02-09T09:31:36.197972Z","shell.execute_reply":"2024-02-09T09:31:37.118706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = keras.utils.normalize(\n    X, axis=1, order=2\n)","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:31:37.122354Z","iopub.execute_input":"2024-02-09T09:31:37.122710Z","iopub.status.idle":"2024-02-09T09:31:39.887830Z","shell.execute_reply.started":"2024-02-09T09:31:37.122679Z","shell.execute_reply":"2024-02-09T09:31:39.886961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = to_categorical(y)","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:31:39.888928Z","iopub.execute_input":"2024-02-09T09:31:39.889129Z","iopub.status.idle":"2024-02-09T09:31:39.893309Z","shell.execute_reply.started":"2024-02-09T09:31:39.889106Z","shell.execute_reply":"2024-02-09T09:31:39.892477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting Training and Test data","metadata":{}},{"cell_type":"code","source":"X_train, X_tvalid, y_train, y_tvalid = train_test_split(X, y)","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:32:22.874244Z","iopub.execute_input":"2024-02-09T09:32:22.874515Z","iopub.status.idle":"2024-02-09T09:32:23.987726Z","shell.execute_reply.started":"2024-02-09T09:32:22.874464Z","shell.execute_reply":"2024-02-09T09:32:23.987017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_valid, X_test, y_valid, y_test = train_test_split(X_tvalid, y_tvalid)","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:32:23.988882Z","iopub.execute_input":"2024-02-09T09:32:23.989174Z","iopub.status.idle":"2024-02-09T09:32:24.264879Z","shell.execute_reply.started":"2024-02-09T09:32:23.989140Z","shell.execute_reply":"2024-02-09T09:32:24.264243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Devloping Model","metadata":{}},{"cell_type":"code","source":"early_stopping = callbacks.EarlyStopping(\n    patience = 15,\n    restore_best_weights = True,\n    verbose = 1,\n    min_delta = 0.001,\n)","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:32:24.265823Z","iopub.execute_input":"2024-02-09T09:32:24.266026Z","iopub.status.idle":"2024-02-09T09:32:24.270421Z","shell.execute_reply.started":"2024-02-09T09:32:24.266002Z","shell.execute_reply":"2024-02-09T09:32:24.269660Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential([\n    preprocessing.RandomFlip('horizontal'),\n    preprocessing.RandomRotation(factor=0.10),\n    \n    layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', input_shape=(100, 100, 3), padding='Same'),\n    layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding='Same'),\n    layers.MaxPool2D(pool_size=(2,2)),\n    layers.BatchNormalization(),\n    \n    layers.Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='Same'),\n    layers.Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='Same'),\n    layers.Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='Same'),\n    layers.MaxPool2D(pool_size=(2, 2)),\n    layers.BatchNormalization(),\n    \n    layers.Conv2D(filters=256, kernel_size=(3, 3), activation='relu', padding='Same'),\n    layers.MaxPool2D(pool_size=(2, 2)),\n    layers.BatchNormalization(),\n\n    layers.Flatten(),\n    layers.Dense(units=512, activation='relu'),\n    layers.Dropout(0.2),\n    layers.BatchNormalization(),\n    \n    layers.Flatten(),\n    layers.Dense(units=512, activation='relu'),\n    layers.Dropout(0.2),\n    layers.BatchNormalization(),\n    \n    layers.Dense(units=512, activation='relu'),\n    layers.Dropout(0.2),\n    layers.BatchNormalization(),\n    \n    layers.Dense(units=512, activation='relu'),\n    layers.Dropout(0.2),\n    layers.BatchNormalization(),\n    \n    layers.Dense(units=512, activation='relu'),\n    layers.Dropout(0.2),\n    layers.BatchNormalization(),\n    \n    layers.Dense(units=524, activation='relu'),\n    layers.Dropout(0.2),\n    layers.BatchNormalization(),\n    \n    layers.Dense(units=524, activation='relu'),\n    layers.Dropout(0.2),\n    layers.BatchNormalization(),\n    \n    layers.Dense(units=524, activation='relu'),\n    layers.Dropout(0.2),\n    layers.BatchNormalization(),\n    \n    layers.Dense(units=16, activation='softmax'),\n])","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:32:24.271957Z","iopub.execute_input":"2024-02-09T09:32:24.272241Z","iopub.status.idle":"2024-02-09T09:32:28.533504Z","shell.execute_reply.started":"2024-02-09T09:32:24.272205Z","shell.execute_reply":"2024-02-09T09:32:28.532826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer = 'adam',\n    loss = 'categorical_crossentropy',\n    metrics = ['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:32:28.534566Z","iopub.execute_input":"2024-02-09T09:32:28.534791Z","iopub.status.idle":"2024-02-09T09:32:28.552866Z","shell.execute_reply.started":"2024-02-09T09:32:28.534764Z","shell.execute_reply":"2024-02-09T09:32:28.552079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    X_train, y_train,\n    validation_data = (X_valid, y_valid),\n    epochs = 100,\n    batch_size = 64, \n    callbacks = [early_stopping]\n)","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:32:28.554320Z","iopub.execute_input":"2024-02-09T09:32:28.554626Z","iopub.status.idle":"2024-02-09T09:51:41.179635Z","shell.execute_reply.started":"2024-02-09T09:32:28.554591Z","shell.execute_reply":"2024-02-09T09:51:41.178781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:51:41.180938Z","iopub.execute_input":"2024-02-09T09:51:41.181177Z","iopub.status.idle":"2024-02-09T09:51:41.197892Z","shell.execute_reply.started":"2024-02-09T09:51:41.181146Z","shell.execute_reply":"2024-02-09T09:51:41.197075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plotting the results","metadata":{}},{"cell_type":"code","source":"sns.lineplot(data = history_df.loc[:, ['loss', 'val_loss']])","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:51:41.198895Z","iopub.execute_input":"2024-02-09T09:51:41.199113Z","iopub.status.idle":"2024-02-09T09:51:41.569281Z","shell.execute_reply.started":"2024-02-09T09:51:41.199088Z","shell.execute_reply":"2024-02-09T09:51:41.568568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.lineplot(data = history_df.loc[:, ['accuracy', 'val_accuracy']])","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:51:41.570361Z","iopub.execute_input":"2024-02-09T09:51:41.570599Z","iopub.status.idle":"2024-02-09T09:51:41.873422Z","shell.execute_reply.started":"2024-02-09T09:51:41.570572Z","shell.execute_reply":"2024-02-09T09:51:41.872719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluating on test data","metadata":{}},{"cell_type":"code","source":"model.evaluate(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:51:41.874581Z","iopub.execute_input":"2024-02-09T09:51:41.874838Z","iopub.status.idle":"2024-02-09T09:51:44.031659Z","shell.execute_reply.started":"2024-02-09T09:51:41.874808Z","shell.execute_reply":"2024-02-09T09:51:44.030999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(X_test)\nfor i in range(10):\n    plt.imshow(X_test[i])\n    plt.xlabel(('Actual: ', Categories[np.argmax(y_test[i])],'Predicted: ',Categories[np.argmax(prediction[i])]))\n    plt.xticks([])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-09T09:51:44.032696Z","iopub.execute_input":"2024-02-09T09:51:44.032926Z","iopub.status.idle":"2024-02-09T09:51:46.975290Z","shell.execute_reply.started":"2024-02-09T09:51:44.032898Z","shell.execute_reply":"2024-02-09T09:51:46.974447Z"},"trusted":true},"execution_count":null,"outputs":[]}]}