{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30615,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np               #for numpy arrays\nimport pandas as pd              #for handling datasets\nimport tensorflow as tf          #for handle multipule dimentional arrays\nimport glob as gb                # to get files from internal folders\nimport matplotlib.pyplot as plt  #for plot diagrams\nimport cv2 as cv                 #for images\nimport os                        #for folders\nfrom sklearn.model_selection import train_test_split #to split data\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator #for data augmentation\nfrom tensorflow.keras.applications import ResNet50       #for importing nn resnet50\nfrom tensorflow.keras.applications.resnet50 import preprocess_input\nfrom tensorflow.keras.models import Model \nfrom tensorflow.keras import Sequential \nfrom tensorflow.keras.layers import Conv2D,MaxPooling2D,Dense,Flatten,Dropout,BatchNormalization\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.losses import SparseCategoricalCrossentropy\nfrom tensorflow.keras.metrics import SparseCategoricalAccuracy\nfrom sklearn.metrics import classification_report\nfrom tensorflow.keras.callbacks import Callback\nfrom keras.utils import to_categorical\nimport time\nimport cProfile ","metadata":{"_uuid":"a226f330-a72d-4cc4-8cf4-e0a396cdeb7b","_cell_guid":"86fa99bb-57ef-45df-9fa4-47c6c10e90e3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-28T10:05:33.851557Z","iopub.execute_input":"2024-01-28T10:05:33.852152Z","iopub.status.idle":"2024-01-28T10:05:46.815313Z","shell.execute_reply.started":"2024-01-28T10:05:33.852094Z","shell.execute_reply":"2024-01-28T10:05:46.813716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Required Libraries**","metadata":{}},{"cell_type":"code","source":"class TimeCallback(Callback):\n    def on_epoch_begin(self, epoch, logs=None):\n        self.epoch_start_time = time.time()\n\n    def on_epoch_end(self, epoch, logs=None):\n        epoch_end_time = time.time()\n        epoch_time = epoch_end_time - self.epoch_start_time\n        self.model.history.history.setdefault('time_per_epoch', []).append(epoch_time)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:05:46.818103Z","iopub.execute_input":"2024-01-28T10:05:46.819236Z","iopub.status.idle":"2024-01-28T10:05:46.829389Z","shell.execute_reply.started":"2024-01-28T10:05:46.819181Z","shell.execute_reply":"2024-01-28T10:05:46.827432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\ntrain_thumbnails='/kaggle/input/UBC-OCEAN/train_thumbnails/'\nfolder_thumbnails=gb.glob(os.path.join(train_thumbnails,'*.png'))","metadata":{"_uuid":"896a86e5-b671-4200-8611-8b3ba00a37a0","_cell_guid":"624635b3-f55f-41e6-b3e0-65b0377c74cf","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-28T10:05:46.831743Z","iopub.execute_input":"2024-01-28T10:05:46.832213Z","iopub.status.idle":"2024-01-28T10:05:46.931667Z","shell.execute_reply.started":"2024-01-28T10:05:46.832143Z","shell.execute_reply":"2024-01-28T10:05:46.929902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['thumbnails'] = pd.Series(dtype='str')\ndf.to_csv('updated_data.csv', index=False)","metadata":{"_uuid":"1695423f-041b-4a70-9bdc-6fb41b324d1e","_cell_guid":"ef7404cb-c8f2-4046-831f-80da055db24e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-28T10:05:46.935517Z","iopub.execute_input":"2024-01-28T10:05:46.936026Z","iopub.status.idle":"2024-01-28T10:05:46.966793Z","shell.execute_reply.started":"2024-01-28T10:05:46.935972Z","shell.execute_reply":"2024-01-28T10:05:46.965354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range (len(df)):\n    image_path = os.path.join(train_thumbnails,str(df.loc[i,'image_id']))+'_thumbnail.png'\n    for x in folder_thumbnails:\n        if x==image_path:\n            df.at[i, 'thumbnails'] = image_path\n            break","metadata":{"_uuid":"9a23a5a3-860b-4a62-8c5e-fd8fd118918a","_cell_guid":"63f018bd-5163-41d7-a56c-cd24adab8402","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-28T10:05:46.968825Z","iopub.execute_input":"2024-01-28T10:05:46.969308Z","iopub.status.idle":"2024-01-28T10:05:47.051297Z","shell.execute_reply.started":"2024-01-28T10:05:46.969269Z","shell.execute_reply":"2024-01-28T10:05:47.049019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder_thumbnails=[]\nlabel=[]\nData={'HGSC':0,'EC':1,'CC':2,'LGSC':3,'MC':4}\nfor i in range(len(df)):\n    if pd.isnull(df.loc[i, 'thumbnails']):\n        continue\n    label.append(Data[df.loc[i]['label']])\n    folder_thumbnails.append(df.loc[i]['thumbnails'])","metadata":{"_uuid":"24d2ed42-0b16-4952-9ef5-7b9d5edf3967","_cell_guid":"54fe8aa3-ef32-4de8-b815-c77306037ff3","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-28T10:05:47.053158Z","iopub.execute_input":"2024-01-28T10:05:47.053622Z","iopub.status.idle":"2024-01-28T10:05:47.182625Z","shell.execute_reply.started":"2024-01-28T10:05:47.053583Z","shell.execute_reply":"2024-01-28T10:05:47.180941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(folder_thumbnails))\nprint(len(label))\ndf.head()\nlabel[1:10]","metadata":{"_uuid":"d8084770-bc67-412c-bd14-81663cd7e2f8","_cell_guid":"25aca774-a0b9-4e80-8b0f-a77e979b8bcd","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-28T10:05:47.18438Z","iopub.execute_input":"2024-01-28T10:05:47.184779Z","iopub.status.idle":"2024-01-28T10:05:47.198004Z","shell.execute_reply.started":"2024-01-28T10:05:47.184743Z","shell.execute_reply":"2024-01-28T10:05:47.196398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath=pd.Series(folder_thumbnails,name='filePaths').astype(str)\nlables=pd.Series(label,name='label')\ndata=pd.concat([filepath,lables],axis=1)\ndata=data.sample(frac=1).reset_index(drop=True)\ndata.head()","metadata":{"_uuid":"8937849a-dbbd-4ff2-9749-fca78ad062aa","_cell_guid":"87482df2-c8eb-4098-9785-2e78fc78243a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-28T10:05:47.199768Z","iopub.execute_input":"2024-01-28T10:05:47.200239Z","iopub.status.idle":"2024-01-28T10:05:47.225799Z","shell.execute_reply.started":"2024-01-28T10:05:47.200199Z","shell.execute_reply":"2024-01-28T10:05:47.224431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_labels=[]\nfor i in Data.values():\n    count_labels.append(label.count(i))\nprint(count_labels)\nprint(Data)\nplt.figure(figsize=(8, 8))\nplt.pie(count_labels, labels=Data.keys(),explode=[0.2]*5 ,autopct='%1.1f%%', startangle=140)\nplt.axis('equal')\nplt.title('Sample Count')","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:05:47.227689Z","iopub.execute_input":"2024-01-28T10:05:47.228125Z","iopub.status.idle":"2024-01-28T10:05:47.481908Z","shell.execute_reply.started":"2024-01-28T10:05:47.228088Z","shell.execute_reply":"2024-01-28T10:05:47.480655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train , test = train_test_split(data, test_size=0.3, random_state=42)\ntest , valid = train_test_split(test,test_size=0.5 , random_state=42)","metadata":{"_uuid":"23b9cedc-5c70-4118-90a5-b71c55fc518b","_cell_guid":"12cc9fba-b5be-4bf3-b618-00d9e99ebc3d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-28T10:05:47.488327Z","iopub.execute_input":"2024-01-28T10:05:47.490054Z","iopub.status.idle":"2024-01-28T10:05:47.509303Z","shell.execute_reply.started":"2024-01-28T10:05:47.489965Z","shell.execute_reply":"2024-01-28T10:05:47.506684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen=ImageDataGenerator(\nrescale=1/255,\nshear_range=0.2, \nzoom_range=0.2, \nhorizontal_flip=True\n)\ntest_datagen=ImageDataGenerator(rescale=1/255)","metadata":{"_uuid":"45f31e78-5502-4c4d-bcec-da769cb66ab0","_cell_guid":"42c4a25e-ec1f-4918-9aff-67fdef06e2eb","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-28T10:05:47.511552Z","iopub.execute_input":"2024-01-28T10:05:47.512071Z","iopub.status.idle":"2024-01-28T10:05:47.523012Z","shell.execute_reply.started":"2024-01-28T10:05:47.512023Z","shell.execute_reply":"2024-01-28T10:05:47.520843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"w=500\nh=500\ntrain_den=train_datagen.flow_from_dataframe(\n    dataframe=train,\n    x_col='filePaths',\n    y_col='label',\n    target_size=(h,w),\n    class_mode='raw',\n    batch_size=32,\n    shuffle=True,\n    seed=42\n)\nvalid_den=test_datagen.flow_from_dataframe(\n    dataframe=valid,\n    x_col='filePaths',\n    y_col='label',\n    target_size=(h,w),\n    class_mode='raw',\n    batch_size=32,\n    shuffle=False,\n    seed=42\n)\ntest_den=test_datagen.flow_from_dataframe(\n    dataframe=test,\n    x_col='filePaths',\n    y_col='label',\n    target_size=(h,w),\n    class_mode='raw',\n    batch_size=32,\n    shuffle=False\n)","metadata":{"_uuid":"2c32845d-6925-4154-97d1-ae6ba5b16eed","_cell_guid":"37fff4d4-4a9c-4bb8-b3b5-f858c1cb9c2a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-28T10:05:47.525416Z","iopub.execute_input":"2024-01-28T10:05:47.525853Z","iopub.status.idle":"2024-01-28T10:05:47.554981Z","shell.execute_reply.started":"2024-01-28T10:05:47.525815Z","shell.execute_reply":"2024-01-28T10:05:47.553769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Pretrained_model=ResNet50(\n    input_shape=(h,w,3),\n    include_top=False,\n    weights='imagenet',\n    pooling='max')\nPretrained_model.trainable=False","metadata":{"_uuid":"135b19e5-8a31-4fb1-91d5-b162957898be","_cell_guid":"f1150e59-519e-4b04-a162-a05491000afa","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-28T10:05:47.55682Z","iopub.execute_input":"2024-01-28T10:05:47.557247Z","iopub.status.idle":"2024-01-28T10:05:51.290909Z","shell.execute_reply.started":"2024-01-28T10:05:47.55721Z","shell.execute_reply":"2024-01-28T10:05:51.289169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs=Pretrained_model.input\nx = Dense(256,activation='relu')(Pretrained_model.output)\nx = Dense(128,activation='relu')(x)\noutput=Dense(5,activation='softmax')(x)\nPretrained_model=Model(inputs=inputs,outputs=output)","metadata":{"_uuid":"7fb64741-f23d-4570-a86a-2be6a2d52218","_cell_guid":"0861ef15-6be0-44e7-9962-12ea050cbe50","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-28T10:05:51.293113Z","iopub.execute_input":"2024-01-28T10:05:51.293502Z","iopub.status.idle":"2024-01-28T10:05:51.373396Z","shell.execute_reply.started":"2024-01-28T10:05:51.293468Z","shell.execute_reply":"2024-01-28T10:05:51.372007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Pretrained_model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-01-28T12:00:08.045506Z","iopub.execute_input":"2024-01-28T12:00:08.046607Z","iopub.status.idle":"2024-01-28T12:00:08.091438Z","shell.execute_reply.started":"2024-01-28T12:00:08.046564Z","shell.execute_reply":"2024-01-28T12:00:08.089756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(256, kernel_size=(3,3), activation='relu',padding='valid'\n          ,input_shape=(h,w, 3)))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(256,strides=(1,1), kernel_size=(3,3), activation='relu',padding='valid'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(128,strides=(1,1), kernel_size=(3,3), activation='relu',padding='valid'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(128,strides=(1,1), kernel_size=(3,3), activation='relu',padding='valid'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(64,strides=(1,1), kernel_size=(3,3), activation='relu',padding='valid'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(64,strides=(1,1), kernel_size=(3,3), activation='relu',padding='valid'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(BatchNormalization())\nmodel.add(Flatten())\nmodel.add(Dense(units=128, activation='relu'))\nmodel.add(Dense(units=64, activation='relu'))\nmodel.add(Dense(units=5, activation='softmax'))\n","metadata":{"execution":{"iopub.status.busy":"2024-01-28T10:05:51.409042Z","iopub.execute_input":"2024-01-28T10:05:51.409428Z","iopub.status.idle":"2024-01-28T10:05:51.737814Z","shell.execute_reply.started":"2024-01-28T10:05:51.409396Z","shell.execute_reply":"2024-01-28T10:05:51.736386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"_uuid":"1d613e49-638a-406d-af98-445d9e0b55a8","_cell_guid":"a905195b-70d1-4c97-89f8-369b023bbe57","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-28T10:05:51.739655Z","iopub.execute_input":"2024-01-28T10:05:51.74009Z","iopub.status.idle":"2024-01-28T10:05:51.758579Z","shell.execute_reply.started":"2024-01-28T10:05:51.740055Z","shell.execute_reply":"2024-01-28T10:05:51.757034Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_custom = model.fit(train_den, epochs=5, validation_data=valid_den , callbacks=[TimeCallback()])","metadata":{"_uuid":"687fc229-bbbd-458f-a233-8988fd3c8481","_cell_guid":"4f953bd1-5fea-4457-a02c-7563d5c5ba39","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-28T12:28:59.782836Z","iopub.execute_input":"2024-01-28T12:28:59.784391Z","iopub.status.idle":"2024-01-28T14:20:58.730063Z","shell.execute_reply.started":"2024-01-28T12:28:59.784343Z","shell.execute_reply":"2024-01-28T14:20:58.72429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_resnet50 = Pretrained_model.fit(train_den, epochs=5, validation_data=valid_den, callbacks=[TimeCallback()])","metadata":{"execution":{"iopub.status.busy":"2024-01-28T12:00:22.148727Z","iopub.execute_input":"2024-01-28T12:00:22.149296Z","iopub.status.idle":"2024-01-28T12:27:52.357865Z","shell.execute_reply.started":"2024-01-28T12:00:22.149253Z","shell.execute_reply":"2024-01-28T12:27:52.354822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot time per epoch\nplt.figure(figsize=(12, 5))\nplt.plot(np.cumsum(history_custom.history['time_per_epoch']), label='Custom Model')\nplt.plot(np.cumsum(history_resnet50.history['time_per_epoch']), label='ResNet50')\nplt.title('Time per Epoch Comparison')\nplt.xlabel('Epochs')\nplt.ylabel('Time (seconds)')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T14:22:32.000718Z","iopub.execute_input":"2024-01-28T14:22:32.001628Z","iopub.status.idle":"2024-01-28T14:22:32.523313Z","shell.execute_reply.started":"2024-01-28T14:22:32.001532Z","shell.execute_reply":"2024-01-28T14:22:32.522352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 5))\nplt.plot(history_custom.history['loss'], label='Custom Model Train')\nplt.plot(history_custom.history['val_loss'], label='Custom Model Validation')\nplt.plot(history_resnet50.history['loss'], label='ResNet50 Train')\nplt.plot(history_resnet50.history['val_loss'], label='ResNet50 Validation')\nplt.title('Loss Comparison')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T14:23:08.516767Z","iopub.execute_input":"2024-01-28T14:23:08.517669Z","iopub.status.idle":"2024-01-28T14:23:08.918186Z","shell.execute_reply.started":"2024-01-28T14:23:08.517629Z","shell.execute_reply":"2024-01-28T14:23:08.916677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 5))\nplt.plot(history_custom.history['accuracy'], label='Custom Model Train')\nplt.plot(history_custom.history['val_accuracy'], label='Custom Model Validation')\nplt.plot(history_resnet50.history['accuracy'], label='ResNet50 Train')\nplt.plot(history_resnet50.history['val_accuracy'], label='ResNet50 Validation')\nplt.title('Accuracy Comparison')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T14:23:24.636653Z","iopub.execute_input":"2024-01-28T14:23:24.637191Z","iopub.status.idle":"2024-01-28T14:23:25.132387Z","shell.execute_reply.started":"2024-01-28T14:23:24.637151Z","shell.execute_reply":"2024-01-28T14:23:25.130977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('model001')","metadata":{"_uuid":"3863ee1a-5928-46c5-8e49-1228269c6de0","_cell_guid":"abd3873c-da0c-418b-8ac8-5ffd3b1a9c51","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-01-28T14:23:43.469824Z","iopub.execute_input":"2024-01-28T14:23:43.470393Z","iopub.status.idle":"2024-01-28T14:24:19.862149Z","shell.execute_reply.started":"2024-01-28T14:23:43.470342Z","shell.execute_reply":"2024-01-28T14:24:19.860127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Pretrained_model.save('model002')","metadata":{"execution":{"iopub.status.busy":"2024-01-28T14:24:19.867227Z","iopub.execute_input":"2024-01-28T14:24:19.868615Z","iopub.status.idle":"2024-01-28T14:24:46.666544Z","shell.execute_reply.started":"2024-01-28T14:24:19.86852Z","shell.execute_reply":"2024-01-28T14:24:46.664994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_model=model.evaluate(test_den,verbose=0)\nprint(f'Test_loss={result_model[0]}')\nprint(f'Test_accuracy={result_model[1]}')","metadata":{"scrolled":true,"_kg_hide-input":false,"execution":{"iopub.status.busy":"2024-01-28T14:24:46.678294Z","iopub.execute_input":"2024-01-28T14:24:46.678796Z","iopub.status.idle":"2024-01-28T14:26:20.033062Z","shell.execute_reply.started":"2024-01-28T14:24:46.678751Z","shell.execute_reply":"2024-01-28T14:26:20.029853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_resnet=Pretrained_model.evaluate(test_den,verbose=0)\nprint(f'Test_loss={result_resnet[0]}')\nprint(f'Test_accuracy={result_resnet[1]}')","metadata":{"execution":{"iopub.status.busy":"2024-01-28T14:27:37.995394Z","iopub.execute_input":"2024-01-28T14:27:37.995839Z","iopub.status.idle":"2024-01-28T14:30:07.424781Z","shell.execute_reply.started":"2024-01-28T14:27:37.995805Z","shell.execute_reply":"2024-01-28T14:30:07.422974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images, test_labels = next(iter(test_den))\ntest_labels=list(test_labels.tolist())\n# Predict labels for test images\ny_pred_model=model.predict(test_den)\ny_pred_model=np.argmax(y_pred_model,axis=1)\ny_pred_model=list(y_pred_model)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T14:30:07.427869Z","iopub.execute_input":"2024-01-28T14:30:07.428435Z","iopub.status.idle":"2024-01-28T14:32:44.368066Z","shell.execute_reply.started":"2024-01-28T14:30:07.428388Z","shell.execute_reply":"2024-01-28T14:32:44.366657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict labels for test images\ny_pred_resnet=Pretrained_model.predict(test_den)\ny_pred_resnet=np.argmax(y_pred_resnet,axis=1)\ny_pred_resnet=list(y_pred_resnet)","metadata":{"execution":{"iopub.status.busy":"2024-01-28T14:32:44.370086Z","iopub.execute_input":"2024-01-28T14:32:44.370914Z","iopub.status.idle":"2024-01-28T14:34:15.045666Z","shell.execute_reply.started":"2024-01-28T14:32:44.370849Z","shell.execute_reply":"2024-01-28T14:34:15.044498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_key(val):\n    for key, value in Data.items():\n        if val == value:\n            return key","metadata":{"execution":{"iopub.status.busy":"2024-01-28T14:34:15.049461Z","iopub.execute_input":"2024-01-28T14:34:15.050348Z","iopub.status.idle":"2024-01-28T14:34:15.05844Z","shell.execute_reply.started":"2024-01-28T14:34:15.050294Z","shell.execute_reply":"2024-01-28T14:34:15.056983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nfor i in range(20):\n    plt.subplot(5, 4, i + 1)\n    plt.imshow(test_images[i])\n    plt.title(f\"Predicted: {get_key(y_pred_model[i])}, True: {get_key(test_labels[i])}\")\n    plt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T14:34:15.060097Z","iopub.execute_input":"2024-01-28T14:34:15.060488Z","iopub.status.idle":"2024-01-28T14:34:18.37969Z","shell.execute_reply.started":"2024-01-28T14:34:15.060453Z","shell.execute_reply":"2024-01-28T14:34:18.377933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 12))\nfor i in range(20):\n    plt.subplot(5, 4, i + 1)\n    plt.imshow(test_images[i])\n    plt.title(f\"Predicted: {get_key(y_pred_resnet[i])}, True: {get_key(test_labels[i])}\")\n    plt.axis('off')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T14:34:18.385438Z","iopub.execute_input":"2024-01-28T14:34:18.385968Z","iopub.status.idle":"2024-01-28T14:34:21.603923Z","shell.execute_reply.started":"2024-01-28T14:34:18.385921Z","shell.execute_reply":"2024-01-28T14:34:21.602124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T14:35:17.744588Z","iopub.execute_input":"2024-01-28T14:35:17.745249Z","iopub.status.idle":"2024-01-28T14:35:17.825662Z","shell.execute_reply.started":"2024-01-28T14:35:17.745203Z","shell.execute_reply":"2024-01-28T14:35:17.823978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Pretrained_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-28T14:34:22.101166Z","iopub.execute_input":"2024-01-28T14:34:22.101558Z","iopub.status.idle":"2024-01-28T14:34:22.750633Z","shell.execute_reply.started":"2024-01-28T14:34:22.101524Z","shell.execute_reply":"2024-01-28T14:34:22.749708Z"},"trusted":true},"execution_count":null,"outputs":[]}]}