{"cells":[{"metadata":{"id":"bX-8NU8Hq2ZY"},"cell_type":"markdown","source":"# Import packages","execution_count":null},{"metadata":{"executionInfo":{"elapsed":6857,"status":"ok","timestamp":1595643804164,"user":{"displayName":"Marcelo Kittlein","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Ghv6hUZzvOc2l_7uFWucdAjxsS-C7qBYXpt85VBXjE=s64","userId":"00182672135053440674"},"user_tz":180},"id":"2UJ1IbrnGh19","outputId":"c59a6489-0609-47ee-c599-ab90b93d08cc","trusted":true},"cell_type":"code","source":"!pip install -q efficientnet\nfrom keras import backend as K\nfrom keras.callbacks import EarlyStopping, LearningRateScheduler, ModelCheckpoint, Callback\nfrom keras.layers import Activation, Conv1D, Conv2D, Dense, Dropout, Flatten, Input, average, concatenate, MaxPool1D, MaxPooling2D, MaxPool2D\nfrom keras.models import Sequential, Model, load_model\nfrom keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport efficientnet.tfkeras as efn","execution_count":null,"outputs":[]},{"metadata":{"executionInfo":{"elapsed":7245,"status":"ok","timestamp":1595643804559,"user":{"displayName":"Marcelo Kittlein","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Ghv6hUZzvOc2l_7uFWucdAjxsS-C7qBYXpt85VBXjE=s64","userId":"00182672135053440674"},"user_tz":180},"id":"EmZpIkJFtYdN","outputId":"a01ba240-71be-428d-db4c-cc9ae554112e","trusted":true},"cell_type":"code","source":"trainTab = pd.read_csv(\"../input/jpeg-melanoma-256x256/train.csv\")\ntestTab = pd.read_csv(\"../input/jpeg-melanoma-256x256/test.csv\")\n\ndim=256\nbs=64\n\ntarget=np.array(trainTab['target'].values)\n\nmali = target == 1\nimali = np.arange(0, trainTab.shape[0], 1, dtype='int')\nimali = imali[mali]\n\nbeni = target == 0\nibeni = np.arange(0, trainTab.shape[0], 1, dtype='int')\nibeni = ibeni[beni]\n\nRows = np.arange(0, trainTab.shape[0], 1)\ntrbeni = np.random.choice(ibeni, size=int(len(ibeni)*0.6), replace=False)\nvlbeni = np.array([a for a in Rows if not a in trbeni], dtype='int')\n\ntrmali=np.repeat(imali, 5)\nvlmali=imali\n\ntrainRows = np.random.choice([*trbeni, *trmali], size=len(trbeni)+len(trmali), replace=False)\nvalRows = np.random.choice([*vlmali, *vlbeni], size=len(vlbeni)+len(vlmali), replace=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"raw","source":"mali = target == 1\nimali = np.arange(0, trainTab.shape[0], 1, dtype='int')\nimali = imali[mali]\n\nbeni = target == 0\nibeni = np.arange(0, trainTab.shape[0], 1, dtype='int')\nibeni = ibeni[beni]\n\nRows = np.arange(0, trainTab.shape[0], 1)\ntrbeni = np.random.choice(ibeni, size=int(len(ibeni)*0.6), replace=False)\nvlbeni = np.array([a for a in Rows if not a in trbeni], dtype='int')\n\ntrmali=np.repeat(imali, 20)\nvlmali=imali\n\ntrainRows = np.random.choice([*trbeni, *trmali], size=len(trbeni)+len(trmali), replace=False)\nvalRows = np.random.choice([*vlmali, *vlbeni], size=len(vlbeni)+len(vlmali), replace=False)","execution_count":null},{"metadata":{},"cell_type":"markdown","source":"# Fill list with image names jpg and png files","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"TrainJPG=[]\nfor i in range(trainTab.shape[0]):\n    TrainJPG.append('../input/jpeg-melanoma-256x256/train/' + str(trainTab['image_name'][i]) + '.jpg')\n    \nTestJPG=[]\nfor i in range(testTab.shape[0]):\n    TestJPG.append('../input/jpeg-melanoma-256x256/test/' + str(testTab['image_name'][i]) + '.jpg')\n    \nTrainPNG=[]\nfor i in range(trainTab.shape[0]):\n    TrainPNG.append('../input/landscape/tabFolders256/trainTab/' + str(trainTab['image_name'][i]) + '.png')\n\nTestPNG=[]\nfor i in range(testTab.shape[0]):\n    TestPNG.append('../input/landscape/tabFolders256/testTab/' + str(testTab['image_name'][i]) + '.png')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Define the image generators","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_args = dict(brightness_range = [0.5,1.5], rescale=1./255, width_shift_range=0.5, height_shift_range=0.5,\n                  shear_range=0.6, zoom_range=0.6, horizontal_flip = True, rotation_range = 90)\n\nval_args = dict(rescale=1./255)\n\ndatagen = ImageDataGenerator(**train_args)\n\nVdatagen = ImageDataGenerator(**val_args)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Define image indices and batch iterator","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"trIndi=np.arange(0, len(trainRows), 1, dtype='int')\nvlIndi=np.arange(0, len(valRows), 1, dtype='int')\nteIndi=np.arange(0, testTab.shape[0], 1, dtype='int')\n\ndef batch(iterable, n=1):\n    l = len(iterable)\n    for ndx in range(0, l, n):\n        yield iterable[ndx:min(ndx + n, l)]\n\nggg=np.arange(0,200,1,dtype='int') \n\nfor x in batch(ggg, 125):\n    print(len(x))\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Functions to yield images and target to the CNN","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def gT():\n    while True:\n        for x in batch(vlIndi, bs):\n            imArrayJPG=np.zeros((len(x), dim, dim, 3))\n            imArrayPNG=np.zeros((len(x), dim, dim, 1))\n            for j in range(len(x)):\n                ima=load_img(TrainPNG[trainRows[x[j]]], color_mode=\"grayscale\")\n                ima=img_to_array(ima)\n                imArrayPNG[j,:,:,0]=ima[:,:,0]\n                ima=load_img(TrainJPG[trainRows[x[j]]], color_mode=\"rgb\")\n                ima=img_to_array(ima)\n                imArrayJPG[j,:,:,0:3]=ima[:,:,0:3]\n            outPNG=Vdatagen.flow(imArrayPNG, batch_size=len(x), shuffle=False)\n            outJPG=datagen.flow(imArrayJPG, batch_size=len(x), shuffle=False)\n            salPNG=outPNG.next()\n            salJPG=outJPG.next()\n            yield [salPNG, salJPG], target[trainRows[x]]\n\ndef gV():\n    while True:\n        for x in batch(vlIndi, bs):\n            imArrayJPG=np.zeros((len(x), dim, dim, 3))\n            imArrayPNG=np.zeros((len(x), dim, dim, 1))\n            for j in range(len(x)):\n                ima=load_img(TrainPNG[valRows[x[j]]], color_mode=\"grayscale\")\n                ima=img_to_array(ima)\n                imArrayPNG[j,:,:,0]=ima[:,:,0]\n                ima=load_img(TrainJPG[valRows[x[j]]], color_mode=\"rgb\")\n                ima=img_to_array(ima)\n                imArrayJPG[j,:,:,0:3]=ima[:,:,0:3]\n            outPNG=Vdatagen.flow(imArrayPNG, batch_size=len(x), shuffle=False)\n            outJPG=Vdatagen.flow(imArrayJPG, batch_size=len(x), shuffle=False)\n            salPNG=outPNG.next()\n            salJPG=outJPG.next()\n            yield [salPNG, salJPG], target[valRows[x]]\n\ndef gTe():\n    while True:\n        for x in batch(teIndi, 34):\n            imArrayJPG=np.zeros((len(x), dim, dim, 3))\n            imArrayPNG=np.zeros((len(x), dim, dim, 1))\n            for j in range(len(x)):\n                ima=load_img(TestPNG[x[j]], color_mode=\"grayscale\")\n                ima=img_to_array(ima)\n                imArrayPNG[j,:,:,0]=ima[:,:,0]\n                ima=load_img(TestJPG[x[j]], color_mode=\"rgb\")\n                ima=img_to_array(ima)\n                imArrayJPG[j,:,:,0:3]=ima[:,:,0:3]\n            outPNG=Vdatagen.flow(imArrayPNG, batch_size=len(x), shuffle=False)\n            outJPG=Vdatagen.flow(imArrayJPG, batch_size=len(x), shuffle=False)\n            salPNG=outPNG.next()\n            salJPG=outJPG.next()\n            yield [salPNG, salJPG]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xu=gT()\n\na=0\nfor i in xu:\n    if a >= 4:\n        break\n    a=a+1\n    print(i[0][0].shape)\n    print(i[0][1].shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(16, 4.5))\n\na=0\nfor i in xu:\n    if a >= 20:\n        break\n    plt.subplot(2, 10, a+1)\n    a=a+1\n    plt.imshow(i[0][0][a,:,:,0],cmap=plt.cm.binary)\n    plt.axis('off')\nplt.subplots_adjust(wspace=0.1, hspace=-0.3)\nplt.show()\n\nplt.figure(figsize=(16, 4.5))\n\na=0\nfor i in xu:\n    if a >= 20:\n        break\n    plt.subplot(2, 10, a+1)\n    a=a+1\n    plt.imshow(i[0][1][a,:,:,0:3],cmap=plt.cm.binary)\n    plt.axis('off')\nplt.subplots_adjust(wspace=0.1, hspace=-0.3)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Define CNNs pretrained and custom models","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"ks= 3\nx = Sequential()\nx.add(Conv2D(24, kernel_size = ks, activation='relu', padding='same', input_shape = (dim, dim, 1)))\nx.add(MaxPool2D())\nx.add(Conv2D(48, kernel_size = ks, activation='relu', padding='same'))\nx.add(MaxPool2D(padding='same'))\nx.add(Conv2D(64, kernel_size = ks, activation='relu', padding='same'))\nx.add(Flatten())\nx.add(Dropout(0.3))\nx.add(Dense(128, activation=\"relu\"))\n\nbase_model = efn.EfficientNetB0(input_shape=(dim, dim,3), weights='imagenet', include_top=False, pooling='avg')\nbase_model.trainable = False\ny = base_model.output\ny = Dense(512)(y)\ny = Dropout(0.4)(y)\ny = Dense(256)(y)\ny = Dropout(0.3)(y)\ny = Dense(128)(y)\ny = Dense(256, activation='relu')(y)\ny = Model(inputs=base_model.input, outputs=y)\n\ncombined = concatenate([x.output, y.output], axis=1)\ncombined= Dense(1, activation=\"sigmoid\")(combined)\n\nmodel = Model(inputs=[x.input, y.input], outputs=combined)","execution_count":null,"outputs":[]},{"metadata":{"executionInfo":{"elapsed":133437,"status":"ok","timestamp":1595643930769,"user":{"displayName":"Marcelo Kittlein","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14Ghv6hUZzvOc2l_7uFWucdAjxsS-C7qBYXpt85VBXjE=s64","userId":"00182672135053440674"},"user_tz":180},"id":"JiZ1KdhZcusd","trusted":true},"cell_type":"code","source":"from keras import backend as K\n\ndef binary_focal_loss(gamma=2., alpha=.75):\n    def binary_focal_loss_fixed(y_true, y_pred):\n        pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))\n        pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))\n\n        epsilon = K.epsilon()\n        # clip to prevent NaN's and Inf's\n        pt_1 = K.clip(pt_1, epsilon, 1. - epsilon)\n        pt_0 = K.clip(pt_0, epsilon, 1. - epsilon)\n\n        return -K.sum(alpha * K.pow(1. - pt_1, gamma) * K.log(pt_1)) \\\n               -K.sum((1 - alpha) * K.pow(pt_0, gamma) * K.log(1. - pt_0))\n\n    return binary_focal_loss_fixed\n\n# COMPILE WITH ADAM OPTIMIZER AND CROSS ENTROPY COST\nmodel.compile(optimizer= 'adam', loss=binary_focal_loss(), metrics=[tf.keras.metrics.AUC()])\n\nfilepath='modelEffNet2CNNs.h5'","execution_count":null,"outputs":[]},{"metadata":{"id":"hQuH3cRuasHl","outputId":"aadcd961-697b-4360-f792-62f37ef3194c","trusted":true},"cell_type":"code","source":"# CUSTOM LEARNING SCHEUDLE\nLR_START = 0.00001\nLR_MAX = 0.001\nLR_RAMPUP_EPOCHS = 5\nLR_SUSTAIN_EPOCHS = 0\nLR_STEP_DECAY = 0.75\n\ndef lrfn(epoch):\n    if epoch < LR_RAMPUP_EPOCHS:\n        lr = (LR_MAX - LR_START) / LR_RAMPUP_EPOCHS * epoch + LR_START\n    elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:\n        lr = LR_MAX\n    else:\n        lr = LR_MAX * LR_STEP_DECAY**((epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS)//10)\n    return lr\n\n\nlr2 = LearningRateScheduler(lrfn, verbose = True)\n\nEarLY=EarlyStopping(monitor='val_loss', mode='min', min_delta=0, patience=30, verbose=0,\n                                    restore_best_weights=True)\n\ncheckpoint = ModelCheckpoint(filepath, monitor='val_loss', mode='min', verbose=1, save_best_only=True)\n\nepochs = 6\ntsteps = int(len(trainRows)/bs)\nvsteps = int(len(valRows)/bs)\n\nhistory = model.fit_generator(gT(), epochs = epochs, verbose=1, \n                              validation_data = gV(),\n                              steps_per_epoch = tsteps, validation_steps=vsteps,\n                              callbacks=[lr2, EarLY, checkpoint])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Predict target","execution_count":null},{"metadata":{"id":"E5DR-am7tYdc","trusted":true},"cell_type":"code","source":"bestModel=model\nbestModel.load_weights(filepath)\npredi=bestModel.predict_generator(gTe(), steps=323, verbose=1)\nsubmi = pd.read_csv(\"../input/jpeg-melanoma-256x256/sample_submission.csv\")\nsubmi[\"target\"]=predi\nsubmi.to_csv(\"submi2CNNs.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Distribution of predicted target","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"MalignCases=len(np.argwhere(predi > 0.5))\n\nfig, ax = plt.subplots(figsize=(16,6))\nax.set_yscale('log')\nsns.distplot(predi, hist_kws={\n                 'rwidth': 0.75,\n                 'edgecolor': 'black',\n                 'alpha': 0.3\n             }, color='r', kde=False)\nax.set_title('Final Predictions with ' + str(MalignCases) + ' malign cases')\nplt.ylabel(\"Counts (log scale)\")\nplt.xlabel(\"Probability target = 1\")\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Plot history","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(12,5))\n\nplt.subplot(1, 2, 1)\nplt.plot(history.history['loss'],'b')  \nplt.plot(history.history['val_loss'],'r')  \nplt.xticks(np.arange(0, epochs, epochs/10)) \nplt.xlabel(\"Num of Epochs\")  \nplt.ylabel(\"Loss\")  \nplt.title(\"Training Loss vs Validation Loss\")  \nplt.legend(['train','validation'])\naxes = plt.gca()\naxes.set_ylim([0,20])\n\nplt.subplot(1, 2, 2)\nplt.plot(history.history['auc'],'b')  \nplt.plot(history.history['val_auc'],'r')  \nplt.xticks(np.arange(0, epochs, epochs/10)) \nplt.xlabel(\"Num of Epochs\")  \nplt.ylabel(\"AUC\")  \nplt.title(\"Training AUC vs Validation AUC\")  \nplt.legend(['train','validation'])\naxes = plt.gca()\naxes.set_ylim([0,1])\n\nplt.show() ","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}