{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 # linear algebra\nimport 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\nimport 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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom tqdm.auto import tqdm\nfrom glob import glob\nimport time, gc\nimport cv2\nfrom keras import backend as K\nimport matplotlib.image as mpimg\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.models import Sequential\nfrom keras.models import clone_model\nfrom keras.layers import Dense,Conv2D,Flatten,MaxPool2D,Dropout,BatchNormalization,Activation\nfrom keras.optimizers import Adam\nfrom keras.callbacks import ReduceLROnPlateau, ModelCheckpoint\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport PIL.Image as Image, PIL.ImageDraw as ImageDraw, PIL.ImageFont as ImageFont\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\nfrom keras.models import Model,Sequential, Input, load_model\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, BatchNormalization, AveragePooling2D, GlobalAveragePooling2D\nfrom keras.optimizers import Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\nfrom keras.applications import DenseNet121","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport gc\nimport json\nimport math\nimport cv2\nimport PIL\nfrom PIL import Image\nimport numpy as np\n\nimport matplotlib.pyplot as plt\nimport pandas as pd\n\nimport scipy\nfrom tqdm import tqdm\n%matplotlib inline\nfrom keras.preprocessing import image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission = pd.read_csv(\"/kaggle/input/siim-isic-melanoma-classification/sample_submission.csv\")\ntest = pd.read_csv(\"/kaggle/input/siim-isic-melanoma-classification/test.csv\")\ntrain = pd.read_csv(\"/kaggle/input/siim-isic-melanoma-classification/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Benign vs Malignant Data","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.hist(train['target'])\nplt.title('Frequency Histogram of Melanoma')\nplt.figure(figsize=(12, 12))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"#def preprocess_image(image_path, desired_size=imSize):\n    #im = Image.open(image_path)\n    #im = im.resize((desired_size, )*2, resample=Image.LANCZOS)\n    \n    #return im","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-input":true},"cell_type":"code","source":"#N = train.shape[0]//3\n#x_train = np.empty((N, imSize, imSize, 3), dtype=np.uint8)\n#for i, image_id in enumerate(tqdm(train['image_name'])):\n    #if i==N:\n        #break\n    #x_train[i, :, :, :] = preprocess_image(\n       # f'../input/siim-isic-melanoma-classification/jpeg/train/{image_id}.jpg'\n    #)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"> ## Taking same number of data from malignant and benign\nI will take less numbers of training images than available training images. It is an experiment","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"x = train['image_name']\ntrain_malignant=train[train['target'] == 1]\ntrain_benign=train[train['target'] == 0]\ntrain_benign=train_benign[0:584]\nimg_size=64","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_malignant.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_benign.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_malignant.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_benign.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Concat Data","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_balanced = pd.concat([train_benign, train_malignant])\ntrain_balanced.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_balanced.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_balanced.shape","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## New Balanced Data","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.hist(train_balanced['target'])\nplt.title('Frequency Histogram of Balanced Melanoma')\nplt.figure(figsize=(12, 12))\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_image=[]\nfor i,name in enumerate(tqdm(train_balanced['image_name'])):\n    path='/kaggle/input/siim-isic-melanoma-classification/jpeg/train/'+name+'.jpg'\n    img=cv2.imread(path)\n    image=cv2.resize(img,(img_size,img_size),interpolation=cv2.INTER_AREA)\n    train_image.append(image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(1, 4, figsize=(15, 15))\nfor i in range(4):\n    ax[i].set_axis_off()\n    ax[i].imshow(train_image[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_image=[]\nfor i,name in enumerate(tqdm(test['image_name'])):\n    path='/kaggle/input/siim-isic-melanoma-classification/jpeg/test/'+name+'.jpg'\n    img=cv2.imread(path)\n    image=cv2.resize(img,(img_size,img_size),interpolation=cv2.INTER_AREA)\n    test_image.append(image)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(1, 4, figsize=(15, 15))\nfor i in range(4):\n    ax[i].set_axis_off()\n    ax[i].imshow(test_image[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_Train = np.ndarray(shape=(len(train_image), img_size, img_size, 3),dtype = np.float32)\ni=0\nfor image in train_image:\n    #X_Train[i]=img_to_array(image)\n    X_Train[i]=train_image[i]\n    i=i+1\nX_Train=X_Train/255\nprint('Train Shape: {}'.format(X_Train.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_Test = np.ndarray(shape=(len(test_image), img_size, img_size, 3),dtype = np.float32)\ni=0\nfor image in test_image:\n    #X_Test[i]=img_to_array(image)\n    X_Test[i]=test_image[i]\n    i=i+1\n    \nX_Test=X_Test/255\nprint('Test Shape: {}'.format(X_Test.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y = train_balanced['target']\ny.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.utils.np_utils import to_categorical\ny_train = np.array(y.values)\ny_train = to_categorical(y_train, num_classes=2)\nprint(y_train.shape,y_train[1100])\nprint(y_train[3])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"EPOCHS = 80\nSIZE=64\nN_ch=3\nBATCH_SIZE = 64","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## DenseNet121 Model","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def build_densenet():\n    densenet = DenseNet121(weights='imagenet', include_top=False)\n\n    input = Input(shape=(SIZE, SIZE, N_ch))\n    x = Conv2D(3, (3, 3), padding='same')(input)\n    \n    x = densenet(x)\n    \n    x = GlobalAveragePooling2D()(x)\n    x = BatchNormalization()(x)\n    x = Dropout(0.5)(x)\n    x = Dense(256, activation='relu')(x)\n    x = BatchNormalization()(x)\n    x = Dropout(0.5)(x)\n\n    # multi output\n    output = Dense(2,activation = 'softmax', name='root')(x)\n \n\n    # model\n    model = Model(input,output)\n    \n    optimizer = Adam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=0.1, decay=0.0)\n    model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])\n    model.summary()\n    \n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, X_val, Y_train, Y_val = train_test_split(X_Train, y_train, test_size=0.2, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Augmentation and Model Fitting","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"model = build_densenet()\nannealer = ReduceLROnPlateau(monitor='val_accuracy', factor=0.5, patience=5, verbose=1, min_lr=1e-3)\ncheckpoint = ModelCheckpoint('model.h5', verbose=1, save_best_only=True)\n# Generates batches of image data with data augmentation\ndatagen = ImageDataGenerator(rotation_range=360, # Degree range for random rotations\n                        width_shift_range=0.2, # Range for random horizontal shifts\n                        height_shift_range=0.2, # Range for random vertical shifts\n                        zoom_range=0.2, # Range for random zoom\n                        horizontal_flip=True, # Randomly flip inputs horizontally\n                        vertical_flip=True) # Randomly flip inputs vertically\n\ndatagen.fit(X_train)\n# Fits the model on batches with real-time data augmentation\nhist = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=BATCH_SIZE),\n               steps_per_epoch=X_train.shape[0] // BATCH_SIZE,\n               epochs=EPOCHS,\n               verbose=1,\n               callbacks=[annealer, checkpoint],\n               validation_data=(X_val, Y_val))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"final_loss, final_accuracy = model.evaluate(X_val, Y_val)\nprint('Final Loss: {}, Final Accuracy: {}'.format(final_loss, final_accuracy))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predict = model.predict(X_Test)\nprint(predict)\nresult=[]\ndisease_class=['0','1']\nfor i in range(len(predict)):\n    ind=np.argmax(predict[i])\n    result.append(disease_class[ind])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sample_submission[\"target\"]= result\nsample_submission.to_csv('submission.csv', index=False)\nsample_submission.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## If you like this Notebook, please Upvote. Thanks","execution_count":null}],"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}