{"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":"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\n# import numpy as np # linear algebra\n# import 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\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n#         break\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":"2023-10-17T15:50:08.247455Z","iopub.status.idle":"2023-10-17T15:50:08.247802Z","shell.execute_reply.started":"2023-10-17T15:50:08.247642Z","shell.execute_reply":"2023-10-17T15:50:08.247659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Import All Libraries","metadata":{}},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings('ignore')\n\nimport os\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport seaborn as sns\nfrom sklearn.metrics import classification_report , confusion_matrix , accuracy_score , auc\nfrom sklearn.model_selection import train_test_split\n\nimport cv2\n#from google.colab.patches import cv2_imshow\nfrom PIL import Image \nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras import Sequential\nfrom keras.layers import Input, Dense,Conv2D , MaxPooling2D, Flatten,BatchNormalization,Dropout\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory\nimport tensorflow_hub as hub \n\nfrom keras.applications.vgg19 import VGG19","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:52:52.521769Z","iopub.execute_input":"2023-10-18T05:52:52.522187Z","iopub.status.idle":"2023-10-18T05:53:04.224126Z","shell.execute_reply.started":"2023-10-18T05:52:52.522152Z","shell.execute_reply":"2023-10-18T05:53:04.222916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv_path = \"/kaggle/input/UBC-OCEAN/train.csv\"\ntest_csv_path = \"/kaggle/input/UBC-OCEAN/test.csv\"\n\ntrain_df = pd.read_csv(train_csv_path)\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:53:29.575234Z","iopub.execute_input":"2023-10-18T05:53:29.576054Z","iopub.status.idle":"2023-10-18T05:53:29.617327Z","shell.execute_reply.started":"2023-10-18T05:53:29.576014Z","shell.execute_reply":"2023-10-18T05:53:29.616191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x=train_df['label'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:53:29.932283Z","iopub.execute_input":"2023-10-18T05:53:29.932696Z","iopub.status.idle":"2023-10-18T05:53:30.181255Z","shell.execute_reply.started":"2023-10-18T05:53:29.932668Z","shell.execute_reply":"2023-10-18T05:53:30.180159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(train_df['image_width'],train_df['image_height'])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:53:30.183035Z","iopub.execute_input":"2023-10-18T05:53:30.183336Z","iopub.status.idle":"2023-10-18T05:53:30.423571Z","shell.execute_reply.started":"2023-10-18T05:53:30.183311Z","shell.execute_reply":"2023-10-18T05:53:30.422329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['is_tma'] = train_df['is_tma'].astype('int8')","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:53:30.425675Z","iopub.execute_input":"2023-10-18T05:53:30.426045Z","iopub.status.idle":"2023-10-18T05:53:30.432004Z","shell.execute_reply.started":"2023-10-18T05:53:30.426014Z","shell.execute_reply":"2023-10-18T05:53:30.430624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['is_tma'].value_counts().plot(kind=\"pie\",autopct=\"%.1f%%\")\nplt.title(\"Image Distributions on Train Data\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:53:30.433458Z","iopub.execute_input":"2023-10-18T05:53:30.433772Z","iopub.status.idle":"2023-10-18T05:53:30.626977Z","shell.execute_reply.started":"2023-10-18T05:53:30.433745Z","shell.execute_reply":"2023-10-18T05:53:30.625361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_thumbnails = os.listdir(\"/kaggle/input/UBC-OCEAN/train_thumbnails\")\ntrain_images = os.listdir(\"/kaggle/input/UBC-OCEAN/train_images\")","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:53:30.63057Z","iopub.execute_input":"2023-10-18T05:53:30.631633Z","iopub.status.idle":"2023-10-18T05:53:30.831348Z","shell.execute_reply.started":"2023-10-18T05:53:30.631572Z","shell.execute_reply":"2023-10-18T05:53:30.829754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels = ['CC', 'EC', 'HGSC', 'LGSC', 'MC']\ntrain_df['label'] = train_df['label'].replace({'CC':0, 'EC':1, 'HGSC':2, 'LGSC':3, 'MC':4})\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:53:30.832735Z","iopub.execute_input":"2023-10-18T05:53:30.833086Z","iopub.status.idle":"2023-10-18T05:53:30.846977Z","shell.execute_reply.started":"2023-10-18T05:53:30.833058Z","shell.execute_reply":"2023-10-18T05:53:30.845517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['image_id']=train_df['image_id'].astype(\"int32\")\ntrain_df['label']=train_df['label'].astype(\"int8\")\ntrain_df['image_width']=train_df['image_width'].astype(\"int32\")\ntrain_df['image_height']=train_df['image_height'].astype(\"int32\")","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:53:31.161133Z","iopub.execute_input":"2023-10-18T05:53:31.162112Z","iopub.status.idle":"2023-10-18T05:53:31.171225Z","shell.execute_reply.started":"2023-10-18T05:53:31.162069Z","shell.execute_reply":"2023-10-18T05:53:31.169907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_df['label'].value_counts()\ntrain_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:53:31.332097Z","iopub.execute_input":"2023-10-18T05:53:31.333001Z","iopub.status.idle":"2023-10-18T05:53:31.355167Z","shell.execute_reply.started":"2023-10-18T05:53:31.332952Z","shell.execute_reply":"2023-10-18T05:53:31.353954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:53:34.851887Z","iopub.execute_input":"2023-10-18T05:53:34.85281Z","iopub.status.idle":"2023-10-18T05:53:34.865062Z","shell.execute_reply.started":"2023-10-18T05:53:34.85277Z","shell.execute_reply":"2023-10-18T05:53:34.863853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training Images Preprocessing","metadata":{}},{"cell_type":"code","source":"Image.MAX_IMAGE_PIXELS = 10000000000\n# Define patch size and overlap (if needed)\npatch_size = (128,128)  # Adjust this according to your requirements\noverlap = 10  # Adjust this if you want overlapping patches\n\nimage_data = []\nimage_label = []\nempty_img=0\nfor img_id, label , tma in zip(train_df['image_id'],train_df['label'], train_df['is_tma']):\n    #print(img_id, label,  tma)\n    if tma==0:\n        img_name = str(img_id)+\"_thumbnail.png\"\n        large_image = Image.open(\"/kaggle/input/UBC-OCEAN/train_thumbnails/\"+img_name)\n        for y in range(0, large_image.height, patch_size[0] - overlap): # (0,2523,192)\n            for x in range(0, large_image.width, patch_size[1] - overlap):  # (0,3000,192)  224-32=192\n                patch = large_image.crop((x, y, x+patch_size[1], y+patch_size[0]))\n                image = np.array(patch)\n                if np.sum(image)==0:\n                    empty_img+=1\n                elif (np.sum(image[0:,0:50])==0) or (np.sum(image[0:,50:])==0) or (np.sum(image[0:,100:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:50,0:])==0) or (np.sum(image[50:,0:])==0) or (np.sum(image[100:,0:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:50,0:50])==0) or (np.sum(image[0:50,50:])==0):\n                    empty_img+=1\n                elif (np.sum(image[50:100,0:50])==0) or (np.sum(image[50:100,50:])==0):\n                    empty_img+=1\n                elif (np.sum(image[50:,0:100])==0) or (np.sum(image[80:,80:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:50,75:])==0) or (np.sum(image[50:,75:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:40,80:])==0) or (np.sum(image[80:,80:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:35,0:])==0) or (np.sum(image[80:,0:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:25,0:50])==0) or (np.sum(image[0:25,100:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:20,0:25])==0) or (np.sum(image[0:20,25:50])==0) or (np.sum(image[0:20,50:80])==0) or (np.sum(image[0:20,90:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:25,0:25])==0) or (np.sum(image[0:25,100:])==0) or (np.sum(image[100:,0:25])==0) or (np.sum(image[100:,100:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:15,0:])==0) or (np.sum(image[115:,0:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:,0:15])==0) or (np.sum(image[0:,115:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:20,0:20])==0) or (np.sum(image[0:20,110:])==0) or (np.sum(image[110:,0:20])==0) or (np.sum(image[110:,110:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:10,0:10])==0) or (np.sum(image[0:10,115:])==0) or (np.sum(image[0:10,40:60])==0) or (np.sum(image[0:10,80:100])==0):\n                    empty_img+=1\n                elif (np.sum(image[40:50,0:10])==0) or (np.sum(image[110:,80:100])==0) or (np.sum(image[70:85,70:90])==0) or (np.sum(image[50:60,110:])==0):\n                    empty_img+=1\n\n                else:\n                    image_data.append(image)\n                    image_label.append(label)\n        \n    elif tma==1:\n        img_name = str(img_id)+\".png\"\n        large_image = Image.open(\"/kaggle/input/UBC-OCEAN/train_images/\"+img_name)\n        for y in range(0, large_image.height, patch_size[0] - overlap): # (0,2523,192)\n            for x in range(0, large_image.width, patch_size[1] - overlap):  # (0,3000,192)  224-32=192\n                patch = large_image.crop((x, y, x+patch_size[1], y+patch_size[0]))\n                image = np.array(patch)\n                if np.sum(image)==0:\n                    empty_img+=1\n                elif (np.sum(image[0:,0:50])==0) or (np.sum(image[0:,50:])==0) or (np.sum(image[0:,100:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:50,0:])==0) or (np.sum(image[50:,0:])==0) or (np.sum(image[100:,0:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:50,0:50])==0) or (np.sum(image[0:50,50:])==0):\n                    empty_img+=1\n                elif (np.sum(image[50:100,0:50])==0) or (np.sum(image[50:100,50:])==0):\n                    empty_img+=1\n                elif (np.sum(image[50:,0:100])==0) or (np.sum(image[80:,80:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:50,75:])==0) or (np.sum(image[50:,75:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:40,80:])==0) or (np.sum(image[80:,80:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:35,0:])==0) or (np.sum(image[80:,0:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:25,0:50])==0) or (np.sum(image[0:25,100:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:20,0:25])==0) or (np.sum(image[0:20,25:50])==0) or (np.sum(image[0:20,50:80])==0) or (np.sum(image[0:20,90:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:25,0:25])==0) or (np.sum(image[0:25,100:])==0) or (np.sum(image[100:,0:25])==0) or (np.sum(image[100:,100:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:15,0:])==0) or (np.sum(image[115:,0:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:,0:15])==0) or (np.sum(image[0:,115:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:20,0:20])==0) or (np.sum(image[0:20,110:])==0) or (np.sum(image[110:,0:20])==0) or (np.sum(image[110:,110:])==0):\n                    empty_img+=1\n                elif (np.sum(image[0:10,0:10])==0) or (np.sum(image[0:10,115:])==0) or (np.sum(image[0:10,40:60])==0) or (np.sum(image[0:10,80:100])==0):\n                    empty_img+=1\n                elif (np.sum(image[40:50,0:10])==0) or (np.sum(image[110:,80:100])==0) or (np.sum(image[70:85,70:90])==0) or (np.sum(image[50:60,110:])==0):\n                    empty_img+=1\n                \n                    \n                else:\n                    image_data.append(image)\n                    image_label.append(label)","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:53:50.1075Z","iopub.execute_input":"2023-10-18T05:53:50.108512Z","iopub.status.idle":"2023-10-18T05:58:01.962609Z","shell.execute_reply.started":"2023-10-18T05:53:50.108468Z","shell.execute_reply":"2023-10-18T05:58:01.961263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(image_data))\nprint(len(image_label))\nprint(empty_img)","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:58:01.964907Z","iopub.execute_input":"2023-10-18T05:58:01.9654Z","iopub.status.idle":"2023-10-18T05:58:01.972167Z","shell.execute_reply.started":"2023-10-18T05:58:01.965338Z","shell.execute_reply":"2023-10-18T05:58:01.971154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_data[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:49:01.62172Z","iopub.execute_input":"2023-10-18T05:49:01.622201Z","iopub.status.idle":"2023-10-18T05:49:01.635227Z","shell.execute_reply.started":"2023-10-18T05:49:01.622179Z","shell.execute_reply":"2023-10-18T05:49:01.634472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training Images Visualization","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(40,60))\nj=1\nfor i in range(500,650):\n    plt.subplot(15,10,j)\n    plt.imshow(image_data[i])\n    plt.title(f\"Label:{class_labels[image_label[i]]}\")\n    j+=1","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:08:21.221759Z","iopub.execute_input":"2023-10-18T03:08:21.222133Z","iopub.status.idle":"2023-10-18T03:09:01.184574Z","shell.execute_reply.started":"2023-10-18T03:08:21.222094Z","shell.execute_reply":"2023-10-18T03:09:01.182507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"set(image_label)","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:14:14.452959Z","iopub.execute_input":"2023-10-18T03:14:14.454276Z","iopub.status.idle":"2023-10-18T03:14:14.464401Z","shell.execute_reply.started":"2023-10-18T03:14:14.454223Z","shell.execute_reply":"2023-10-18T03:14:14.463211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Covert image data into array for training","metadata":{}},{"cell_type":"code","source":"x = np.array(image_data) \ny = np.array(image_label)","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:58:01.973679Z","iopub.execute_input":"2023-10-18T05:58:01.974016Z","iopub.status.idle":"2023-10-18T05:58:05.107865Z","shell.execute_reply.started":"2023-10-18T05:58:01.973972Z","shell.execute_reply":"2023-10-18T05:58:05.106635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:58:05.110135Z","iopub.execute_input":"2023-10-18T05:58:05.110622Z","iopub.status.idle":"2023-10-18T05:58:05.115826Z","shell.execute_reply.started":"2023-10-18T05:58:05.110588Z","shell.execute_reply":"2023-10-18T05:58:05.11478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import joblib\n\n# # Save the NumPy array to a file\n#joblib.dump(x3, 'x_24000.joblib')\njoblib.dump(x, 'x_data.joblib')\njoblib.dump(y, 'y_data.joblib')","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:14:43.017314Z","iopub.execute_input":"2023-10-18T03:14:43.017669Z","iopub.status.idle":"2023-10-18T03:15:08.304942Z","shell.execute_reply.started":"2023-10-18T03:14:43.017642Z","shell.execute_reply":"2023-10-18T03:15:08.303831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import joblib\nx_data = joblib.load(\"/kaggle/working/x_data.joblib\")\ny_data = joblib.load(\"/kaggle/working/y_data.joblib\")\nprint(x_data.shape)\nprint(y_data.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:36:25.95483Z","iopub.execute_input":"2023-10-18T03:36:25.955249Z","iopub.status.idle":"2023-10-18T03:37:05.628255Z","shell.execute_reply.started":"2023-10-18T03:36:25.955216Z","shell.execute_reply":"2023-10-18T03:37:05.626964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split The Data","metadata":{}},{"cell_type":"code","source":"x1 = x[:1000]\ny1 = y[:1000]","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:58:42.49591Z","iopub.execute_input":"2023-10-18T05:58:42.496357Z","iopub.status.idle":"2023-10-18T05:58:42.502367Z","shell.execute_reply.started":"2023-10-18T05:58:42.496327Z","shell.execute_reply":"2023-10-18T05:58:42.501288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test ,y_train, y_test = train_test_split(x1, y1, test_size=0.20, shuffle=True)\nprint(x_train.shape)\nprint(x_test.shape)\nprint(y_train.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:58:50.783649Z","iopub.execute_input":"2023-10-18T05:58:50.784102Z","iopub.status.idle":"2023-10-18T05:58:50.818227Z","shell.execute_reply.started":"2023-10-18T05:58:50.784067Z","shell.execute_reply":"2023-10-18T05:58:50.816961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Scale The Data for Train images","metadata":{}},{"cell_type":"code","source":"x_train_scaled = x_train/255\nx_test_scaled = x_test/255","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:58:59.635307Z","iopub.execute_input":"2023-10-18T05:58:59.635797Z","iopub.status.idle":"2023-10-18T05:58:59.841463Z","shell.execute_reply.started":"2023-10-18T05:58:59.635756Z","shell.execute_reply":"2023-10-18T05:58:59.840191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train Image Visualization","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(40,40))\nfor i in range(100):\n    plt.subplot(10,10,i+1)\n    plt.imshow(x_train[i])\n    plt.title(f\"Label:{class_labels[y_train[i]]}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:16:51.150645Z","iopub.execute_input":"2023-10-18T03:16:51.151079Z","iopub.status.idle":"2023-10-18T03:17:17.986893Z","shell.execute_reply.started":"2023-10-18T03:16:51.151048Z","shell.execute_reply":"2023-10-18T03:17:17.98405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Test Image Visualization","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(40,40))\nfor i in range(100):\n    plt.subplot(10,10,i+1)\n    plt.imshow(x_test[i])\n    plt.title(f\"Label:{class_labels[y_test[i]]}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-18T03:19:15.285284Z","iopub.execute_input":"2023-10-18T03:19:15.28573Z","iopub.status.idle":"2023-10-18T03:19:41.656947Z","shell.execute_reply.started":"2023-10-18T03:19:15.285683Z","shell.execute_reply":"2023-10-18T03:19:41.65482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-10-16T04:28:59.009406Z","iopub.execute_input":"2023-10-16T04:28:59.009821Z","iopub.status.idle":"2023-10-16T04:29:09.342761Z","shell.execute_reply.started":"2023-10-16T04:28:59.009792Z","shell.execute_reply":"2023-10-16T04:29:09.341697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Building Using VGG16 Model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import VGG16\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense, Flatten\nfrom tensorflow.keras.optimizers import Adam\nnum_classes = 5\n# Load the VGG16 model with ImageNet weights and exclude the top classification layer\nbase_model = VGG16(weights='imagenet', include_top=False, input_shape=(128,128, 3))\n\n# Customize the top classification layers\nx = base_model.output\nx = Flatten()(x)\nx = Dense(4096, activation='relu')(x)\nx = Dense(4096, activation='relu')(x)\npredictions = Dense(num_classes, activation='softmax')(x)  # Replace num_classes with the number of classes in your dataset\n\n# Create the VGG16 model with your custom top layer\nmodel = Model(inputs=base_model.input, outputs=predictions)\n\n# Freeze pre-trained layers (optional)\nfor layer in base_model.layers:\n    layer.trainable = False\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-10-18T05:59:25.105205Z","iopub.execute_input":"2023-10-18T05:59:25.105673Z","iopub.status.idle":"2023-10-18T05:59:30.203447Z","shell.execute_reply.started":"2023-10-18T05:59:25.105635Z","shell.execute_reply":"2023-10-18T05:59:30.20219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer=Adam(lr=0.0001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Train the model on your new dataset\nhistory = model.fit(x_train_scaled, y_train, epochs=5, batch_size=64,\n                   validation_data=(x_test_scaled,y_test))","metadata":{"execution":{"iopub.status.busy":"2023-10-18T06:03:24.657181Z","iopub.execute_input":"2023-10-18T06:03:24.657682Z","iopub.status.idle":"2023-10-18T06:12:12.638473Z","shell.execute_reply.started":"2023-10-18T06:03:24.657641Z","shell.execute_reply":"2023-10-18T06:12:12.637556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model Evaluation on Train & Test Data","metadata":{}},{"cell_type":"code","source":"loss ,acc = model.evaluate(x_train_scaled, y_train)\nprint(\"Accuracy on Train Data:\",acc)\nprint()\nloss ,acc = model.evaluate(x_test_scaled, y_test )\nprint(\"Accuracy on Test Data:\",acc)","metadata":{"execution":{"iopub.status.busy":"2023-10-18T06:12:12.640926Z","iopub.execute_input":"2023-10-18T06:12:12.641665Z","iopub.status.idle":"2023-10-18T06:13:49.559519Z","shell.execute_reply.started":"2023-10-18T06:12:12.641629Z","shell.execute_reply":"2023-10-18T06:13:49.558287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(x_test_scaled)\ny_pred_test = [np.argmax(i) for i in y_pred]","metadata":{"execution":{"iopub.status.busy":"2023-10-18T06:13:49.561137Z","iopub.execute_input":"2023-10-18T06:13:49.561572Z","iopub.status.idle":"2023-10-18T06:14:12.970073Z","shell.execute_reply.started":"2023-10-18T06:13:49.56154Z","shell.execute_reply":"2023-10-18T06:14:12.969177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#len(y_pred_test)","metadata":{"execution":{"iopub.status.busy":"2023-10-16T04:13:04.271065Z","iopub.status.idle":"2023-10-16T04:13:04.271887Z","shell.execute_reply.started":"2023-10-16T04:13:04.271499Z","shell.execute_reply":"2023-10-16T04:13:04.271563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save the model to a file\n# mobilenet_model.save(\"mobilenet_model_3.h5\")","metadata":{"execution":{"iopub.status.busy":"2023-10-17T04:14:06.076124Z","iopub.execute_input":"2023-10-17T04:14:06.077481Z","iopub.status.idle":"2023-10-17T04:14:06.323698Z","shell.execute_reply.started":"2023-10-17T04:14:06.077432Z","shell.execute_reply":"2023-10-17T04:14:06.322573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Metrics Evaluation on Test Data","metadata":{}},{"cell_type":"code","source":"print(\"Confusion Matrix:\\n\",confusion_matrix(y_test,y_pred_test))\nprint()\nprint(\"Classification Report:\\n\",classification_report(y_test,y_pred_test))","metadata":{"execution":{"iopub.status.busy":"2023-10-16T04:13:04.275442Z","iopub.status.idle":"2023-10-16T04:13:04.276048Z","shell.execute_reply.started":"2023-10-16T04:13:04.275776Z","shell.execute_reply":"2023-10-16T04:13:04.275803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Compare Actual & Predicted Labels","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(24,40))\nfor i in range(60):\n    plt.subplot(10,6,i+1)\n    plt.imshow(x_test[i])\n    plt.title(f\"Actual Label:{class_labels[y_test[i]]}\\nPredicted Label:{class_labels[y_pred_test[i]]}\")\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-10-18T06:14:12.972942Z","iopub.execute_input":"2023-10-18T06:14:12.973785Z","iopub.status.idle":"2023-10-18T06:14:21.895905Z","shell.execute_reply.started":"2023-10-18T06:14:12.973738Z","shell.execute_reply":"2023-10-18T06:14:21.893724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}