{"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":"import pandas as pd\nimport numpy as np \nimport cv2\nimport tensorflow as tf\nfrom tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input\nfrom tensorflow.keras.layers import Dense, Flatten, GlobalAveragePooling2D, Dropout\nfrom tensorflow.keras.models import Sequential, Model\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping\nfrom sklearn.utils import class_weight\nimport os","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":8.17834,"end_time":"2023-05-20T15:18:26.678829","exception":false,"start_time":"2023-05-20T15:18:18.500489","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-19T15:41:52.698206Z","iopub.execute_input":"2023-09-19T15:41:52.698609Z","iopub.status.idle":"2023-09-19T15:41:52.705769Z","shell.execute_reply.started":"2023-09-19T15:41:52.698576Z","shell.execute_reply":"2023-09-19T15:41:52.704679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"os.listdir(\"../input/\")\nbasepath = \"../input/siim-isic-melanoma-classification/\"\nmodelspath = \"../input/pytorch-pretrained-image-models/\"","metadata":{"papermill":{"duration":0.003882,"end_time":"2023-05-20T15:18:26.687154","exception":false,"start_time":"2023-05-20T15:18:26.683272","status":"completed"},"tags":[]}},{"cell_type":"code","source":"def map_imgs(meta_file_path, image_file_path):\n    IMAGE_PATH = image_file_path\n#     meta_file= '/kaggle/input/siim-isic-melanoma-classification/train.csv'\n#     IMAGE_PATH = '/kaggle/input/siim-isic-melanoma-classification/jpeg/train'\n    \n    df = pd.read_csv(meta_file_path)\n\n    # adding new column = filepath (complete image path)\n    df['image_path'] = df['image_name'].map(lambda x:  os.path.join(IMAGE_PATH,x+'.jpg'))\n\n    # mapping dictionary\n    labelmap = {}\n    for l in df.target.unique().tolist():\n        if l == 1:\n            labelmap[l] = 'melanoma'\n        else:\n            labelmap[l] = 'benign'\n\n    # seperate list of image that are labelled = melanoma\n    df_melanoma = df[df['target'] == 1]\n    df_melanoma.reset_index(drop=True, inplace=True)\n\n    # view \n    print(f\"Found {df.groupby('target').count()['image_name'][1]} images that are labelled = melanoma and {df.groupby('target').count()['image_name'][0]} that are labelled = benign\")\n    return df","metadata":{"papermill":{"duration":0.017756,"end_time":"2023-05-20T15:18:26.708823","exception":false,"start_time":"2023-05-20T15:18:26.691067","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-19T15:41:57.486164Z","iopub.execute_input":"2023-09-19T15:41:57.486910Z","iopub.status.idle":"2023-09-19T15:41:57.494337Z","shell.execute_reply.started":"2023-09-19T15:41:57.486868Z","shell.execute_reply":"2023-09-19T15:41:57.493271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = map_imgs(\"/kaggle/input/siim-isic-melanoma-classification/train.csv\",\"/kaggle/input/siim-isic-melanoma-classification/jpeg/train\")\ndf_train= df_train[[\"target\",\"image_path\"]]\ndf_train[\"target\"] = df_train[\"target\"].astype(str)\ndf_train","metadata":{"papermill":{"duration":0.365848,"end_time":"2023-05-20T15:18:27.078809","exception":false,"start_time":"2023-05-20T15:18:26.712961","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-19T15:42:03.904035Z","iopub.execute_input":"2023-09-19T15:42:03.905060Z","iopub.status.idle":"2023-09-19T15:42:04.116801Z","shell.execute_reply.started":"2023-09-19T15:42:03.905023Z","shell.execute_reply":"2023-09-19T15:42:04.115610Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_generator = ImageDataGenerator(preprocessing_function=preprocess_input, validation_split=0.2)\ntrain_generator = data_generator.flow_from_dataframe(df_train,\n                                              target_size= (224, 224),\n                                                     x_col='image_path',\n                                                     y_col='target',\n                                              batch_size = 128,\n                                              subset = 'training',\n                                              shuffle = True,\n                                              class_mode ='binary')\nval_generator = data_generator.flow_from_dataframe(df_train,\n                                              target_size= (224, 224),x_col='image_path',\n                                                     y_col='target',\n                                              batch_size = 128,\n                                              subset = 'validation',\n                                              shuffle = False,\n                                              class_mode ='binary')","metadata":{"papermill":{"duration":82.648434,"end_time":"2023-05-20T15:19:49.731765","exception":false,"start_time":"2023-05-20T15:18:27.083331","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-19T15:42:08.187179Z","iopub.execute_input":"2023-09-19T15:42:08.187877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_weights = class_weight.compute_class_weight('balanced',\n                                                 classes= np.unique(df_train[\"target\"]),\n                                                 y =df_train[\"target\"])\nclass_weight = dict(enumerate(class_weights.flatten(), 0))\nclass_weight","metadata":{"papermill":{"duration":0.096831,"end_time":"2023-05-20T15:19:49.832949","exception":false,"start_time":"2023-05-20T15:19:49.736118","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-19T15:43:25.517864Z","iopub.execute_input":"2023-09-19T15:43:25.518226Z","iopub.status.idle":"2023-09-19T15:43:25.583364Z","shell.execute_reply.started":"2023-09-19T15:43:25.518197Z","shell.execute_reply":"2023-09-19T15:43:25.582349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = ResNet50(include_top=False, pooling='avg', weights='imagenet', input_shape=(224,224,3))\nfor layer in base_model.layers[:-4]:\n    layer.trainable = False","metadata":{"papermill":{"duration":4.920771,"end_time":"2023-05-20T15:19:54.758295","exception":false,"start_time":"2023-05-20T15:19:49.837524","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-19T15:43:31.594670Z","iopub.execute_input":"2023-09-19T15:43:31.595032Z","iopub.status.idle":"2023-09-19T15:43:39.339121Z","shell.execute_reply.started":"2023-09-19T15:43:31.595005Z","shell.execute_reply":"2023-09-19T15:43:39.337964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEPS_PER_EPOCH = 26501 // 128\nVALID_STEPS = 6625 // 128","metadata":{"papermill":{"duration":0.014114,"end_time":"2023-05-20T15:19:54.778314","exception":false,"start_time":"2023-05-20T15:19:54.764200","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2023-09-19T15:43:41.649784Z","iopub.execute_input":"2023-09-19T15:43:41.650660Z","iopub.status.idle":"2023-09-19T15:43:41.656790Z","shell.execute_reply.started":"2023-09-19T15:43:41.650615Z","shell.execute_reply":"2023-09-19T15:43:41.654956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_model = Sequential([base_model])\n# my_model.add(GlobalAveragePooling2D())\nmy_model.add(Dense(512, activation='relu'))\nmy_model.add(Dropout(0.5))\nmy_model.add(Dense(1, activation='sigmoid'))\n\nmy_model.compile(optimizer=Adam(learning_rate=0.0001),\n             loss='binary_crossentropy',\n             metrics= ['accuracy'])\n\nmodel_name = \"melanoma_model.h5\"\ncheckpoint = ModelCheckpoint(model_name,\n                            monitor=\"val_loss\",\n                            mode=\"min\",\n                            save_best_only = True,\n                            verbose=1)\n\nearlystopping = EarlyStopping(monitor='val_loss',min_delta = 0, patience = 5, verbose = 1, restore_best_weights=True)\n\ntry:\n    history = my_model.fit(train_generator,\n                           epochs=10,\n                           steps_per_epoch=STEPS_PER_EPOCH,\n                           validation_data=val_generator,\n                           validation_steps=VALID_STEPS,\n                           callbacks=[checkpoint,earlystopping],\n                           class_weight=class_weight)\nexcept KeyboardInterrupt:\n    print(\"\\nTraining Stopped\")","metadata":{"execution":{"iopub.execute_input":"2023-05-20T15:19:54.789593Z","iopub.status.busy":"2023-05-20T15:19:54.789328Z","iopub.status.idle":"2023-05-20T23:10:10.580179Z","shell.execute_reply":"2023-05-20T23:10:10.578473Z"},"papermill":{"duration":28215.83621,"end_time":"2023-05-20T23:10:10.619676","exception":false,"start_time":"2023-05-20T15:19:54.783466","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nacc=history.history['accuracy']\nval_acc=history.history['val_accuracy']\nloss=history.history['loss']\nval_loss=history.history['val_loss']\nepochs=10\nepochs_range=range(epochs)\n\nplt.figure(figsize=(8, 8))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.execute_input":"2023-05-20T23:10:10.939145Z","iopub.status.busy":"2023-05-20T23:10:10.938394Z","iopub.status.idle":"2023-05-20T23:10:11.508863Z","shell.execute_reply":"2023-05-20T23:10:11.507953Z"},"papermill":{"duration":0.733781,"end_time":"2023-05-20T23:10:11.511421","exception":false,"start_time":"2023-05-20T23:10:10.777640","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = my_model.evaluate(val_generator)\nprint('Test loss:', score[0])\nprint('Test accuracy:', score[1])","metadata":{"execution":{"iopub.execute_input":"2023-05-20T23:10:11.831458Z","iopub.status.busy":"2023-05-20T23:10:11.831144Z","iopub.status.idle":"2023-05-20T23:19:54.184586Z","shell.execute_reply":"2023-05-20T23:19:54.183611Z"},"papermill":{"duration":582.516098,"end_time":"2023-05-20T23:19:54.187718","exception":false,"start_time":"2023-05-20T23:10:11.671620","status":"completed"},"tags":[]},"execution_count":null,"outputs":[]}]}