{"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\nimport numpy as np\nimport pandas as pd\n\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-29T05:34:27.897472Z","iopub.execute_input":"2021-11-29T05:34:27.897976Z","iopub.status.idle":"2021-11-29T05:34:27.923265Z","shell.execute_reply.started":"2021-11-29T05:34:27.897883Z","shell.execute_reply":"2021-11-29T05:34:27.922623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport PIL\nimport cv2\nimport itertools\nimport os\nimport shutil\nimport random\nimport glob\nimport matplotlib.pyplot as plt\n%matplotlib inline\nimport warnings\n\nfrom tensorflow import keras\nfrom PIL import Image\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Activation, Dense, Flatten, BatchNormalization, Conv2D, MaxPool2D\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.metrics import binary_crossentropy\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.metrics import confusion_matrix\nfrom keras.models import Model, Sequential\nfrom keras.layers import Activation, Dense, BatchNormalization, concatenate, Dropout, Conv2D, Conv2DTranspose, MaxPooling2D, UpSampling2D, Input, Reshape\nfrom keras.callbacks import EarlyStopping\nfrom keras.layers.core import SpatialDropout2D\nfrom sklearn.metrics import precision_recall_curve\nfrom sklearn.metrics import plot_precision_recall_curve\nfrom sklearn.metrics import precision_score\nfrom sklearn.metrics import recall_score\nfrom keras import backend as K\n#from keras.optimizers import Adam\nfrom sklearn.model_selection import train_test_split\nfrom warnings import filterwarnings\n\nfilterwarnings('ignore')\nnp.random.seed(123)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:45:30.531720Z","iopub.execute_input":"2021-11-29T05:45:30.532082Z","iopub.status.idle":"2021-11-29T05:45:30.745971Z","shell.execute_reply.started":"2021-11-29T05:45:30.532046Z","shell.execute_reply":"2021-11-29T05:45:30.745094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASEPATH = \"../input/siim-isic-melanoma-classification\"\ndf_train = pd.read_csv(os.path.join(BASEPATH, 'train.csv'))\ndf_test  = pd.read_csv(os.path.join(BASEPATH, 'test.csv'))\ndf_sub   = pd.read_csv(os.path.join(BASEPATH, 'sample_submission.csv'))","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:46:13.652155Z","iopub.execute_input":"2021-11-29T05:46:13.652464Z","iopub.status.idle":"2021-11-29T05:46:13.799538Z","shell.execute_reply.started":"2021-11-29T05:46:13.652435Z","shell.execute_reply":"2021-11-29T05:46:13.798681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '../input/skin-cancer9-classesisic/Skin cancer ISIC The International Skin Imaging Collaboration/Train'\ntest_path = '../input/skin-cancer9-classesisic/Skin cancer ISIC The International Skin Imaging Collaboration/Test'","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:57:10.715135Z","iopub.execute_input":"2021-11-29T05:57:10.715489Z","iopub.status.idle":"2021-11-29T05:57:10.720094Z","shell.execute_reply.started":"2021-11-29T05:57:10.715457Z","shell.execute_reply":"2021-11-29T05:57:10.718869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \\\n    .flow_from_directory(directory=train_path, target_size=(224,224),batch_size=10)\nvalid_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \\\n    .flow_from_directory(directory=train_path, target_size=(224,224),batch_size=10)\ntest_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \\\n    .flow_from_directory(directory=test_path, target_size=(224,224),batch_size=10, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:57:13.815681Z","iopub.execute_input":"2021-11-29T05:57:13.816336Z","iopub.status.idle":"2021-11-29T05:57:14.579426Z","shell.execute_reply.started":"2021-11-29T05:57:13.816290Z","shell.execute_reply":"2021-11-29T05:57:14.578795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SEED = 123\nbatch_size = 10\nimg_height = 224\nimg_width =  224","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:57:24.847344Z","iopub.execute_input":"2021-11-29T05:57:24.848007Z","iopub.status.idle":"2021-11-29T05:57:24.852376Z","shell.execute_reply.started":"2021-11-29T05:57:24.847954Z","shell.execute_reply":"2021-11-29T05:57:24.851657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes=['pigmented benign keratosis',\n 'melanoma',\n 'vascular lesion',\n 'actinic keratosis',\n 'squamous cell carcinoma',\n 'basal cell carcinoma',\n 'seborrheic keratosis',\n 'dermatofibroma',\n 'nevus']","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:57:48.236281Z","iopub.execute_input":"2021-11-29T05:57:48.237059Z","iopub.status.idle":"2021-11-29T05:57:48.241201Z","shell.execute_reply.started":"2021-11-29T05:57:48.237021Z","shell.execute_reply":"2021-11-29T05:57:48.240452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pathlib\ntrain_dir = pathlib.Path(train_path)\ntest_dir = pathlib.Path(test_path)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:58:23.019802Z","iopub.execute_input":"2021-11-29T05:58:23.020273Z","iopub.status.idle":"2021-11-29T05:58:23.025575Z","shell.execute_reply.started":"2021-11-29T05:58:23.020224Z","shell.execute_reply":"2021-11-29T05:58:23.024627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport os, sys\n\npath = \"../input/siim-isic-melanoma-classification/jpeg/train\"\ndirs = os.listdir( path )\n\ndef resize():\n    for item in dirs:\n        if os.path.isfile(path+item):\n            im = Image.open(path+item)\n            f, e = os.path.splitext(path+item)\n            imResize = im.resize((224,224), Image.ANTIALIAS)\n            imResize.save(f + ' resized.jpg', 'JPEG', quality=90)\n\nresize()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:59:03.694053Z","iopub.execute_input":"2021-11-29T05:59:03.694597Z","iopub.status.idle":"2021-11-29T05:59:16.936047Z","shell.execute_reply.started":"2021-11-29T05:59:03.694536Z","shell.execute_reply":"2021-11-29T05:59:16.935125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imgs, labels = next(train_batches)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:59:21.494010Z","iopub.execute_input":"2021-11-29T05:59:21.494313Z","iopub.status.idle":"2021-11-29T05:59:21.990757Z","shell.execute_reply.started":"2021-11-29T05:59:21.494281Z","shell.execute_reply":"2021-11-29T05:59:21.989947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plotImages(images_arr):\n    fig, axes = plt.subplots(1, 10, figsize=(20,20))\n    axes = axes.flatten()\n    for img, ax in zip( images_arr, axes):\n        ax.imshow(img)\n        ax.axis('off')\n    plt.tight_layout()\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:59:26.608287Z","iopub.execute_input":"2021-11-29T05:59:26.608784Z","iopub.status.idle":"2021-11-29T05:59:26.614565Z","shell.execute_reply.started":"2021-11-29T05:59:26.608742Z","shell.execute_reply":"2021-11-29T05:59:26.613638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotImages(imgs)\nprint(labels)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T05:59:30.202800Z","iopub.execute_input":"2021-11-29T05:59:30.203117Z","iopub.status.idle":"2021-11-29T05:59:30.958137Z","shell.execute_reply.started":"2021-11-29T05:59:30.203076Z","shell.execute_reply":"2021-11-29T05:59:30.957222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Conv2D(filters=32, kernel_size=(3, 3), activation='relu', padding = 'same', input_shape=(224,224,3)),\n    MaxPool2D(pool_size=(2, 2), strides=2),\n    Conv2D(filters=64, kernel_size=(3, 3), activation='relu', padding = 'same'),\n    MaxPool2D(pool_size=(2, 2), strides=2),\n    Flatten(),\n    Dense(units= 9, activation='sigmoid')\n])","metadata":{"execution":{"iopub.status.busy":"2021-11-29T06:14:31.665402Z","iopub.execute_input":"2021-11-29T06:14:31.665714Z","iopub.status.idle":"2021-11-29T06:14:31.721686Z","shell.execute_reply.started":"2021-11-29T06:14:31.665684Z","shell.execute_reply":"2021-11-29T06:14:31.720873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=Adam(learning_rate=0.0001), loss='binary_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-11-29T06:14:33.493243Z","iopub.execute_input":"2021-11-29T06:14:33.493515Z","iopub.status.idle":"2021-11-29T06:14:33.504346Z","shell.execute_reply.started":"2021-11-29T06:14:33.493488Z","shell.execute_reply":"2021-11-29T06:14:33.503368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-29T06:16:30.960489Z","iopub.execute_input":"2021-11-29T06:16:30.960859Z","iopub.status.idle":"2021-11-29T06:16:30.970457Z","shell.execute_reply.started":"2021-11-29T06:16:30.960823Z","shell.execute_reply":"2021-11-29T06:16:30.969558Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CollectBatchStats(tf.keras.callbacks.Callback):\n  def __init__(self):\n    self.batch_losses = []\n    self.batch_acc = []\n    \n  def on_batch_end(self, batch, logs=None):\n    self.batch_losses.append(logs['loss'])\n    self.batch_acc.append(logs['accuracy'])\n    \n# Early stopping to stop the training if loss start to increase. It also avoids overfitting.\nes = EarlyStopping(patience=3,monitor=\"val_loss\")","metadata":{"execution":{"iopub.status.busy":"2021-11-29T06:16:33.433036Z","iopub.execute_input":"2021-11-29T06:16:33.433349Z","iopub.status.idle":"2021-11-29T06:16:33.439784Z","shell.execute_reply.started":"2021-11-29T06:16:33.433316Z","shell.execute_reply":"2021-11-29T06:16:33.438873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping\n#set early stopping monitor so the model stops training when it won't improve anymore\nearly_stopping_monitor = EarlyStopping(patience=3)\n\n#train model\nhistory = model.fit(x=train_batches,\n    steps_per_epoch=len(train_batches),\n    validation_data=valid_batches,\n    validation_steps=len(valid_batches),\n                    epochs=10,\n                    verbose=2,\n                    callbacks=[early_stopping_monitor]\n                   )","metadata":{"execution":{"iopub.status.busy":"2021-11-29T06:16:36.506334Z","iopub.execute_input":"2021-11-29T06:16:36.507164Z","iopub.status.idle":"2021-11-29T06:36:01.032638Z","shell.execute_reply.started":"2021-11-29T06:16:36.507121Z","shell.execute_reply":"2021-11-29T06:36:01.031525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_names = sorted(train_batches.class_indices.items(), key=lambda pair:pair[1])\nlabel_names = np.array([key.title() for key, value in label_names])\nlabel_names","metadata":{"execution":{"iopub.status.busy":"2021-11-29T06:49:42.067187Z","iopub.execute_input":"2021-11-29T06:49:42.067674Z","iopub.status.idle":"2021-11-29T06:49:42.076909Z","shell.execute_reply.started":"2021-11-29T06:49:42.067632Z","shell.execute_reply":"2021-11-29T06:49:42.075938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result_batch = model.predict(test_batches)\n\nlabels_batch = label_names[np.argmax(result_batch, axis=-1)]\nlabels_batch","metadata":{"execution":{"iopub.status.busy":"2021-11-29T06:49:48.632667Z","iopub.execute_input":"2021-11-29T06:49:48.632979Z","iopub.status.idle":"2021-11-29T06:50:31.284956Z","shell.execute_reply.started":"2021-11-29T06:49:48.632946Z","shell.execute_reply":"2021-11-29T06:50:31.283979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_imgs, test_labels = next(test_batches)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T06:51:29.791041Z","iopub.execute_input":"2021-11-29T06:51:29.791400Z","iopub.status.idle":"2021-11-29T06:51:29.923764Z","shell.execute_reply.started":"2021-11-29T06:51:29.791364Z","shell.execute_reply":"2021-11-29T06:51:29.923033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plotImages(test_imgs)\nprint(test_labels)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T06:51:32.327233Z","iopub.execute_input":"2021-11-29T06:51:32.327965Z","iopub.status.idle":"2021-11-29T06:51:33.076104Z","shell.execute_reply.started":"2021-11-29T06:51:32.327898Z","shell.execute_reply":"2021-11-29T06:51:33.075446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"saved_model = model.save(\"melanoma_model\")\nfrom keras.preprocessing import image\nimg = image.load_img('../input/skin-cancer9-classesisic/Skin cancer ISIC The International Skin Imaging Collaboration/Test/dermatofibroma/ISIC_0001114.jpg',target_size=(224,224))\nimg = np.asarray(img)\nplt.imshow(img)\nimg = np.expand_dims(img, axis=0)\n\nfrom keras.models import load_model\nsaved_model = load_model(\"melanoma_model\")\noutput = saved_model.predict(img)\nif output[0][0] > output[0][1]:\n    print(\"melanoma\")\nelse:\n    print('not melanoma')","metadata":{"execution":{"iopub.status.busy":"2021-11-29T06:51:39.033962Z","iopub.execute_input":"2021-11-29T06:51:39.034709Z","iopub.status.idle":"2021-11-29T06:51:41.207160Z","shell.execute_reply.started":"2021-11-29T06:51:39.034666Z","shell.execute_reply":"2021-11-29T06:51:41.203734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(x=test_batches, steps=len(test_batches), verbose=0)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T06:52:01.823314Z","iopub.execute_input":"2021-11-29T06:52:01.824194Z","iopub.status.idle":"2021-11-29T06:52:11.113204Z","shell.execute_reply.started":"2021-11-29T06:52:01.824147Z","shell.execute_reply":"2021-11-29T06:52:11.112221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.round(predictions)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T06:52:11.115498Z","iopub.execute_input":"2021-11-29T06:52:11.116085Z","iopub.status.idle":"2021-11-29T06:52:11.122882Z","shell.execute_reply.started":"2021-11-29T06:52:11.116046Z","shell.execute_reply":"2021-11-29T06:52:11.122212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(model = model , rankdir=\"TB\", dpi=72, show_shapes=True)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T06:52:11.124398Z","iopub.execute_input":"2021-11-29T06:52:11.124951Z","iopub.status.idle":"2021-11-29T06:52:12.159455Z","shell.execute_reply.started":"2021-11-29T06:52:11.124886Z","shell.execute_reply":"2021-11-29T06:52:12.158747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(test_imgs, test_labels)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T06:52:12.162043Z","iopub.execute_input":"2021-11-29T06:52:12.162289Z","iopub.status.idle":"2021-11-29T06:52:12.442082Z","shell.execute_reply.started":"2021-11-29T06:52:12.162259Z","shell.execute_reply":"2021-11-29T06:52:12.441441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('test data', test_imgs)\npreds = np.round(model.predict(test_imgs),0) \n#to fit them into classification metrics and confusion metrics, some additional modificaitions are required\nprint('rounded test_labels', preds)","metadata":{"execution":{"iopub.status.busy":"2021-11-29T06:52:12.443172Z","iopub.execute_input":"2021-11-29T06:52:12.443484Z","iopub.status.idle":"2021-11-29T06:52:12.616309Z","shell.execute_reply.started":"2021-11-29T06:52:12.443442Z","shell.execute_reply":"2021-11-29T06:52:12.614801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 10\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(epochs)\n\nplt.figure(figsize=(8, 6))\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.status.busy":"2021-11-29T06:52:12.617965Z","iopub.execute_input":"2021-11-29T06:52:12.618713Z","iopub.status.idle":"2021-11-29T06:52:12.871775Z","shell.execute_reply.started":"2021-11-29T06:52:12.618662Z","shell.execute_reply":"2021-11-29T06:52:12.870553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}