{"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":8483442,"sourceType":"datasetVersion","datasetId":5060211},{"sourceId":8494490,"sourceType":"datasetVersion","datasetId":5068421}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport glob\nimport PIL\nfrom PIL import ImageFilter, ImageStat, Image, ImageDraw\nfrom multiprocessing import Pool, cpu_count\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport cv2\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.layers import Activation, BatchNormalization, Conv2D, Dense, Dropout, Flatten, MaxPooling2D\nfrom tensorflow.keras.models import Sequential\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.applications.resnet50 import preprocess_input","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:13.137976Z","iopub.execute_input":"2024-05-23T10:40:13.138729Z","iopub.status.idle":"2024-05-23T10:40:24.563024Z","shell.execute_reply.started":"2024-05-23T10:40:13.138692Z","shell.execute_reply":"2024-05-23T10:40:24.562187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = glob.glob('../input/intel-mobileodt-cervical-cancer-screening/train/**/*.jpg') + glob.glob('../input/intel-mobileodt-cervical-cancer-screening/additional_Type_1_v2/**/*.jpg')+glob.glob('../input/intel-mobileodt-cervical-cancer-screening/additional_Type_2_v2/**/*.jpg')+glob.glob('../input/intel-mobileodt-cervical-cancer-screening/additional_Type_3_v2/**/*.jpg')\ntrain = pd.DataFrame([[p.split('/')[4],p] for p in train], columns = ['type','path'])\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:24.564916Z","iopub.execute_input":"2024-05-23T10:40:24.565881Z","iopub.status.idle":"2024-05-23T10:40:25.397400Z","shell.execute_reply.started":"2024-05-23T10:40:24.565844Z","shell.execute_reply":"2024-05-23T10:40:25.396513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['type']","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:25.398549Z","iopub.execute_input":"2024-05-23T10:40:25.398833Z","iopub.status.idle":"2024-05-23T10:40:25.407053Z","shell.execute_reply.started":"2024-05-23T10:40:25.398810Z","shell.execute_reply":"2024-05-23T10:40:25.405889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, val_df = train_test_split(train,test_size = 0.2,stratify=train['type'],random_state=1)\nprint(len(train_df), len(val_df))","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:25.410147Z","iopub.execute_input":"2024-05-23T10:40:25.410488Z","iopub.status.idle":"2024-05-23T10:40:25.432446Z","shell.execute_reply.started":"2024-05-23T10:40:25.410452Z","shell.execute_reply":"2024-05-23T10:40:25.431590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# /kaggle/input/intel-mobileodt-cervical-cancer-screening  /kaggle/input/intel-mobileodt-cervical-cancer-screening/test","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:25.433396Z","iopub.execute_input":"2024-05-23T10:40:25.433674Z","iopub.status.idle":"2024-05-23T10:40:25.437643Z","shell.execute_reply.started":"2024-05-23T10:40:25.433651Z","shell.execute_reply":"2024-05-23T10:40:25.436741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test = glob.glob('../input/intel-mobileodt-cervical-cancer-screening/test/test/*.jpg')\n# test = pd.DataFrame([[p.split('/')[3],p] for p in test], columns = ['type','path'])\n# test.head(5)\n# len(test)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:25.438819Z","iopub.execute_input":"2024-05-23T10:40:25.439104Z","iopub.status.idle":"2024-05-23T10:40:25.446616Z","shell.execute_reply.started":"2024-05-23T10:40:25.439075Z","shell.execute_reply":"2024-05-23T10:40:25.445647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_images(df):\n    features = []\n    filepaths = df['path'].values\n    labels = df['type'].values\n    for path in filepaths:\n        img = cv2.imread(path)\n        if img is None:\n            print(df[df['path'] == path])\n            df.drop(df[df['path'] == 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['type'].values)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:25.447652Z","iopub.execute_input":"2024-05-23T10:40:25.448685Z","iopub.status.idle":"2024-05-23T10:40:25.455967Z","shell.execute_reply.started":"2024-05-23T10:40:25.448659Z","shell.execute_reply":"2024-05-23T10:40:25.455072Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:25.456919Z","iopub.execute_input":"2024-05-23T10:40:25.457154Z","iopub.status.idle":"2024-05-23T10:40:25.465658Z","shell.execute_reply.started":"2024-05-23T10:40:25.457126Z","shell.execute_reply":"2024-05-23T10:40:25.464741Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:25.466987Z","iopub.execute_input":"2024-05-23T10:40:25.467377Z","iopub.status.idle":"2024-05-23T10:40:25.473621Z","shell.execute_reply.started":"2024-05-23T10:40:25.467343Z","shell.execute_reply":"2024-05-23T10:40:25.472660Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:25.478037Z","iopub.execute_input":"2024-05-23T10:40:25.478306Z","iopub.status.idle":"2024-05-23T10:40:28.857583Z","shell.execute_reply.started":"2024-05-23T10:40:25.478283Z","shell.execute_reply":"2024-05-23T10:40:28.856565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def im_multi(path):\n#     try:\n#         im_stats_im_ = Image.open(path)\n#         return [path, {'size': im_stats_im_.size}]\n#     except:\n#         print(path)\n#         return [path, {'size': [0,0]}]\n\n# def im_stats(im_stats_df):\n#     im_stats_d = {}\n#     p = Pool(cpu_count())\n#     ret = p.map(im_multi, im_stats_df['path'])\n#     for i in range(len(ret)):\n#         im_stats_d[ret[i][0]] =ret[i][1]#{'size': ret[i][1].shape[:2]} \n#     im_stats_df['size'] = im_stats_df['path'].map(lambda x: ' '.join(str(s) for s in im_stats_d[x]['size']))\n#     return im_stats_df\n\n# def get_im_cv2(path):\n#     img = cv2.imread(path)\n#     resized = cv2.resize(img, (64,64), cv2.INTER_LINEAR) \n#     resized = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n#     return [path, resized]\n\n# def normalize_image_features(paths):\n#     imf_d = {}\n#     p = Pool(cpu_count())\n#     ret = p.map(get_im_cv2, paths)\n#     for i in range(len(ret)):\n#         imf_d[ret[i][0]] = ret[i][1]\n#     fdata = [imf_d[f] for f in paths]\n#     fdata = np.array(fdata, dtype=np.uint8)\n#     fdata = fdata.transpose((0, 3, 1, 2))\n#     fdata = fdata.transpose(0,2,3,1)\n#     fdata = fdata.astype('float32')\n#     fdata = fdata / 255\n#     return fdata\n# def load_and_normalize_images(df):\n#       features = []\n#       for idx, row in df.iterrows():  # Iterate through DataFrame rows\n#             path = row['path']  # Assuming 'path' is the image path column\n#             img = cv2.imread(path)\n#             if img is None:\n#               print(f\"Error reading image: {path}\")\n#               continue  # Skip to next image on error\n\n#             resized_img = cv2.resize(img, (120, 120), cv2.INTER_LINEAR)  # Resize to 120x120\n#             normalized_img = resized_img.astype('float32') / 255  # Normalize to 0-1 range\n\n#             features.append(normalized_img)\n\n#       return np.array(features)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:28.858964Z","iopub.execute_input":"2024-05-23T10:40:28.859726Z","iopub.status.idle":"2024-05-23T10:40:28.866086Z","shell.execute_reply.started":"2024-05-23T10:40:28.859685Z","shell.execute_reply":"2024-05-23T10:40:28.865055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_data = load_and_normalize_images(train)\n# np.save('train.npy', train_data, allow_pickle=True, fix_imports=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:28.867616Z","iopub.execute_input":"2024-05-23T10:40:28.868269Z","iopub.status.idle":"2024-05-23T10:40:28.881836Z","shell.execute_reply.started":"2024-05-23T10:40:28.868232Z","shell.execute_reply":"2024-05-23T10:40:28.881004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df = im_stats(train_df)\n# train_df = train_df[train_df['size'] != '0 0'].reset_index(drop=True) #remove bad images\n# train_data = normalize_image_features(train_df['path'])\n# np.save('train.npy', train_data, allow_pickle=True, fix_imports=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:28.882910Z","iopub.execute_input":"2024-05-23T10:40:28.883181Z","iopub.status.idle":"2024-05-23T10:40:28.889007Z","shell.execute_reply.started":"2024-05-23T10:40:28.883157Z","shell.execute_reply":"2024-05-23T10:40:28.887984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"le = LabelEncoder().fit(['Type_1', 'Type_2', 'Type_3'])\ny_train = le.transform(train_labels)\ny_val = le.transform(val_labels)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:28.890225Z","iopub.execute_input":"2024-05-23T10:40:28.890612Z","iopub.status.idle":"2024-05-23T10:40:28.905279Z","shell.execute_reply.started":"2024-05-23T10:40:28.890576Z","shell.execute_reply":"2024-05-23T10:40:28.904488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# One-hot encoding\nfrom tensorflow.keras.utils import to_categorical\ny_train_one_hot = to_categorical(y_train, num_classes=3)\ny_val_one_hot = to_categorical(y_val, num_classes=3)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:28.906537Z","iopub.execute_input":"2024-05-23T10:40:28.906890Z","iopub.status.idle":"2024-05-23T10:40:28.915533Z","shell.execute_reply.started":"2024-05-23T10:40:28.906856Z","shell.execute_reply":"2024-05-23T10:40:28.914782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_features.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:28.916899Z","iopub.execute_input":"2024-05-23T10:40:28.917702Z","iopub.status.idle":"2024-05-23T10:40:28.924004Z","shell.execute_reply.started":"2024-05-23T10:40:28.917669Z","shell.execute_reply":"2024-05-23T10:40:28.922987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_one_hot.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:28.925002Z","iopub.execute_input":"2024-05-23T10:40:28.925309Z","iopub.status.idle":"2024-05-23T10:40:28.932488Z","shell.execute_reply.started":"2024-05-23T10:40:28.925277Z","shell.execute_reply":"2024-05-23T10:40:28.931518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# le = LabelEncoder()\n# train_target = le.fit_transform(train['type'].values)\n# print(le.classes_) #in case not 1 to 3 order\n# np.save('train_target.npy', train_target, allow_pickle=True, fix_imports=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:28.933656Z","iopub.execute_input":"2024-05-23T10:40:28.934615Z","iopub.status.idle":"2024-05-23T10:40:28.939112Z","shell.execute_reply.started":"2024-05-23T10:40:28.934446Z","shell.execute_reply":"2024-05-23T10:40:28.938096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test = glob.glob('../input/test/*.jpg')\n# test = pd.DataFrame([[p.split('/')[3],p] for p in test], columns = ['image','path']) #[::20] #limit for Kaggle Demo\n# test_data = load_and_normalize_images(test)\n# np.save('test.npy', test_data, allow_pickle=True, fix_imports=True)\n\n# test_id = test.image.values\n# np.save('test_id.npy', test_id, allow_pickle=True, fix_imports=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:28.940423Z","iopub.execute_input":"2024-05-23T10:40:28.941118Z","iopub.status.idle":"2024-05-23T10:40:28.946608Z","shell.execute_reply.started":"2024-05-23T10:40:28.941085Z","shell.execute_reply":"2024-05-23T10:40:28.945624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(len(x_train), len(y_train))","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:28.947663Z","iopub.execute_input":"2024-05-23T10:40:28.947939Z","iopub.status.idle":"2024-05-23T10:40:28.954027Z","shell.execute_reply.started":"2024-05-23T10:40:28.947901Z","shell.execute_reply":"2024-05-23T10:40:28.953260Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(dtype='float32',\n#                                   zoom_range=0.3,\n#                                 rotation_range = 0.3,\n#                                    horizontal_flip=True,\n#                                    vertical_flip=True,\n#                                   width_shift_range=0.2,\n#                                   height_shift_range=0.2,\n                                  preprocessing_function=preprocess_input)\nval_datagen = ImageDataGenerator(dtype='float32',preprocessing_function=preprocess_input)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:28.955055Z","iopub.execute_input":"2024-05-23T10:40:28.955647Z","iopub.status.idle":"2024-05-23T10:40:28.962132Z","shell.execute_reply.started":"2024-05-23T10:40:28.955622Z","shell.execute_reply":"2024-05-23T10:40:28.961313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# apply data augmentation to features\nBATCH_SIZE= 32\ntrain_gen = train_datagen.flow(train_features, y_train_one_hot, batch_size= BATCH_SIZE)\nval_gen = val_datagen.flow(val_features, y_val_one_hot, batch_size= BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:28.963160Z","iopub.execute_input":"2024-05-23T10:40:28.963436Z","iopub.status.idle":"2024-05-23T10:40:29.245884Z","shell.execute_reply.started":"2024-05-23T10:40:28.963414Z","shell.execute_reply":"2024-05-23T10:40:29.244801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_gen","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:29.247201Z","iopub.execute_input":"2024-05-23T10:40:29.247616Z","iopub.status.idle":"2024-05-23T10:40:29.254276Z","shell.execute_reply.started":"2024-05-23T10:40:29.247579Z","shell.execute_reply":"2024-05-23T10:40:29.253345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# show shape of each  batch\nfor data_batch, labels_batch in train_gen:\n    print('data batch shape: {} \\n labels batch shape: {}'.format(data_batch.shape, labels_batch.shape))\n    break\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:29.255600Z","iopub.execute_input":"2024-05-23T10:40:29.255973Z","iopub.status.idle":"2024-05-23T10:40:29.276324Z","shell.execute_reply.started":"2024-05-23T10:40:29.255945Z","shell.execute_reply":"2024-05-23T10:40:29.275293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.fit_generator(datagen.flow(x_train,y_train, batch_size=15, shuffle=True), nb_epoch=1, samples_per_epoch=len(x_train), verbose=20, validation_data=(x_val_train, y_val_train))","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:29.277696Z","iopub.execute_input":"2024-05-23T10:40:29.278057Z","iopub.status.idle":"2024-05-23T10:40:29.282577Z","shell.execute_reply.started":"2024-05-23T10:40:29.278024Z","shell.execute_reply":"2024-05-23T10:40:29.281565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from keras.applications.resnet50 import ResNet50\n\n# # Specify channel order as 'bgr'\n# resnet_model = ResNet50(weights='imagenet', include_top=False, input_shape=(64, 64, 3), channels_last=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:29.283807Z","iopub.execute_input":"2024-05-23T10:40:29.284106Z","iopub.status.idle":"2024-05-23T10:40:29.290604Z","shell.execute_reply.started":"2024-05-23T10:40:29.284070Z","shell.execute_reply":"2024-05-23T10:40:29.289671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def create_model(opt_='adamax'):\n#     model = Sequential()\n#     model.add(Convolution2D(4, 3, 3, activation='relu', dim_ordering='th', input_shape=(3, 32, 32))) #use input_shape=(3, 64, 64)\n#     model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), dim_ordering='th'))\n#     model.add(Convolution2D(8, 3, 3, activation='relu', dim_ordering='th'))\n#     model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), dim_ordering='th'))\n#     model.add(Dropout(0.2))\n    \n#     model.add(Flatten())\n#     model.add(Dense(12, activation='tanh'))\n#     model.add(Dropout(0.1))\n#     model.add(Dense(3, activation='softmax'))\n\n#     model.compile(optimizer=opt_, loss='sparse_categorical_crossentropy', metrics=['accuracy']) \n#     return model","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:29.291791Z","iopub.execute_input":"2024-05-23T10:40:29.292112Z","iopub.status.idle":"2024-05-23T10:40:29.298273Z","shell.execute_reply.started":"2024-05-23T10:40:29.292086Z","shell.execute_reply":"2024-05-23T10:40:29.297325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model = Sequential()\npretrained_model = tf.keras.applications.ResNet50(\n    include_top = False,\n    input_shape = (120,120,3),\n    pooling = 'avg',\n    classes = 3,\n    weights='imagenet'\n)\nfor layer in pretrained_model.layers[:2]:\n    layer.trainable = False\n\nresnet_model.add(pretrained_model)\nresnet_model.add(Flatten())\nresnet_model.add(Dropout(0.5))\nresnet_model.add(Dense(512,activation='relu'))\nresnet_model.add(Dense(3,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:29.303302Z","iopub.execute_input":"2024-05-23T10:40:29.303608Z","iopub.status.idle":"2024-05-23T10:40:32.088272Z","shell.execute_reply.started":"2024-05-23T10:40:29.303584Z","shell.execute_reply":"2024-05-23T10:40:32.087307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model = Sequential([conv_base, \n#                     Flatten(),\n#                    Dropout(0.5),\n#                    Dense(3, activation='softmax')])","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:32.089292Z","iopub.execute_input":"2024-05-23T10:40:32.089583Z","iopub.status.idle":"2024-05-23T10:40:32.093486Z","shell.execute_reply.started":"2024-05-23T10:40:32.089558Z","shell.execute_reply":"2024-05-23T10:40:32.092500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:32.094844Z","iopub.execute_input":"2024-05-23T10:40:32.095125Z","iopub.status.idle":"2024-05-23T10:40:32.135955Z","shell.execute_reply.started":"2024-05-23T10:40:32.095101Z","shell.execute_reply":"2024-05-23T10:40:32.134990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.compile(optimizer=Adam(0.0001), loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:32.137080Z","iopub.execute_input":"2024-05-23T10:40:32.137464Z","iopub.status.idle":"2024-05-23T10:40:32.153202Z","shell.execute_reply.started":"2024-05-23T10:40:32.137424Z","shell.execute_reply":"2024-05-23T10:40:32.152495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_STEPS = len(train_df)//BATCH_SIZE\nVAL_STEPS = len(val_df)//BATCH_SIZE","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:32.154204Z","iopub.execute_input":"2024-05-23T10:40:32.154482Z","iopub.status.idle":"2024-05-23T10:40:32.159089Z","shell.execute_reply.started":"2024-05-23T10:40:32.154448Z","shell.execute_reply":"2024-05-23T10:40:32.158018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reduceLR = ReduceLROnPlateau(monitor='val_loss',\n                             patience=10,\n                             verbose= 1,\n                             mode='min',\n                             factor=  0.2,\n                             min_lr = 1e-5)\n\nearly_stopping = EarlyStopping(monitor='val_accuracy',\n                               patience = 20,\n                               verbose=1,\n                               mode='max',\n                               restore_best_weights= True)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:40:32.160489Z","iopub.execute_input":"2024-05-23T10:40:32.161163Z","iopub.status.idle":"2024-05-23T10:40:32.166887Z","shell.execute_reply.started":"2024-05-23T10:40:32.161130Z","shell.execute_reply":"2024-05-23T10:40:32.165872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# resnet_model.load_weights('/kaggle/input/cervix-model/cervix_model(val1train78).keras')","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:41:19.943306Z","iopub.execute_input":"2024-05-23T10:41:19.943920Z","iopub.status.idle":"2024-05-23T10:41:19.948232Z","shell.execute_reply.started":"2024-05-23T10:41:19.943886Z","shell.execute_reply":"2024-05-23T10:41:19.947255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# filepath = \"saved-model-{epoch:02d}-{val_acc:.2f}.hdf5\"\nfilepath = \"saved-model-{epoch:02d}-acc-{accuracy:.2f}-val_acc_{val_accuracy:.2f}.keras\"\ncheckpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=False, mode='max')","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:41:22.904599Z","iopub.execute_input":"2024-05-23T10:41:22.905465Z","iopub.status.idle":"2024-05-23T10:41:22.909705Z","shell.execute_reply.started":"2024-05-23T10:41:22.905415Z","shell.execute_reply":"2024-05-23T10:41:22.908762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = resnet_model.fit(train_gen,\n                          validation_data=val_gen,\n                           steps_per_epoch= TRAIN_STEPS,\n                           validation_steps=VAL_STEPS,\n                          epochs=100,\n                          callbacks= [reduceLR,checkpoint]\n                          )","metadata":{"execution":{"iopub.status.busy":"2024-05-23T10:41:25.447600Z","iopub.execute_input":"2024-05-23T10:41:25.448440Z","iopub.status.idle":"2024-05-23T10:57:04.075340Z","shell.execute_reply.started":"2024-05-23T10:41:25.448384Z","shell.execute_reply":"2024-05-23T10:57:04.074002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.save('cervix_model.keras')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history_df = pd.DataFrame(history.history)","metadata":{"execution":{"iopub.status.busy":"2024-05-23T11:36:54.960137Z","iopub.execute_input":"2024-05-23T11:36:54.960915Z","iopub.status.idle":"2024-05-23T11:36:54.996101Z","shell.execute_reply.started":"2024-05-23T11:36:54.960881Z","shell.execute_reply":"2024-05-23T11:36:54.994962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize= (15,6))\nplt.subplot(1,2,1)\nplt.plot(history_df['accuracy'], label= 'accuracy' )\nplt.plot(history_df['val_accuracy'], label= 'val_accuracy')\n# history_df[['acc', 'val_acc']]\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.title('Training and Validation Accuracy History')\nplt.legend()\n\n# display history of loss\nplt.subplot(1,2,2)\nplt.plot(history_df['loss'], label= 'loss')\nplt.plot(history_df['val_loss'], label= 'val_loss')\n# history_df[['loss', 'val_loss']].plot()\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.title('Training and Validation Loss History')\nplt.legend()\n\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.evaluate(val_gen)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# loaded_model = keras.saving.load_model(\"model.keras\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}