{"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":6243,"databundleVersionId":868544,"sourceType":"competition"},{"sourceId":8381926,"sourceType":"datasetVersion","datasetId":4984600}],"dockerImageVersionId":30699,"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\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\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-05-11T17:43:39.579562Z","iopub.execute_input":"2024-05-11T17:43:39.580371Z","iopub.status.idle":"2024-05-11T17:43:39.5857Z","shell.execute_reply.started":"2024-05-11T17:43:39.580339Z","shell.execute_reply":"2024-05-11T17:43:39.584609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport glob\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nfrom sklearn.model_selection import train_test_split\nimport random \nimport keras\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.applications.vgg16 import VGG16\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Flatten , Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau,ModelCheckpoint \nfrom sklearn.metrics import accuracy_score, confusion_matrix, classification_report\nfrom keras.layers import Activation","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:43:39.588482Z","iopub.execute_input":"2024-05-11T17:43:39.588782Z","iopub.status.idle":"2024-05-11T17:44:02.241404Z","shell.execute_reply.started":"2024-05-11T17:43:39.588743Z","shell.execute_reply":"2024-05-11T17:44:02.240507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_dir = '../input/intel-mobileodt-cervical-cancer-screening'\ntrain_dir = os.path.join(root_dir,'train', 'train')\n\ntype1_dir = os.path.join(train_dir, 'Type_1')\ntype2_dir = os.path.join(train_dir, 'Type_2')\ntype3_dir = os.path.join(train_dir, 'Type_3')\n\ntrain_type1_files = glob.glob(type1_dir+'/*.jpg')\ntrain_type2_files = glob.glob(type2_dir+'/*.jpg')\ntrain_type3_files = glob.glob(type3_dir+'/*.jpg')\n\nadded_type1_files  =  glob.glob(os.path.join(root_dir, \"additional_Type_1_v2\", \"Type_1\")+'/*.jpg')\nadded_type2_files  =  glob.glob(os.path.join(root_dir, \"additional_Type_2_v2\", \"Type_2\")+'/*.jpg')\nadded_type3_files  =  glob.glob(os.path.join(root_dir, \"additional_Type_3_v2\", \"Type_3\")+'/*.jpg')\n\n\ntype1_files = train_type1_files + added_type1_files\ntype2_files = train_type2_files + added_type2_files\ntype3_files = train_type3_files + added_type3_files\n\n# print(f'''Type 1 files for training: {len(train_type1_files)} \n# Type 2 files for training: {len(train_type2_files)}\n# Type 3 files for training: {len(train_type3_files)}''' )\n\n# print(f'''Added Type 1 files for training: {len(added_type1_files)} \n# Added Type 2 files for training: {len(added_type2_files)}\n# Added Type 3 files for training: {len(added_type3_files)}''' )\n\nprint(f'''Total Type 1 files for training: {len(type1_files)} \nTotal Type 2 files for training: {len(type2_files)}\nTotal Type 3 files for training: {len(type3_files)}''' )","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:02.243371Z","iopub.execute_input":"2024-05-11T17:44:02.244535Z","iopub.status.idle":"2024-05-11T17:44:04.034437Z","shell.execute_reply.started":"2024-05-11T17:44:02.244488Z","shell.execute_reply":"2024-05-11T17:44:04.033397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Dataframe of Files and Labels\nfiles = {'filepath': type1_files + type2_files + type3_files,\n          'label': ['Type 1']* len(type1_files) + ['Type 2']* len(type2_files) + ['Type 3']* len(type3_files)}\n\nfiles_df = pd.DataFrame(files).sample(frac=1, random_state= 1).reset_index(drop=True)\nfiles_df","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:04.035469Z","iopub.execute_input":"2024-05-11T17:44:04.03573Z","iopub.status.idle":"2024-05-11T17:44:04.075977Z","shell.execute_reply.started":"2024-05-11T17:44:04.035707Z","shell.execute_reply":"2024-05-11T17:44:04.07504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display sample images of types\nfor label in ('Type 1', 'Type 2', 'Type 3'):\n    filepaths = files_df[files_df['label']==label]['filepath'].values[:5]\n    fig = plt.figure(figsize= (15, 6))\n    for i, path in enumerate(filepaths):\n        img = cv2.imread(path)\n        img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)\n        img = cv2.resize(img, (224, 224))\n        fig.add_subplot(1, 5, i+1)#1row 5cols currentindex\n        plt.imshow(img)\n        plt.subplots_adjust(hspace=0.5)\n        plt.axis(False)\n        plt.title(label)","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:04.077961Z","iopub.execute_input":"2024-05-11T17:44:04.078697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#splitting data into train and validation set\ntrain_df, eval_df = train_test_split(files_df,test_size = 0.2,stratify=files_df['label'],random_state=1)\nval_df, test_df = train_test_split(eval_df,test_size = 0.5,stratify=eval_df['label'],random_state=1)\nprint(len(train_df), len(val_df), len(test_df))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\ndef load_images(df):\n    features = []\n    filepaths = df['filepath'].values\n    labels = df['label'].values\n    for path in filepaths:\n        img = cv2.imread(path)\n        if img is None:\n            print(df[df['filepath'] == path])\n            df.drop(df[df['filepath'] == path].index, inplace = True)\n            continue\n        resized_img = cv2.resize(img,(120,120))\n        features.append(np.array(resized_img))\n    return np.array(features), np.array(df['label'].values)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_features, train_labels = load_images(train_df)\n# val_features, val_labels = load_images(val_df)\n# test_features, test_labels = load_images(test_df)    ","metadata":{"execution":{"iopub.status.idle":"2024-05-11T17:44:09.081971Z","shell.execute_reply.started":"2024-05-11T17:44:09.069642Z","shell.execute_reply":"2024-05-11T17:44:09.080949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pickle\n# with open('train.pickle','wb') as f:\n#     pickle.dump((train_features,train_labels),f,protocol=pickle.HIGHEST_PROTOCOL)\n# with open('val.pickle','wb') as f:\n#     pickle.dump((val_features,val_labels),f,protocol=pickle.HIGHEST_PROTOCOL)\n# with open('test.pickle','wb') as f:\n#     pickle.dump((test_features,test_labels),f,protocol=pickle.HIGHEST_PROTOCOL)","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:09.083311Z","iopub.execute_input":"2024-05-11T17:44:09.08361Z","iopub.status.idle":"2024-05-11T17:44:09.095584Z","shell.execute_reply.started":"2024-05-11T17:44:09.083584Z","shell.execute_reply":"2024-05-11T17:44:09.094553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle\ntrain_features, train_labels = None, None \nwith open('/kaggle/input/pickle/train.pickle', 'rb') as handle:\n    train_features, train_labels = pickle.load(handle)\n\nval_features, val_labels = None, None\nwith open('/kaggle/input/pickle/val.pickle', 'rb') as handle:\n    val_features, val_labels = pickle.load(handle)\n\ntest_features, test_labels = None, None\nwith open('/kaggle/input/pickle/test.pickle', 'rb') as handle:\n    test_features, test_labels = pickle.load(handle)","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:09.09676Z","iopub.execute_input":"2024-05-11T17:44:09.09708Z","iopub.status.idle":"2024-05-11T17:44:12.506559Z","shell.execute_reply.started":"2024-05-11T17:44:09.097006Z","shell.execute_reply":"2024-05-11T17:44:12.50569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# lengths of training and evaluation  sets\nlen(train_features), len(train_labels), len(test_features), len(test_labels), len(test_features), len(test_labels) ","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:12.510932Z","iopub.execute_input":"2024-05-11T17:44:12.511496Z","iopub.status.idle":"2024-05-11T17:44:12.517679Z","shell.execute_reply.started":"2024-05-11T17:44:12.511469Z","shell.execute_reply":"2024-05-11T17:44:12.516835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#shape of input image\ntrain_features[0].shape","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:12.518881Z","iopub.execute_input":"2024-05-11T17:44:12.519195Z","iopub.status.idle":"2024-05-11T17:44:12.531351Z","shell.execute_reply.started":"2024-05-11T17:44:12.51917Z","shell.execute_reply":"2024-05-11T17:44:12.53053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalizing features\nX_train = train_features/255\nX_val = val_features/255\nX_test = test_features/255","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:12.532392Z","iopub.execute_input":"2024-05-11T17:44:12.532646Z","iopub.status.idle":"2024-05-11T17:44:13.444014Z","shell.execute_reply.started":"2024-05-11T17:44:12.532624Z","shell.execute_reply":"2024-05-11T17:44:13.443163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#numerical labelling of images\nfrom sklearn.preprocessing import LabelEncoder\nle = LabelEncoder().fit(['Type 1','Type 2','Type 3'])  # creates a mapping s.t type1 assigned 0, type2 1,type3 2 \ny_train = le.transform(train_labels)\ny_val = le.transform(val_labels)\ny_test = le.transform(test_labels)","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:13.445209Z","iopub.execute_input":"2024-05-11T17:44:13.445524Z","iopub.status.idle":"2024-05-11T17:44:13.458754Z","shell.execute_reply.started":"2024-05-11T17:44:13.445497Z","shell.execute_reply":"2024-05-11T17:44:13.457845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#initializing ImageDataGenerator to augment image parameters\ntrain_datagen = ImageDataGenerator(  #so train data sees all kind of diverse data \n    rotation_range  = 40,\n    zoom_range = 0.2,\n    vertical_flip = True,\n    horizontal_flip = True,   \n    width_shift_range = 0.2,\n    height_shift_range = 0.2,\n    shear_range = 0.2\n)\neval_datagen = ImageDataGenerator()  # no augmentation params cause we wanna evaluate model's performance on original dataset","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:13.460173Z","iopub.execute_input":"2024-05-11T17:44:13.460485Z","iopub.status.idle":"2024-05-11T17:44:13.472532Z","shell.execute_reply.started":"2024-05-11T17:44:13.46046Z","shell.execute_reply":"2024-05-11T17:44:13.471787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#augmenting features\nBATCH_SIZE = 16\ntrain_generator = train_datagen.flow(X_train,y_train,batch_size = BATCH_SIZE) # return npArrayIterator\nval_generator = eval_datagen.flow(X_val,y_val,batch_size = BATCH_SIZE)\ntest_generator = eval_datagen.flow(X_val,y_val,batch_size = BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:13.473711Z","iopub.execute_input":"2024-05-11T17:44:13.474152Z","iopub.status.idle":"2024-05-11T17:44:14.044144Z","shell.execute_reply.started":"2024-05-11T17:44:13.474118Z","shell.execute_reply":"2024-05-11T17:44:14.043315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for data_batch,label_batch in train_generator:\n    print(f'data batch shape : {data_batch.shape}\\nlabel batch shape : {label_batch.shape}')\n    break","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:14.045324Z","iopub.execute_input":"2024-05-11T17:44:14.045626Z","iopub.status.idle":"2024-05-11T17:44:14.10352Z","shell.execute_reply.started":"2024-05-11T17:44:14.045599Z","shell.execute_reply":"2024-05-11T17:44:14.102589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#distribtution of training set of data \nlabels = list(map(lambda x: int(x[-1]),train_labels)) #taking last element of label string and converting to int for all labels\nplt.figure(figsize = (5,5))\nplt.bar(['Type 1','Type 2','Type 3'],[labels.count(1),labels.count(2),labels.count(3)])\nplt.title('Number of Cervixes')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:14.105613Z","iopub.execute_input":"2024-05-11T17:44:14.105919Z","iopub.status.idle":"2024-05-11T17:44:14.346983Z","shell.execute_reply.started":"2024-05-11T17:44:14.105892Z","shell.execute_reply":"2024-05-11T17:44:14.346113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#initializing pretrained vgg16 model\ncnn_base = VGG16(weights ='imagenet',\n                include_top=False,\n                input_shape = (120,120,3))","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:46:27.712439Z","iopub.execute_input":"2024-05-11T17:46:27.713487Z","iopub.status.idle":"2024-05-11T17:46:29.545816Z","shell.execute_reply.started":"2024-05-11T17:46:27.713443Z","shell.execute_reply":"2024-05-11T17:46:29.5449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#total trainable layers\n# show trainable layers before freezing\nprint('Number of trainable layers before freezing ', len(cnn_base.trainable_weights))","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:46:31.295099Z","iopub.execute_input":"2024-05-11T17:46:31.295464Z","iopub.status.idle":"2024-05-11T17:46:31.300504Z","shell.execute_reply.started":"2024-05-11T17:46:31.295435Z","shell.execute_reply":"2024-05-11T17:46:31.299561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#freeze few layers of vgg19\nfor layer in cnn_base.layers[:-5]:\n    layer.trainable= False","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:46:33.008182Z","iopub.execute_input":"2024-05-11T17:46:33.008549Z","iopub.status.idle":"2024-05-11T17:46:33.013461Z","shell.execute_reply.started":"2024-05-11T17:46:33.00852Z","shell.execute_reply":"2024-05-11T17:46:33.01252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number of trainable layers after freezing ', len(cnn_base.trainable_weights))","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:46:35.460933Z","iopub.execute_input":"2024-05-11T17:46:35.461306Z","iopub.status.idle":"2024-05-11T17:46:35.466871Z","shell.execute_reply.started":"2024-05-11T17:46:35.461276Z","shell.execute_reply":"2024-05-11T17:46:35.465985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"METRICS = [\n    keras.metrics.BinaryAccuracy(name='acc') #can add precision or recall\n]","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:46:37.236451Z","iopub.execute_input":"2024-05-11T17:46:37.236799Z","iopub.status.idle":"2024-05-11T17:46:37.246354Z","shell.execute_reply.started":"2024-05-11T17:46:37.236765Z","shell.execute_reply":"2024-05-11T17:46:37.245491Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building Model\n# model=Sequential()\n# model.add(cnn_base)\n# model.add(Dropout(0.5))\n# model.add(Flatten())\n# model.add(Dense(512,kernel_initializer='he_uniform'))\n# model.add(Activation('relu'))\n# model.add(Dense(3,activation='softmax'))\n","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:15.075492Z","iopub.status.idle":"2024-05-11T17:44:15.075942Z","shell.execute_reply.started":"2024-05-11T17:44:15.075709Z","shell.execute_reply":"2024-05-11T17:44:15.075729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([cnn_base, \n                    Flatten(),\n                    Dropout(0.5),\n                    Dense(3, activation='softmax')])","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:46:41.045469Z","iopub.execute_input":"2024-05-11T17:46:41.046257Z","iopub.status.idle":"2024-05-11T17:46:41.057478Z","shell.execute_reply.started":"2024-05-11T17:46:41.046228Z","shell.execute_reply":"2024-05-11T17:46:41.056735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# compile model\nmodel.compile(optimizer= Adam(0.0001),\n              loss= 'sparse_categorical_crossentropy',\n              metrics= ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:46:45.048525Z","iopub.execute_input":"2024-05-11T17:46:45.049215Z","iopub.status.idle":"2024-05-11T17:46:45.064021Z","shell.execute_reply.started":"2024-05-11T17:46:45.049181Z","shell.execute_reply":"2024-05-11T17:46:45.063019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:46:47.324871Z","iopub.execute_input":"2024-05-11T17:46:47.325233Z","iopub.status.idle":"2024-05-11T17:46:47.346948Z","shell.execute_reply.started":"2024-05-11T17:46:47.325205Z","shell.execute_reply":"2024-05-11T17:46:47.3461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow.keras.utils import plot_model\n# from IPython.display import SVG, Image\n# plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True)\n# Image('model.png',width=200, height=100)","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:15.082917Z","iopub.status.idle":"2024-05-11T17:44:15.083351Z","shell.execute_reply.started":"2024-05-11T17:44:15.083132Z","shell.execute_reply":"2024-05-11T17:44:15.083149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_STEPS = len(train_labels)//BATCH_SIZE\nVAL_STEPS = len(val_labels)//BATCH_SIZE","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:46:51.026454Z","iopub.execute_input":"2024-05-11T17:46:51.027078Z","iopub.status.idle":"2024-05-11T17:46:51.031236Z","shell.execute_reply.started":"2024-05-11T17:46:51.027028Z","shell.execute_reply":"2024-05-11T17:46:51.030277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# initialize callbacks\nreduceLR = ReduceLROnPlateau(monitor='val_loss', patience=10, verbose= 1, mode='min', factor=  0.2, min_lr = 1e-5)\n\nearly_stopping = EarlyStopping(monitor='val_accuracy', patience = 10, verbose=1, mode='max', restore_best_weights= True)\ncheckpoint = ModelCheckpoint('/kaggle/working/cervicalModel_weights.weights.h5', monitor='val_accuracy', verbose=1, save_best_only=False, mode='max', save_weights_only=True)\n\n# checkpoint = ModelCheckpoint('/kaggle/working/cervicalModel_weights.h5', monitor='val_accuracy', verbose=1,save_best_only=True, mode= 'max')\n# filepath = \"/content/drive/MyDrive/cervix/cervical-weights/nemodels.keras\"\n# checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=False, mode='max')","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:46:53.378434Z","iopub.execute_input":"2024-05-11T17:46:53.379216Z","iopub.status.idle":"2024-05-11T17:46:53.384665Z","shell.execute_reply.started":"2024-05-11T17:46:53.379185Z","shell.execute_reply":"2024-05-11T17:46:53.383766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.load_weights(\"/kaggle/input/cervical-weights/cervicalModel.weights.hdf5\")","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:15.088731Z","iopub.status.idle":"2024-05-11T17:44:15.089107Z","shell.execute_reply.started":"2024-05-11T17:44:15.088909Z","shell.execute_reply":"2024-05-11T17:44:15.088923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow.keras.models import Model\n# vgg = VGG19(input_shape=(120, 120, 3), weights='imagenet', include_top=False)\n# for layer in vgg.layers:\n#     layer.trainable = False\n\n# x = Flatten()(vgg.output)\n# prediction = Dense(3, activation='softmax')(x)\n# model = Model(inputs=vgg.input, outputs=prediction)\n# model.compile(\n#   loss='sparse_categorical_crossentropy',\n#   optimizer=\"adam\",\n#   metrics=['accuracy']\n# )","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:15.090594Z","iopub.status.idle":"2024-05-11T17:44:15.09093Z","shell.execute_reply.started":"2024-05-11T17:44:15.090759Z","shell.execute_reply":"2024-05-11T17:44:15.090772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.save('/kaggle/working/my_model.h5')","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:15.092721Z","iopub.status.idle":"2024-05-11T17:44:15.093038Z","shell.execute_reply.started":"2024-05-11T17:44:15.092881Z","shell.execute_reply":"2024-05-11T17:44:15.092894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.load_weights('/kaggle/working/cervicalModel_weights.keras')","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:44:15.094507Z","iopub.status.idle":"2024-05-11T17:44:15.094865Z","shell.execute_reply.started":"2024-05-11T17:44:15.094675Z","shell.execute_reply":"2024-05-11T17:44:15.094689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train model\nhistory = model.fit(train_generator,\n                    steps_per_epoch= TRAIN_STEPS,\n                    validation_data=val_generator,\n                    validation_steps=VAL_STEPS,\n                    epochs= 100,\n                   callbacks= [checkpoint,reduceLR])\n","metadata":{"execution":{"iopub.status.busy":"2024-05-11T17:47:01.249929Z","iopub.execute_input":"2024-05-11T17:47:01.25071Z","iopub.status.idle":"2024-05-11T18:08:52.595574Z","shell.execute_reply.started":"2024-05-11T17:47:01.250679Z","shell.execute_reply":"2024-05-11T18:08:52.594518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"/kaggle/working/cervical_full_model.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-05-11T18:15:25.79851Z","iopub.execute_input":"2024-05-11T18:15:25.799269Z","iopub.status.idle":"2024-05-11T18:15:26.017093Z","shell.execute_reply.started":"2024-05-11T18:15:25.799239Z","shell.execute_reply":"2024-05-11T18:15:26.015906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\n# Load the model architecture from the saved weights\nmodel = load_model(\"/kaggle/working/cervicalModel_weights.weights.h5\")","metadata":{"execution":{"iopub.status.busy":"2024-05-11T18:13:57.995975Z","iopub.execute_input":"2024-05-11T18:13:57.996894Z","iopub.status.idle":"2024-05-11T18:13:58.401238Z","shell.execute_reply.started":"2024-05-11T18:13:57.996859Z","shell.execute_reply":"2024-05-11T18:13:58.399747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nfrom keras.preprocessing import image\n\n# Load and preprocess the image\nimg_path = '/kaggle/input/intel-mobileodt-cervical-cancer-screening/train/train/Type_2/1.jpg'\nimg = image.load_img(img_path, target_size=(120,120))\nimg_array = image.img_to_array(img)\nimg_array = np.expand_dims(img_array, axis=0)  # Add batch dimension\nimg_array /= 255.0  # Normalize pixel values if needed (assuming pixel values are in the range [0, 255])\n\n# Perform prediction\npredictions = model.predict(img_array)\n\n# Print predicted probabilities for each class\nprint(predictions)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-11T18:18:38.783619Z","iopub.execute_input":"2024-05-11T18:18:38.784009Z","iopub.status.idle":"2024-05-11T18:18:41.832915Z","shell.execute_reply.started":"2024-05-11T18:18:38.783979Z","shell.execute_reply":"2024-05-11T18:18:41.832033Z"},"trusted":true},"execution_count":null,"outputs":[]}]}