{"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 all required Libraries\nfrom sklearn.metrics import roc_curve, roc_auc_score, ConfusionMatrixDisplay\nimport shutil\nimport warnings\nimport re\nimport math\nimport os\nfrom sklearn.model_selection import KFold\nfrom plotly.subplots import make_subplots\nimport plotly.io as pio\nimport plotly.graph_objects as go\nimport plotly.express as px\nimport seaborn as sns\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.gridspec import GridSpec\nimport cv2\nfrom sklearn.metrics import roc_curve,confusion_matrix\nimport glob\nfrom tqdm.notebook import tqdm\nimport seaborn as sns\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom PIL import Image\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport random\nfrom random import seed\nimport math\n\n\n\n\nplt.style.use('fivethirtyeight')\npio.templates.default = 'plotly_dark'\n!pip install -q efficientnet >> /dev/null\nimport tensorflow as tf\nfrom kaggle_datasets import KaggleDatasets\nimport tensorflow.keras.backend as K\nimport efficientnet.tfkeras as efn\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2023-04-08T12:52:51.818869Z","iopub.execute_input":"2023-04-08T12:52:51.819644Z","iopub.status.idle":"2023-04-08T12:53:12.232413Z","shell.execute_reply.started":"2023-04-08T12:52:51.819519Z","shell.execute_reply":"2023-04-08T12:53:12.231330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Global Variables\nglobal_vars = {\n    'DEVICE': 'tpu',\n    'NFOLDS': 5,\n    'BATCH_SIZE': 32,\n    'IMAGE_SIZE': 512,\n    'EXTERNAL_DATA': True,\n     'AUTO':tf.data.AUTOTUNE,\n    'LR_MIN':0.00001,\n    'LR_MAX':0.00005,\n    'STEP_SIZE':5,\n    'LR_METHOD':'triangular',\n    'EPOCHS':12\n}","metadata":{"execution":{"iopub.execute_input":"2023-03-25T09:45:37.842430Z","iopub.status.busy":"2023-03-25T09:45:37.841638Z","iopub.status.idle":"2023-03-25T09:45:37.849969Z","shell.execute_reply":"2023-03-25T09:45:37.849183Z","shell.execute_reply.started":"2023-03-25T09:45:37.842394Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set up TPU\n# Locate tpu\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n# connect\ntf.config.experimental_connect_to_cluster(tpu)\n# initialize\ntf.tpu.experimental.initialize_tpu_system(tpu)\n# TPU Strategy\nstrategy = tf.distribute.TPUStrategy()\nreplicas = strategy.num_replicas_in_sync\nprint(\"Num of Replicas: \", replicas)","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:50:28.016803Z","iopub.status.busy":"2022-04-23T07:50:28.016477Z","iopub.status.idle":"2022-04-23T07:50:33.930335Z","shell.execute_reply":"2022-04-23T07:50:33.929277Z","shell.execute_reply.started":"2022-04-23T07:50:28.016769Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"isic2019=pd.read_csv('../input/isic2019-512x512/train.csv')\nmelanoma=pd.read_csv('../input/melanoma-512x512/train.csv')\ntrain=pd.concat((melanoma.drop('patient_code',axis=1),isic2019)).reset_index(drop=True)\ntest = pd.read_csv('../input/siim-isic-melanoma-classification/test.csv')\ncols = test.columns\ndf = pd.concat([train[cols], test[cols]],\n               ignore_index=True).reset_index(drop=True)\ndf.head()","metadata":{"execution":{"iopub.execute_input":"2023-03-25T09:45:45.205244Z","iopub.status.busy":"2023-03-25T09:45:45.204972Z","iopub.status.idle":"2023-03-25T09:45:45.359751Z","shell.execute_reply":"2023-03-25T09:45:45.358896Z","shell.execute_reply.started":"2023-03-25T09:45:45.205213Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"GCS_PATH = KaggleDatasets().get_gcs_path('melanoma-512x512')\n\ntrain_files=np.sort(tf.io.gfile.glob(GCS_PATH+'/train*.tfrec'))\ntest_files=np.sort(tf.io.gfile.glob(GCS_PATH+'/test*.tfrec'))\n\nif global_vars['EXTERNAL_DATA']:\n    GCS_PATH2=KaggleDatasets().get_gcs_path('isic2019-512x512')\n","metadata":{"execution":{"iopub.execute_input":"2023-03-14T18:13:20.333117Z","iopub.status.busy":"2023-03-14T18:13:20.332677Z","iopub.status.idle":"2023-03-14T18:13:21.211500Z","shell.execute_reply":"2023-03-14T18:13:21.210553Z","shell.execute_reply.started":"2023-03-14T18:13:20.333079Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"GCS_TRAIN = KaggleDatasets().get_gcs_path('siim-isic-melanoma-classification')\nGCS_TRAIN=  GCS_TRAIN+'/jpeg/train'\nGCS_HAIRS=KaggleDatasets().get_gcs_path('melanoma-hairs')\n\nhair_images=os.listdir('../input/melanoma-hairs')\nhair_images=[GCS_HAIRS+'/'+image_name for image_name in hair_images]\nhair_images=tf.convert_to_tensor(hair_images)","metadata":{"execution":{"iopub.execute_input":"2023-03-14T18:34:51.501519Z","iopub.status.busy":"2023-03-14T18:34:51.501139Z","iopub.status.idle":"2023-03-14T18:34:51.506546Z","shell.execute_reply":"2023-03-14T18:34:51.505094Z","shell.execute_reply.started":"2023-03-14T18:34:51.501473Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:50:36.034955Z","iopub.status.busy":"2022-04-23T07:50:36.034597Z","iopub.status.idle":"2022-04-23T07:50:36.088753Z","shell.execute_reply":"2022-04-23T07:50:36.088117Z","shell.execute_reply.started":"2022-04-23T07:50:36.034914Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create Figure\nfig=plt.figure(constrained_layout=True, figsize=(20, 10))\ncolor = '#8B0000'\n# Create grid:\ngrid = GridSpec(ncols=4, nrows=2, figure=fig)\n\n# Plot Gender Distribution  on first grid\nax1 = fig.add_subplot(grid[0, :2])\n\nax1.set_title('Gender Distribution')\n\nsns.countplot(\n    df.sex.sort_values(ignore_index=True),\n    alpha=0.9,\n    ax=ax1,\n    color=color,\n)\n\n# Plot Anatom Site General Challenge Distribution on second grid.\nax2 = fig.add_subplot(grid[0, 2:])\n\nsns.countplot(df['anatom_site_general_challenge'],\n              alpha=0.9,\n              ax=ax2,\n              color=color,\n              order=df['anatom_site_general_challenge'].value_counts().index)\nax2.set_title('Anatom Site Challenge Distribution')\nplt.xticks(rotation=20)\n\n# Plot Distribution of Age on third grid.\n\nax3 = fig.add_subplot(grid[1, :])\nsns.distplot(df['age_approx'], color=color, ax=ax3)\n\nax3.set_title('Age Distribution')\n\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2022-04-23T07:50:36.090241Z","iopub.status.busy":"2022-04-23T07:50:36.089916Z","iopub.status.idle":"2022-04-23T07:50:37.624223Z","shell.execute_reply":"2022-04-23T07:50:37.623066Z","shell.execute_reply.started":"2022-04-23T07:50:36.090215Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Observations:-**\n1. Men and Female have almost same counts.\n2. In Anatom Site Challenge there are different types of torso.\n3. Age follows Normal Distribution.","metadata":{}},{"cell_type":"code","source":"# Donut Chart of Target\nfig = go.Figure()\nfig.add_trace(\n    go.Pie(labels=train['benign_malignant'].value_counts().index,\n           values=train['benign_malignant'].value_counts().values,\n           hole=0.4,\n           marker_colors=['#008B8B', '#FF7F50']))\nfig.update_layout(title_text='Donut Chart of Target',\n                  title_font_size=30,\n                  annotations=[\n                      dict(x=0.49,\n                           y=0.5,\n                           text='Target',\n                           font_size=20,\n                           showarrow=False)\n                  ])\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2022-04-23T07:50:37.625712Z","iopub.status.busy":"2022-04-23T07:50:37.625462Z","iopub.status.idle":"2022-04-23T07:50:37.752388Z","shell.execute_reply":"2022-04-23T07:50:37.751537Z","shell.execute_reply.started":"2022-04-23T07:50:37.625670Z"}},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**There is Class Imbalance**","metadata":{}},{"cell_type":"code","source":"print(\"No of rows where age is zero =\", len(df[df['age_approx'] == 0]))","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:50:37.753801Z","iopub.status.busy":"2022-04-23T07:50:37.753502Z","iopub.status.idle":"2022-04-23T07:50:37.759967Z","shell.execute_reply":"2022-04-23T07:50:37.759217Z","shell.execute_reply.started":"2022-04-23T07:50:37.753773Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Age distribution by target and sex\ncolor1 = '#008B8B'\ncolor2 = '#DC143C'\ngrid = GridSpec(nrows=1, ncols=4)\nfigure = plt.figure(figsize=(15, 9), constrained_layout=True)\n\nax1 = figure.add_subplot(grid[0, :2])\nb, m = train.loc[train['target'] == 0,\n                 'age_approx'], train.loc[train['target'] == 1, 'age_approx']\nsns.distplot(b, ax=ax1, color=color1, label='Benign')\nsns.distplot(m, ax=ax1, color=color2, label='Malignant')\nax1.set_title('Age Distribution by Target')\nax1.legend()\n\nax2 = figure.add_subplot(grid[0, 2:])\nm, f = df.loc[df['sex'] == 'male', 'age_approx'], df.loc[df['sex'] == 'female',\n                                                         'age_approx']\nsns.distplot(f, ax=ax2, color=color1, label='Female')\nsns.distplot(m, ax=ax2, color=color2, label='Male')\nax2.set_title('Age Distribution by sex')\nax2.legend()\n\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2022-04-23T07:50:37.761515Z","iopub.status.busy":"2022-04-23T07:50:37.761289Z","iopub.status.idle":"2022-04-23T07:50:39.558376Z","shell.execute_reply":"2022-04-23T07:50:39.557543Z","shell.execute_reply.started":"2022-04-23T07:50:37.761487Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Sunburst Chart\ncolors = ['#a2ef44', '#31aa75', '#fcd47d', '#b23256']\nfig = px.sunburst(data_frame=train.dropna(),\n                  path=['target', 'sex', 'anatom_site_general_challenge'],\n                  color='sex',\n                  color_discrete_sequence=colors,\n                  maxdepth=-1,\n                  title='Sunburst Chart Benign/Malignant > Sex > Location')\n\nfig.update_traces(textinfo='label+percent parent')\nfig.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2022-04-23T07:50:39.562315Z","iopub.status.busy":"2022-04-23T07:50:39.561436Z","iopub.status.idle":"2022-04-23T07:50:40.941039Z","shell.execute_reply":"2022-04-23T07:50:40.940156Z","shell.execute_reply.started":"2022-04-23T07:50:39.562262Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pivot Tables\nprint(\n    'Pivot Table where index=anatom site challenge, columns=sex, values=target, aggfunc=count\\n'\n)\nprint(\n    pd.pivot_table(data=train,\n                   index='anatom_site_general_challenge',\n                   columns='sex',\n                   values='target',\n                   aggfunc='count',\n                   fill_value=0))\nprint('-' * 80)\nprint(\n    'Pivot Table where index=anatom site challenge, columns=[sex,target], values=age_approx, aggfunc=mean\\n'\n)\nprint(\n    pd.pivot_table(data=train,\n                   index='anatom_site_general_challenge',\n                   columns=['sex', 'target'],\n                   values='age_approx',\n                   fill_value=0))","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:50:40.942432Z","iopub.status.busy":"2022-04-23T07:50:40.942185Z","iopub.status.idle":"2022-04-23T07:50:41.017711Z","shell.execute_reply":"2022-04-23T07:50:41.017099Z","shell.execute_reply.started":"2022-04-23T07:50:40.942404Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = set(test['patient_id'].unique()).intersection(\n    set(train['patient_id'].unique()))\nif len(a) == 0:\n    print(\"There is no patient in test set that was present in train set.\")\nelse:\n    print(\"There are some patients in test which were present in train set.\")","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:50:41.019267Z","iopub.status.busy":"2022-04-23T07:50:41.018886Z","iopub.status.idle":"2022-04-23T07:50:41.030799Z","shell.execute_reply":"2022-04-23T07:50:41.029868Z","shell.execute_reply.started":"2022-04-23T07:50:41.019230Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot some benign and Malignant Images\ndf=pd.read_csv('../input/siim-isic-melanoma-classification/train.csv')\nbenign_images=df.loc[df.target==0,'image_name']\nmalignant_images=df.loc[df.target==1,'image_name']\nrandom_benign_images=[np.random.choice(benign_images)+'.jpg' for i in range(9)]\nrandom_malignant_images=[np.random.choice(malignant_images)+'.jpg' for i in range(9)]\nfigure=plt.figure(figsize=(20,10),tight_layout=True)\nfolder_path='../input/siim-isic-melanoma-classification/jpeg/train'\nprint('Benign Images')\nfor i in range(9):\n    figure.add_subplot(3,3,i+1)\n    image=plt.imread(os.path.join(folder_path,random_benign_images[i]))\n    plt.imshow(image)\n    plt.axis('off')\nplt.show()\n\n    \n    ","metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2022-04-23T07:50:41.032363Z","iopub.status.busy":"2022-04-23T07:50:41.032130Z","iopub.status.idle":"2022-04-23T07:50:51.928418Z","shell.execute_reply":"2022-04-23T07:50:51.927283Z","shell.execute_reply.started":"2022-04-23T07:50:41.032338Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure=plt.figure(figsize=(20,10),tight_layout=True)\nprint('Malignant Images')\nfor i in range(9):\n    figure.add_subplot(3,3,i+1)\n    image=plt.imread(os.path.join(folder_path,random_malignant_images[i]))\n    plt.imshow(image)\n    plt.axis('off')\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2022-04-23T07:50:51.930144Z","iopub.status.busy":"2022-04-23T07:50:51.929911Z","iopub.status.idle":"2022-04-23T07:50:57.997668Z","shell.execute_reply":"2022-04-23T07:50:57.996705Z","shell.execute_reply.started":"2022-04-23T07:50:51.930117Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = plt.figure(figsize=(16,8))\nfig.add_subplot(1,2, 1)\nbenign=train.loc[train.target==0]\nsample_img = benign['image_name'][0]+'.jpg'\nfolder_path='../input/siim-isic-melanoma-classification/jpeg/train'\nraw_image = plt.imread(os.path.join(folder_path, sample_img))\nplt.imshow(raw_image, cmap='gray')\nplt.colorbar()\nplt.title('Benign Image')\nprint(f\"Image dimensions:  {raw_image.shape[0],raw_image.shape[1]}\")\nprint(f\"Maximum pixel value : {raw_image.max():.1f} ; Minimum pixel value:{raw_image.min():.1f}\")\nprint(f\"Mean value of the pixels : {raw_image.mean():.1f} ; Standard deviation : {raw_image.std():.1f}\")\n\nfig.add_subplot(1,2, 2)\n\n#_ = plt.hist(raw_image.ravel(),bins = 256, color = 'orange')\n_ = plt.hist(raw_image[:, :, 0].ravel(), bins = 256, color = 'red', alpha = 0.5)\n_ = plt.hist(raw_image[:, :, 1].ravel(), bins = 256, color = 'Green', alpha = 0.5)\n_ = plt.hist(raw_image[:, :, 2].ravel(), bins = 256, color = 'Blue', alpha = 0.5)\n_ = plt.xlabel('Intensity Value')\n_ = plt.ylabel('Count')\n_ = plt.legend(['Red_Channel', 'Green_Channel', 'Blue_Channel'])\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2022-04-23T07:50:57.999320Z","iopub.status.busy":"2022-04-23T07:50:57.999056Z","iopub.status.idle":"2022-04-23T07:51:04.620426Z","shell.execute_reply":"2022-04-23T07:51:04.619555Z","shell.execute_reply.started":"2022-04-23T07:50:57.999289Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f = plt.figure(figsize=(16,8))\nf.add_subplot(1,2, 1)\nmalignant=train.loc[train.target==1].reset_index(drop=True)\nsample_img = malignant['image_name'][0]+'.jpg'\nraw_image = plt.imread(os.path.join(folder_path, sample_img))\nplt.imshow(raw_image, cmap='gray')\nplt.colorbar()\nplt.title('Malignant Image')\nprint(f\"Image dimensions:  {raw_image.shape[0],raw_image.shape[1]}\")\nprint(f\"Maximum pixel value : {raw_image.max():.1f} ; Minimum pixel value:{raw_image.min():.1f}\")\nprint(f\"Mean value of the pixels : {raw_image.mean():.1f} ; Standard deviation : {raw_image.std():.1f}\")\n\nf.add_subplot(1,2, 2)\n\n#_ = plt.hist(raw_image.ravel(),bins = 256, color = 'orange',)\n_ = plt.hist(raw_image[:, :, 0].ravel(), bins = 256, color = 'red', alpha = 0.5)\n_ = plt.hist(raw_image[:, :, 1].ravel(), bins = 256, color = 'Green', alpha = 0.5)\n_ = plt.hist(raw_image[:, :, 2].ravel(), bins = 256, color = 'Blue', alpha = 0.5)\n_ = plt.xlabel('Intensity Value')\n_ = plt.ylabel('Count')\n_ = plt.legend(['Red_Channel', 'Green_Channel', 'Blue_Channel'])\nplt.show()","metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2022-04-23T07:51:04.622362Z","iopub.status.busy":"2022-04-23T07:51:04.621670Z","iopub.status.idle":"2022-04-23T07:51:11.528839Z","shell.execute_reply":"2022-04-23T07:51:11.527873Z","shell.execute_reply.started":"2022-04-23T07:51:04.622326Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_rotation_matrix(rotation):\n    # Convert  degrees to radians\n    rotation=math.pi*rotation/180.\n    \n    # Sine,Cosine,one,zero\n    sin=tf.math.sin(rotation)\n    cos=tf.math.cos(rotation)\n    one=tf.constant(1,dtype=tf.float32)\n    zero=tf.constant(0,dtype=tf.float32)\n    \n    # Rotation Matrix\n    rotation_matrix=tf.reshape(tf.concat([[cos,-sin,zero,sin,cos,zero,zero,zero,one]],axis=0),(3,3))\n    return rotation_matrix\ndef Transform(image,rotation):\n    DIM=image.shape[1]\n    XDIM=DIM%2\n    # List Destination Pixel indices\n    x=tf.repeat(tf.range(DIM//2,-DIM//2,-1),DIM)\n    y=tf.tile(tf.range(-DIM//2,DIM//2),[DIM])\n    z=tf.ones([DIM*DIM],dtype=tf.int32)\n    idx=tf.stack([x,y,z])\n    # Rotate destination pixels\n    m=get_rotation_matrix(rotation)\n    m=K.dot(m,tf.cast(idx,tf.float32))\n    m=tf.cast(m,tf.int32)\n    m=K.clip(m,-DIM//2+XDIM+1,DIM//2)\n    \n    # Find Original Pixels\n    idx3=tf.stack([DIM//2-m[0,],DIM//2-1+m[1,]])\n    image=tf.gather_nd(image,tf.transpose(idx3))\n    return tf.reshape(image,(DIM,DIM,3))\n    ","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:11.530426Z","iopub.status.busy":"2022-04-23T07:51:11.530158Z","iopub.status.idle":"2022-04-23T07:51:11.542898Z","shell.execute_reply":"2022-04-23T07:51:11.541809Z","shell.execute_reply.started":"2022-04-23T07:51:11.530388Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a='../input/siim-isic-melanoma-classification/jpeg/train/ISIC_0015719.jpg'\nb='../input/siim-isic-melanoma-classification/jpeg/train/ISIC_0052212.jpg'\na=cv2.imread(a)\na=cv2.resize(a,(512,512))\nb=cv2.imread(b)\nb=cv2.resize(b,(512,512))\nimg=Transform(a,50)\nplt.figure(figsize=(10,6))\nax=plt.subplot(1,2,1)\nax.imshow(a)\nax.set_title('Original Image')\n\nax1=plt.subplot(1,2,2)\nax1.imshow(img)\nax1.set_title('Rotated Image')","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:11.544745Z","iopub.status.busy":"2022-04-23T07:51:11.544316Z","iopub.status.idle":"2022-04-23T07:51:12.627439Z","shell.execute_reply":"2022-04-23T07:51:12.626570Z","shell.execute_reply.started":"2022-04-23T07:51:11.544674Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image=plt.imread('../input/siim-isic-melanoma-classification/jpeg/train/ISIC_0075663.jpg')\nhue_image=tf.image.adjust_hue(image,delta=0.1)\nplt.figure(figsize=(15,9))\nax=plt.subplot(1,2,1)\nax.set_title('Original Image')\nax.imshow(image)\nax1=plt.subplot(1,2,2)\nax1.set_title('Image with random Hue')\nax1.imshow(hue_image)","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:12.629282Z","iopub.status.busy":"2022-04-23T07:51:12.628650Z","iopub.status.idle":"2022-04-23T07:51:14.834713Z","shell.execute_reply":"2022-04-23T07:51:14.833878Z","shell.execute_reply.started":"2022-04-23T07:51:12.629230Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"brightness_image=tf.image.adjust_brightness(image,delta=0.3)\nplt.figure(figsize=(15,9))\nax=plt.subplot(1,2,1)\nax.set_title('Original Image')\nax.imshow(image)\nax1=plt.subplot(1,2,2)\nax1.set_title('Image with random Brightness')\nax1.imshow(brightness_image)","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:14.836843Z","iopub.status.busy":"2022-04-23T07:51:14.835972Z","iopub.status.idle":"2022-04-23T07:51:16.865548Z","shell.execute_reply":"2022-04-23T07:51:16.864619Z","shell.execute_reply.started":"2022-04-23T07:51:14.836792Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"saturation_image=tf.image.adjust_saturation(image,saturation_factor=1.5)\nplt.figure(figsize=(15,9))\nax=plt.subplot(1,2,1)\nax.set_title('Original Image')\nax.imshow(image)\nax1=plt.subplot(1,2,2)\nax1.set_title('Image with random Saturation')\nax1.imshow(saturation_image)","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:16.869219Z","iopub.status.busy":"2022-04-23T07:51:16.868995Z","iopub.status.idle":"2022-04-23T07:51:18.938633Z","shell.execute_reply":"2022-04-23T07:51:18.937783Z","shell.execute_reply.started":"2022-04-23T07:51:16.869193Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"contrast_image=tf.image.adjust_contrast(image,contrast_factor=1.0)\nplt.figure(figsize=(15,9))\nax=plt.subplot(1,2,1)\nax.set_title('Original Image')\nax.imshow(image)\nax1=plt.subplot(1,2,2)\nax1.set_title('Image with random Constrast')\nax1.imshow(contrast_image)","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:18.940117Z","iopub.status.busy":"2022-04-23T07:51:18.939910Z","iopub.status.idle":"2022-04-23T07:51:20.920609Z","shell.execute_reply":"2022-04-23T07:51:20.919662Z","shell.execute_reply.started":"2022-04-23T07:51:18.940093Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cutout(input_img,p=0.5, s_l=0.02, s_h=0.4, r_1=0.3, r_2=1/0.3, v_l=0, v_h=255, pixel_level=False):\n   \n    img_h, img_w, img_c = input_img.shape\n    while True:\n        s = np.random.uniform(s_l, s_h) * img_h * img_w\n        r = np.random.uniform(r_1, r_2)\n        w = int(np.sqrt(s / r))\n        h = int(np.sqrt(s * r))\n        left = np.random.randint(0, img_w)\n        top = np.random.randint(0, img_h)\n\n        if left + w <= img_w and top + h <= img_h:\n            break\n\n   \n    c = np.random.uniform(v_l, v_h)\n\n    input_img[top:top + h, left:left + w, :] = c\n\n    return input_img","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:20.922116Z","iopub.status.busy":"2022-04-23T07:51:20.921873Z","iopub.status.idle":"2022-04-23T07:51:20.931284Z","shell.execute_reply":"2022-04-23T07:51:20.930241Z","shell.execute_reply.started":"2022-04-23T07:51:20.922089Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a='../input/siim-isic-melanoma-classification/jpeg/train/ISIC_0015719.jpg'\nb='../input/siim-isic-melanoma-classification/jpeg/train/ISIC_0052212.jpg'\na=cv2.imread(a)\na=cv2.resize(a,(512,512))\nb=cv2.imread(b)\nb=cv2.resize(b,(512,512))\nplt.figure(figsize=(10,6))\nax=plt.subplot(1,2,1)\nax.imshow(a)\nax.set_title('Original Image')\n\nimg=cutout(a)\nax1=plt.subplot(1,2,2)\nax1.imshow(img)\nax1.set_title('Cutout Image')","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:20.933020Z","iopub.status.busy":"2022-04-23T07:51:20.932717Z","iopub.status.idle":"2022-04-23T07:51:21.831715Z","shell.execute_reply":"2022-04-23T07:51:21.830793Z","shell.execute_reply.started":"2022-04-23T07:51:20.932981Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a='../input/siim-isic-melanoma-classification/jpeg/train/ISIC_0015719.jpg'\nb='../input/siim-isic-melanoma-classification/jpeg/train/ISIC_0052212.jpg'\na=cv2.imread(a)\na=cv2.resize(a,(512,512))\nb=cv2.imread(b)\nb=cv2.resize(b,(512,512))\nDIM=512\nc=tf.random.uniform([],0,1)\nimg=tf.cast(a*c+(1-c)*b,tf.int32)\n\nplt.figure(figsize=(10,6))\nax=plt.subplot(1,3,1)\nax.imshow(a)\nax.set_title('Original Image A')\n\nax1=plt.subplot(1,3,2)\nax1.imshow(b)\nax1.set_title('Original Image B')\n\n\nax2=plt.subplot(1,3,3)\nax2.imshow(img)\nax2.set_title('Image A Mixup with B')","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:21.833028Z","iopub.status.busy":"2022-04-23T07:51:21.832811Z","iopub.status.idle":"2022-04-23T07:51:22.867732Z","shell.execute_reply":"2022-04-23T07:51:22.866848Z","shell.execute_reply.started":"2022-04-23T07:51:21.833004Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a='../input/siim-isic-melanoma-classification/jpeg/train/ISIC_0015719.jpg'\nb='../input/siim-isic-melanoma-classification/jpeg/train/ISIC_0052212.jpg'\na=cv2.imread(a)\na=cv2.resize(a,(512,512))\nb=cv2.imread(b)\nb=cv2.resize(b,(512,512))\nDIM=512\nx=tf.cast(tf.random.uniform([],0,DIM),tf.int32)\ny=tf.cast(tf.random.uniform([],0,DIM),tf.int32)\nc=tf.random.uniform([],0,1)\nWIDTH=tf.cast(DIM*tf.math.sqrt(c),tf.int32)\nya = tf.math.maximum(0,y-WIDTH//2)\nyb = tf.math.minimum(DIM,y+WIDTH//2)\nxa = tf.math.maximum(0,x-WIDTH//2)\nxb = tf.math.minimum(DIM,x+WIDTH//2)\none=a[ya:yb,0:xa,:]\ntwo=b[ya:yb,xa:xb,:]\nthree=a[ya:yb,xb:,:]\nimg=tf.concat((one,two,three),axis=1)\nimg=tf.concat((a[0:ya,:,:],img,a[yb:DIM,:,:]),axis=0)\n\n\nplt.figure(figsize=(10,6))\nax=plt.subplot(1,3,1)\nax.imshow(a)\nax.set_title('Original Image A')\n\nax1=plt.subplot(1,3,2)\nax1.imshow(b)\nax1.set_title('Original Image B')\n\nax2=plt.subplot(1,3,3)\nax2.imshow(img)\nax2.set_title('Image A CutMix with B')","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:22.869729Z","iopub.status.busy":"2022-04-23T07:51:22.869029Z","iopub.status.idle":"2022-04-23T07:51:23.878716Z","shell.execute_reply":"2022-04-23T07:51:23.877865Z","shell.execute_reply.started":"2022-04-23T07:51:22.869668Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def hair_aug(input_image,label):\n    # Unnormalize: Returning the image from 0-1 to 0-255:\n    image=tf.identity(input_image)\n    image=tf.multiply(image,255)\n\n    # Select Random Number of Hairs to Augment\n    random_number=tf.random.uniform([],0,maxval=21,dtype=tf.int32)\n    scale=tf.cast(500/256,tf.int32)\n    for i in range(random_number):\n        # Randomly select a image to augment\n        hair_image_name=hair_images[tf.random.uniform([],0,maxval=tf.shape(hair_images)[0],dtype=tf.int32)]\n        hair=tf.io.read_file(hair_image_name)\n        hair=tf.image.decode_jpeg(hair)\n\n        # Resize hair to new_width,new_scale\n        new_height=scale*tf.shape(hair)[0]\n        new_width=scale*tf.shape(hair)[1]\n        hair=tf.image.resize(hair,[new_height,new_width])\n\n        # Perform Augmentation on hair\n        hair = tf.image.random_flip_left_right(hair)\n        hair = tf.image.random_flip_up_down(hair)\n        n_rot = tf.random.uniform(shape=[], maxval=4,dtype=tf.int32)\n        hair = tf.image.rot90(hair, k=n_rot)\n        new_height,new_width=tf.shape(hair)[0],tf.shape(hair)[1]\n\n        # Top Left Coordinates \n        left_h=tf.random.uniform([],0,512-new_height+1,dtype=tf.int32)\n        left_w=tf.random.uniform([],0,512-new_width+1,dtype=tf.int32)\n\n        # Select Region of Interest\n        roi=image[left_h:left_h+new_height,left_w:left_w+new_width]\n\n        # Convert the hair image to grayscale (slice to remove the trainsparency channel)\n        hair2gray = tf.image.rgb_to_grayscale(hair[:, :, :3])\n\n        # Threshold:\n        mask = hair2gray>10\n\n        # Get Image Background and just hair of hair image and add both.\n        image_bg=tf.multiply(roi,tf.cast(tf.image.grayscale_to_rgb(~mask),dtype=tf.float32))\n        hair_fg=tf.multiply(tf.cast(hair[:,:,:3],dtype=tf.int32),tf.cast(tf.image.grayscale_to_rgb(mask),dtype=tf.int32))\n        dst=tf.add(image_bg,tf.cast(hair_fg,tf.float32))\n\n        # Generate paddings\n        paddings=tf.stack([[left_h,512-(left_h+new_height)],[left_w,512-(left_w+new_width)],[0,0]])\n        dst_padded=tf.pad(dst,paddings)\n\n        # Create a boolean mask with zeros at the pixels of the augmentation segment and ones everywhere else\n        mask_img=tf.pad(tf.ones_like(dst),paddings)\n        mask_img=~tf.cast(mask_img,dtype=tf.bool)\n\n        # Make a hole in the original image at the location of the augmentation segment\n        hole=tf.multiply(image,tf.cast(mask_img,dtype=tf.float32))\n\n        # Add hole and dst_padded\n        image=tf.add(hole,dst_padded)\n\n    # Normalize image\n    img = tf.multiply(image, 1/255)\n    return img,label","metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2022-04-23T07:51:23.880280Z","iopub.status.busy":"2022-04-23T07:51:23.880045Z","iopub.status.idle":"2022-04-23T07:51:23.898653Z","shell.execute_reply":"2022-04-23T07:51:23.897772Z","shell.execute_reply.started":"2022-04-23T07:51:23.880252Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path=tf.io.gfile.glob(GCS_TRAIN+'/*.jpg')[0]\nimage=tf.io.read_file(image_path)\nimage=tf.image.decode_jpeg(image)\nimage=tf.image.resize(image,(512,512))\n\nimg,_=hair_aug(image,0)\n\n\n\nfig=plt.figure(figsize=(10,6))\nax1=fig.add_subplot(1,2,1)\nax1.imshow(tf.cast(image,tf.int32))\n\nax2=fig.add_subplot(1,2,2)\nax2.imshow(tf.cast(img,tf.int32))","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:23.900575Z","iopub.status.busy":"2022-04-23T07:51:23.900242Z","iopub.status.idle":"2022-04-23T07:51:30.290639Z","shell.execute_reply":"2022-04-23T07:51:30.289997Z","shell.execute_reply.started":"2022-04-23T07:51:23.900534Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv('../input/siim-isic-melanoma-classification/train.csv')\nimage_names=list(df['image_name'])\nimage_names=[os.path.join(GCS_TRAIN,image_name+'.jpg') for image_name in image_names]\nimage_names=tf.convert_to_tensor(image_names)\ntargets=list(df['target'])\ntargets=tf.convert_to_tensor(targets)","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:30.292496Z","iopub.status.busy":"2022-04-23T07:51:30.291634Z","iopub.status.idle":"2022-04-23T07:51:30.463077Z","shell.execute_reply":"2022-04-23T07:51:30.462291Z","shell.execute_reply.started":"2022-04-23T07:51:30.292435Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_random_sample():\n    idx=tf.random.uniform([],0,len(df),dtype=tf.int32)\n    image_name=image_names[idx]\n    target=targets[idx]\n    image=tf.io.read_file(image_name)\n    image=tf.image.decode_jpeg(image)\n    image=tf.image.resize(image,(global_vars['IMAGE_SIZE'],global_vars['IMAGE_SIZE']))\n    image=tf.cast(image/255.0,dtype=tf.float32)\n    target=tf.cast(target,dtype=tf.int64)\n    return image,target\n","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:30.464991Z","iopub.status.busy":"2022-04-23T07:51:30.464308Z","iopub.status.idle":"2022-04-23T07:51:30.472135Z","shell.execute_reply":"2022-04-23T07:51:30.471290Z","shell.execute_reply.started":"2022-04-23T07:51:30.464940Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Augmentation functions\n\ndef get_transformation_matrix(rotation, shear, height_zoom, width_zoom, height_shift, width_shift):\n    # Convert degree to radians\n    rotation = math.pi * rotation / 180.\n    shear = math.pi * shear / 180.\n    \n    # ROTATION MATRIX\n    c1 = tf.math.cos(rotation)\n    s1 = tf.math.sin(rotation)\n    one = tf.constant([1],dtype='float32')\n    zero = tf.constant([0],dtype='float32')\n    rotation_matrix = tf.reshape( tf.concat([c1,s1,zero, -s1,c1,zero, zero,zero,one],axis=0),[3,3] )\n        \n    # SHEAR MATRIX\n    c2 = tf.math.cos(shear)\n    s2 = tf.math.sin(shear)\n    shear_matrix = tf.reshape( tf.concat([one,s2,zero, zero,c2,zero, zero,zero,one],axis=0),[3,3] )    \n    \n    # ZOOM MATRIX\n    zoom_matrix = tf.reshape( tf.concat([one/height_zoom,zero,zero, zero,one/width_zoom,zero, zero,zero,one],axis=0),[3,3] )\n    \n    # SHIFT MATRIX\n    shift_matrix = tf.reshape( tf.concat([one,zero,height_shift, zero,one,width_shift, zero,zero,one],axis=0),[3,3] )\n    \n    return K.dot(K.dot(rotation_matrix, shear_matrix), K.dot(zoom_matrix, shift_matrix))\n\ndef Spatial_Transformation(image,label,DIM=global_vars['IMAGE_SIZE']):\n    XDIM = DIM%2 \n    rotation = 180. * tf.random.normal([1],dtype='float32')\n    shear = 2. * tf.random.normal([1],dtype='float32') \n    height_zoom = 1.0 + tf.random.normal([1],dtype='float32')/8.\n    width_zoom = 1.0 + tf.random.normal([1],dtype='float32')/8.\n    height_shift = 8. * tf.random.normal([1],dtype='float32') \n    width_shift = 8. * tf.random.normal([1],dtype='float32') \n\n    m=get_transformation_matrix(rotation, shear, height_zoom, width_zoom, height_shift, width_shift)\n    \n    # List Destination Pixels\n    x   = tf.repeat(tf.range(DIM//2, -DIM//2,-1), DIM)\n    y   = tf.tile(tf.range(-DIM//2, DIM//2), [DIM])\n    z   = tf.ones([DIM*DIM], dtype='int32')\n    idx = tf.stack( [x,y,z] )\n    \n    # ROTATE DESTINATION PIXELS ONTO ORIGIN PIXELS\n    idx2 = K.dot(m, tf.cast(idx, dtype='float32'))\n    idx2 = K.cast(idx2, dtype='int32')\n    idx2 = K.clip(idx2, -DIM//2+XDIM+1, DIM//2)\n    \n    # FIND ORIGIN PIXEL VALUES           \n    idx3 = tf.stack([DIM//2-idx2[0,], DIM//2-1+idx2[1,]])\n    d    = tf.gather_nd(image, tf.transpose(idx3))\n        \n    return tf.reshape(d,[DIM, DIM,3]),label\n\ndef Color_Distortion(image,label):\n    image = tf.image.random_flip_left_right(image)\n    image = tf.image.random_hue(image, 0.01)\n    image = tf.image.random_saturation(image, 0.7, 1.3)\n    image = tf.image.random_contrast(image, 0.8, 1.2)\n    image = tf.image.random_brightness(image, 0.1)\n    return image,label\n\ndef Mixup_Augmentation(image,label):\n    # image=>(dim,dim,3)  label=>(1,1)\n    image2,label2=get_random_sample()\n    d=tf.random.uniform([],0,1,seed=42)\n    d1=tf.cast(d,tf.float32)\n    d2=tf.cast(d,tf.int64)\n    image=image*d1+(1-d1)*image2\n    label=label*d2+(1-d2)*label2\n    label=tf.cast(label,tf.int64)\n    return image,label\n                     \ndef Cutout_Augmentation(image,label):\n    p=0.5\n    s_l=0.02\n    s_h=0.4\n    r_1=0.3\n    r_2=1/0.3\n    v_l=0\n    v_h=255\n    img_h,img_w=global_vars['IMAGE_SIZE'],global_vars['IMAGE_SIZE']\n    while True:\n        s = np.random.uniform(s_l, s_h) * img_h * img_w\n        r = np.random.uniform(r_1, r_2)\n        w = int(np.sqrt(s / r))\n        h = int(np.sqrt(s * r))\n        left = np.random.randint(0, img_w)\n        top = np.random.randint(0, img_h)\n\n        if left + w <= img_w and top + h <= img_h:\n            break\n\n    \n    c = np.random.uniform(v_l, v_h)\n    \n    img=tf.reshape(tf.repeat(c,h*w*3),(h,w,3))\n    one=image[top:top+h,0:left]\n    two=img\n    three=image[top:top+h,left+w:global_vars['IMAGE_SIZE']]\n    img=tf.concat([one,two,three],axis=1)\n    \n    return tf.concat((image[0:top,:,:],img,image[top+h:,:,:]),axis=0),label\n    \n    \n\n    \n    \ndef CutMix_Augmentation(image,label):\n    #image=>(dim,dim,3)\n    #label=>(1,1)\n    sample_image,sample_label=get_random_sample()\n    x,y=tf.cast(tf.random.uniform([],0,global_vars['IMAGE_SIZE'],seed=42),tf.int32),tf.cast(tf.random.uniform([],0,global_vars['IMAGE_SIZE'],seed=42),tf.int32)\n    b = tf.random.uniform([],0,1) \n    WIDTH = tf.cast( global_vars['IMAGE_SIZE'] * tf.math.sqrt(1-b),tf.int32)\n    xmin,ymin=tf.math.maximum(0,x-WIDTH//2),tf.math.maximum(0,y-WIDTH//2)\n    xmax,ymax=tf.math.minimum(global_vars['IMAGE_SIZE'],x+WIDTH//2),tf.math.minimum(global_vars['IMAGE_SIZE'],y+WIDTH//2)\n\n    a=image[ymin:ymax,0:xmin,:]\n\n    b=sample_image[ymin:ymax,xmin:xmax,:]\n\n    c=image[ymin:ymax,xmax:,:]\n\n    img=tf.concat((a,b,c),axis=1)\n\n    img=tf.concat([image[0:ymin,:,:],img,image[ymax:global_vars['IMAGE_SIZE'],:,:]],axis=0)\n    a = tf.cast(WIDTH*WIDTH/global_vars['IMAGE_SIZE']/global_vars['IMAGE_SIZE'],tf.int64)\n    label=label*a+(1-a)*sample_label\n    label=tf.cast(label,tf.int64)\n    return img,label\n    \ndef InformationDelection_Augmentation(image,label):\n    \n    random_number=tf.cast(tf.random.uniform([],1,6,seed=42),tf.int64)\n    if random_number==1:\n        return CutMix_Augmentation(image,label)\n    elif random_number==2:\n        return Cutout_Augmentation(image,label)\n    elif random_number==3:\n        return Mixup_Augmentation(image,label)\n    elif random_number==4:\n        return hair_aug(image,label)\n    else:\n        return image,label\n    \n\n    \n    \n    \n    ","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:30.473613Z","iopub.status.busy":"2022-04-23T07:51:30.473405Z","iopub.status.idle":"2022-04-23T07:51:30.515451Z","shell.execute_reply":"2022-04-23T07:51:30.514583Z","shell.execute_reply.started":"2022-04-23T07:51:30.473590Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def InformationDelection_Augmentation(image,label):\n    \n    random_number=tf.cast(tf.random.uniform([],1,4,seed=42),tf.int64)\n    if random_number==1:\n        return Mixup_Augmentation(image,label)\n    elif random_number==2:\n        return hair_aug(image,label)\n    else:\n        return image,label","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:30.517028Z","iopub.status.busy":"2022-04-23T07:51:30.516564Z","iopub.status.idle":"2022-04-23T07:51:30.531053Z","shell.execute_reply":"2022-04-23T07:51:30.530022Z","shell.execute_reply.started":"2022-04-23T07:51:30.516994Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Pipeline\ndef read_labeled_tfrecord(example):\n    '''\n    This Function decodes a labeled tfrecord example.\n    returns image,target\n    '''\n    tfrecord_format={\n        'image'                        : tf.io.FixedLenFeature([], tf.string),\n        'image_name'                   : tf.io.FixedLenFeature([], tf.string),\n        'patient_id'                   : tf.io.FixedLenFeature([], tf.int64),\n        'sex'                          : tf.io.FixedLenFeature([], tf.int64),\n        'age_approx'                   : tf.io.FixedLenFeature([], tf.int64),\n        'anatom_site_general_challenge': tf.io.FixedLenFeature([], tf.int64),\n        'diagnosis'                    : tf.io.FixedLenFeature([], tf.int64),\n        'target'                       : tf.io.FixedLenFeature([], tf.int64)\n    }     \n    example=tf.io.parse_single_example(example,tfrecord_format)\n    return example['image'], example['target']\n\ndef read_unlabeled_tfrecord(example,image_name):\n    '''\n    This Function decodes a unlabeled tfrecord example.\n    returns image,image_name\n    '''\n    tfrecord_format={\n        'image'                        : tf.io.FixedLenFeature([], tf.string),\n        'image_name'                   : tf.io.FixedLenFeature([], tf.string)\n     }\n    example=tf.io.parse_single_example(example,tfrecord_format)\n    return example['image'], example['image_name'] if image_name else 0\ndef prepare_image(image,label,augment=True,dim=256):\n    image=tf.image.decode_jpeg(image,channels=3)\n    image=tf.cast(image,dtype=tf.float32)/255.0\n    if augment:\n        image,label = Spatial_Transformation(image,label,DIM=dim)\n        image,label = Color_Distortion(image,label)\n        image,label = InformationDelection_Augmentation(image,label)\n        \n    image=tf.reshape(image,[dim,dim,3])\n    return image,label\ndef get_dataset(files,labeled=True,shuffle=True,repeat=False,augment=False,batch_size=32,dim=256,return_image_name=True):\n    dataset=tf.data.TFRecordDataset(files,num_parallel_reads=global_vars['AUTO'])\n    dataset=dataset.cache()\n    if repeat:\n        dataset=dataset.repeat()\n        \n    if shuffle:\n        dataset=dataset.shuffle(1024*8)\n        options=tf.data.Options()\n        options.experimental_deterministic=False\n        dataset=dataset.with_options(options)\n        \n    if labeled:\n        dataset=dataset.map(read_labeled_tfrecord,num_parallel_calls=global_vars['AUTO'])\n    else:\n        dataset=dataset.map(lambda example: read_unlabeled_tfrecord(example,return_image_name),num_parallel_calls=global_vars['AUTO'])\n    \n    dataset=dataset.map(lambda x,y:prepare_image(x,y,augment,dim),num_parallel_calls=global_vars['AUTO'])\n    \n    dataset=dataset.batch(batch_size*replicas)\n    \n    dataset=dataset.prefetch(global_vars['AUTO'])\n    return dataset\ndef count_data_items(filenames):\n    return np.sum([int(file.split('.tfrec')[0].split('-')[2]) for file in filenames])\n    \n\n","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:30.532704Z","iopub.status.busy":"2022-04-23T07:51:30.532346Z","iopub.status.idle":"2022-04-23T07:51:30.554173Z","shell.execute_reply":"2022-04-23T07:51:30.553184Z","shell.execute_reply.started":"2022-04-23T07:51:30.532593Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset=get_dataset(train_files,repeat=True,augment=True,batch_size=global_vars['BATCH_SIZE'],dim=global_vars['IMAGE_SIZE'])","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:30.556129Z","iopub.status.busy":"2022-04-23T07:51:30.555740Z","iopub.status.idle":"2022-04-23T07:51:32.968516Z","shell.execute_reply":"2022-04-23T07:51:32.967752Z","shell.execute_reply.started":"2022-04-23T07:51:30.556085Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Cylic Learning Rate Callback\ndef cylic(epoch,a,init_lr=0.0001,max_lr=0.001,step_size=3):\n    cycle=np.floor(1+epoch/(2*step_size))\n    x=np.abs(epoch/step_size-2*cycle+1)\n    lr=init_lr+(max_lr-init_lr)*np.maximum(0,(1-x))*a(cycle)\n    return lr\n# Learning-Rate Schedule\nclass LearningRateSchedule(tf.keras.callbacks.Callback):\n    def __init__(self,step_size,init_lr,max_lr,method,gamma=1):\n        super().__init__()\n        self.step_size=step_size\n        self.init_lr=init_lr\n        self.max_lr=max_lr\n        self.method=method\n        self.gamma=gamma\n        if self.method=='triangular':\n            self.a=lambda x: 1\n            self.schedule=cylic\n        elif self.method=='triangular2':\n            self.a=lambda x: 1/(2**(x-1))\n            self.schedule=cylic\n        elif self.method=='exp_range':\n            self.a=lambda x:  gamma**(x)\n            self.schedule=cylic\n    def on_train_begin(self,logs={}):\n        K.set_value(self.model.optimizer.learning_rate,self.init_lr)\n    def on_epoch_end(self,epoch,logs={}):\n        lr=self.schedule(epoch,self.a,self.init_lr,self.max_lr,self.step_size)\n        K.set_value(self.model.optimizer.learning_rate,lr)\n\n\n        ","metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2022-04-23T07:51:32.970192Z","iopub.status.busy":"2022-04-23T07:51:32.969888Z","iopub.status.idle":"2022-04-23T07:51:32.983151Z","shell.execute_reply":"2022-04-23T07:51:32.982324Z","shell.execute_reply.started":"2022-04-23T07:51:32.970154Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot Charts Function\ndef plot_charts(y_true,y_prob,history,fold):\n    losses=history.history['loss']\n    auc=history.history['auc']\n    y_pred=np.array([0  if i<=0.5 else 1for i in y_prob ])\n    cm=confusion_matrix(y_true,y_pred)\n    \n    grid=GridSpec(1,8)\n    Figure=plt.figure(figsize=(15,5),constrained_layout=True)\n    #Plot Line Chart of Loss and ROC AUC\n    plt.suptitle(f'{fold}',fontsize=20)\n    ax1=Figure.add_subplot(grid[0,:2])\n    ax1.plot(np.arange(len(losses)),losses,'-o',label='Loss',color='#DC143C')\n    ax1.legend(loc=(0.6,0.5))\n    ax1.set_title('Line Chart of Loss and ROC AUC')\n\n    ax12=Figure.gca().twinx()\n    ax12.plot(np.arange(len(auc)),auc,'-o',label='AUC',color='#1E90FF')\n    ax12.legend(loc=(0.6,0.43))\n    ax12.grid(False)\n\n    # Plot Confusion Matrix\n    ax2=Figure.add_subplot(grid[0,3:5])\n    sns.heatmap(cm,cbar=False,annot=True,ax=ax2)\n    ax2.set_xticklabels([])\n    ax2.set_yticklabels([])\n    ax2.set_title('Confusion Matrix')\n\n    \n    # Plot ROC AUC Curve\n    fpr,tpr,_=roc_curve(y_true,y_prob)\n    ax3=Figure.add_subplot(grid[0,6:8])\n    ax3.plot(fpr,tpr,'-o',color='#B22222')\n    ax3.fill_between(fpr,tpr,alpha=0.5,color='#F08080')\n    ax3.set_title('ROC AUC Curve')\n    \n    plt.show()\n    \n    return Figure","metadata":{"_kg_hide-input":true,"execution":{"iopub.execute_input":"2022-04-23T07:51:32.984427Z","iopub.status.busy":"2022-04-23T07:51:32.984177Z","iopub.status.idle":"2022-04-23T07:51:32.999009Z","shell.execute_reply":"2022-04-23T07:51:32.998263Z","shell.execute_reply.started":"2022-04-23T07:51:32.984401Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_model(dim=512):\n    inp = tf.keras.layers.Input(shape=(dim,dim,3))\n    base =efn.EfficientNetB7(input_shape=(dim,dim,3),weights='imagenet',include_top=False)\n    x = base(inp)\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n    x = tf.keras.layers.Dense(1,activation='sigmoid')(x)\n    model = tf.keras.Model(inputs=inp,outputs=x)\n    opt = tf.keras.optimizers.Adam(learning_rate=0.001)\n    loss = tf.keras.losses.BinaryCrossentropy(label_smoothing=0.05) \n    model.compile(optimizer=opt,loss=loss,metrics=['AUC'])\n    return model\ndef count_data_items(filenames):\n    n = [int(re.compile(r\"-([0-9]*)\\.\").search(filename).group(1)) \n         for filename in filenames]\n    return np.sum(n)","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:33.000100Z","iopub.status.busy":"2022-04-23T07:51:32.999878Z","iopub.status.idle":"2022-04-23T07:51:33.014198Z","shell.execute_reply":"2022-04-23T07:51:33.013227Z","shell.execute_reply.started":"2022-04-23T07:51:33.000074Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LR_START = 0.00001\nLR_MAX = 0.00005\nLR_MIN = 0.00001\nLR_RAMPUP_EPOCHS = 5\nLR_SUSTAIN_EPOCHS = 0\nLR_DECAY = .8\ndef lr_schedule(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_MIN) * LR_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS) + LR_MIN\n    return lr","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:33.015581Z","iopub.status.busy":"2022-04-23T07:51:33.015355Z","iopub.status.idle":"2022-04-23T07:51:33.025331Z","shell.execute_reply":"2022-04-23T07:51:33.024523Z","shell.execute_reply.started":"2022-04-23T07:51:33.015554Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train Loop\ndef TrainModel():\n    kfold=KFold(n_splits=5,shuffle=True)\n    y_prediction=np.zeros((count_data_items(test_files),1))\n    y_validation=np.zeros((count_data_items(train_files),1))\n    losses=[]\n    test_dataset=get_dataset(test_files,repeat=False,shuffle=False,dim=global_vars['IMAGE_SIZE'],batch_size=global_vars['BATCH_SIZE'],labeled=False,return_image_name=False)\n    roc_auc_scores=[]\n    if os.path.exists('SavedModels'):\n        shutil.rmtree('SavedModels')\n    os.mkdir('SavedModels')\n    for i,(train_index,val_index) in enumerate(kfold.split(np.arange(15))):\n        # Get all Files\n        Train_Files=tf.io.gfile.glob([GCS_PATH+'/train%.2i*.tfrec'%x for x in train_index])\n        Validation_Files=tf.io.gfile.glob([GCS_PATH+'/train%.2i*.tfrec'%x for x in train_index])\n\n\n\n        if global_vars['EXTERNAL_DATA']:\n            Train_Files+=tf.io.gfile.glob([GCS_PATH2+'/train%.2i*.tfrec'%x for x in train_index*2+1])\n            Train_Files+=tf.io.gfile.glob([GCS_PATH2+'/train%.2i*.tfrec'%x for x in train_index*2])\n        np.random.shuffle(Train_Files)\n\n        # Get Dataset\n        train_dataset=get_dataset(Train_Files,repeat=True,augment=True,batch_size=global_vars['BATCH_SIZE'],dim=global_vars['IMAGE_SIZE'])\n        val_dataset=get_dataset(Validation_Files,repeat=False,augment=False,shuffle=False,batch_size=global_vars['BATCH_SIZE'],dim=global_vars['IMAGE_SIZE'])\n\n        # Get Model\n        K.clear_session()\n        with strategy.scope():\n            model=get_model(dim=global_vars['IMAGE_SIZE'])\n            print(\"Model Sucessfully Loaded\")\n\n        # Callbacks\n        ckpt=tf.keras.callbacks.ModelCheckpoint(f'SavedModels/EfficientNet7{i+1}.h5',save_best_only=True,mode='min',save_weights_only=True)\n        es=tf.keras.callbacks.EarlyStopping(mode='min',patience=3,restore_best_weights=True)\n        lr_scheduler=tf.keras.callbacks.LearningRateScheduler(lr_schedule)\n        callbacks=[ckpt,es,lr_scheduler]\n\n        # Calculate steps_per_epoch\n        steps_per_epoch=int(count_data_items(Train_Files)/(global_vars['BATCH_SIZE']*replicas))\n\n        # Fit Model\n        model.fit(train_dataset,validation_data=val_dataset,epochs=15,steps_per_epoch=steps_per_epoch,callbacks=callbacks)\n\n        y_prediction[:,0]+=model.predict(test_dataset)[:,0]\n    \n    y_prediction[:,0]=y_prediction[:,0]/5\n    return y_prediction\ny_prediction=TrainModel()","metadata":{"execution":{"iopub.execute_input":"2022-04-23T07:51:33.027176Z","iopub.status.busy":"2022-04-23T07:51:33.026685Z","iopub.status.idle":"2022-04-23T16:48:59.366071Z","shell.execute_reply":"2022-04-23T16:48:59.363500Z","shell.execute_reply.started":"2022-04-23T07:51:33.027134Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install livelossplot --quiet\n","metadata":{"execution":{"iopub.execute_input":"2023-03-25T09:47:35.039696Z","iopub.status.busy":"2023-03-25T09:47:35.039397Z","iopub.status.idle":"2023-03-25T09:47:43.180788Z","shell.execute_reply":"2023-03-25T09:47:43.179914Z","shell.execute_reply.started":"2023-03-25T09:47:35.039662Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from livelossplot import PlotLossesKeras\n","metadata":{"execution":{"iopub.execute_input":"2023-03-25T09:47:46.757221Z","iopub.status.busy":"2023-03-25T09:47:46.756921Z","iopub.status.idle":"2023-03-25T09:47:46.772368Z","shell.execute_reply":"2023-03-25T09:47:46.771610Z","shell.execute_reply.started":"2023-03-25T09:47:46.757189Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating a config class to store all the configurations\nclass config:\n    \n    # Image and Tabular data paths\n    DIRECTORY_PATH = \"/kaggle/input/siim-isic-melanoma-classification/\"\n    TRAINING_SAMPLES_FOLDER = DIRECTORY_PATH + \"jpeg/train/\"\n    TESTING_SAMPLES_FOLDER = DIRECTORY_PATH + \"jpeg/test/\"\n    TRAIN_FULL_DATA = DIRECTORY_PATH + \"train.csv\"\n    TEST_FULL_DATA = DIRECTORY_PATH + \"test.csv\"\n    \n    # New directory path for image data\n    WORK_DIRECTORY = \"dataset/\"\n    TRAIN_IMAGES_FOLDER = WORK_DIRECTORY + \"training_set/\"\n    TEST_IMAGES_FOLDER = WORK_DIRECTORY + \"test_set/\"\n    VALIDATION_IMAGES_FOLDER = WORK_DIRECTORY + \"validation_set/\"\n    \n    # Input parameters for data preprocessing\n    TARGET_NAME = \"target\"\n    TRAIN_SIZE = 0.80\n    VALIDATION_SIZE = 0.10\n    TEST_SIZE = 0.10\n    SEED = 42\n    \n    # Tensorflow settings for model training\n    IMAGE_HEIGHT = 256\n    IMAGE_WIDTH = 256\n    NO_CHANNELS = 3\n    BATCH_SIZE = 256\n    EPOCHS = 20\n    DROPOUT = 0.5\n    LEARNING_RATE = 0.01\n    PATIENCE = 5","metadata":{"execution":{"iopub.execute_input":"2023-03-25T09:47:49.167675Z","iopub.status.busy":"2023-03-25T09:47:49.167409Z","iopub.status.idle":"2023-03-25T09:47:49.174609Z","shell.execute_reply":"2023-03-25T09:47:49.173924Z","shell.execute_reply.started":"2023-03-25T09:47:49.167646Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating folders for training and validation data\ndataset_home = \"./dataset/\"\nsubdirs = [\"training_set/\", \"test_set/\", \"validation_set/\"]\nfor subdir in subdirs:\n    labeldirs = [\"benign\", \"malignant\"]\n    for labeldir in labeldirs:\n        newdir = dataset_home + subdir + labeldir\n        os.makedirs(newdir, exist_ok=True)","metadata":{"execution":{"iopub.execute_input":"2023-03-25T09:47:51.989830Z","iopub.status.busy":"2023-03-25T09:47:51.989340Z","iopub.status.idle":"2023-03-25T09:47:51.995147Z","shell.execute_reply":"2023-03-25T09:47:51.994357Z","shell.execute_reply.started":"2023-03-25T09:47:51.989794Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Splitting the dataset into train, test and validation set\n\ntest_examples = train_examples = validation_examples = 0\nseed(config.SEED)\n\nfor record in open(config.TRAIN_FULL_DATA).readlines()[1:]:\n    \n    split_record = record.split(\",\")\n    image_name = split_record[0]\n    target = split_record[7]\n  \n\n    random_num = random.random()\n\n\n    if random_num < config.TRAIN_SIZE:\n        destination = config.TRAIN_IMAGES_FOLDER\n        train_examples += 1\n#         print('inif 1')\n\n    elif random_num < 0.9:\n        destination = config.VALIDATION_IMAGES_FOLDER\n        validation_examples += 1\n\n    else:\n        destination = config.TEST_IMAGES_FOLDER\n        test_examples += 1\n\n    if int(target) == 0:\n        shutil.copy(\n            config.TRAINING_SAMPLES_FOLDER + image_name + \".jpg\",\n            destination + \"benign/\" + image_name + \".jpg\"\n        )\n\n    elif int(target) == 1:\n        shutil.copy(\n            config.TRAINING_SAMPLES_FOLDER + image_name + \".jpg\",\n            destination + \"malignant/\" + image_name + \".jpg\"\n        )\n\n","metadata":{"execution":{"iopub.execute_input":"2023-03-25T09:47:55.792658Z","iopub.status.busy":"2023-03-25T09:47:55.792391Z","iopub.status.idle":"2023-03-25T09:48:25.769881Z","shell.execute_reply":"2023-03-25T09:48:25.769083Z","shell.execute_reply.started":"2023-03-25T09:47:55.792628Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Preparing the data and performing Data Augmentation\ntrain_datagen = ImageDataGenerator(\n    rescale=1./255,\n    shear_range=0.2,\n    zoom_range=(0.95, 0.95),\n    rotation_range=15,\n    horizontal_flip=True,\n    vertical_flip=True,\n    data_format=\"channels_last\",\n    dtype=tf.float32\n)\n\nvalidation_datagen = ImageDataGenerator(\n    rescale=1./255,\n    dtype=tf.float32\n)\n\ntrain_generator = train_datagen.flow_from_directory(\n    directory=config.TRAIN_IMAGES_FOLDER,\n    target_size=(config.IMAGE_HEIGHT, config.IMAGE_WIDTH),\n    color_mode=\"rgb\",\n    batch_size=config.BATCH_SIZE,\n    class_mode=\"binary\",\n    shuffle=True\n)\n\nvalidation_generator = validation_datagen.flow_from_directory(\n    directory=config.VALIDATION_IMAGES_FOLDER,\n    target_size=(config.IMAGE_HEIGHT, config.IMAGE_WIDTH),\n    color_mode=\"rgb\",\n    batch_size=config.BATCH_SIZE,\n    class_mode=\"binary\",\n    shuffle=True\n)","metadata":{"execution":{"iopub.execute_input":"2023-03-25T09:48:41.323575Z","iopub.status.busy":"2023-03-25T09:48:41.323297Z","iopub.status.idle":"2023-03-25T09:48:41.540087Z","shell.execute_reply":"2023-03-25T09:48:41.539273Z","shell.execute_reply.started":"2023-03-25T09:48:41.323542Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Metrics to use for compiling the model\nMETRICS = [keras.metrics.AUC(name=\"auc\")]","metadata":{"execution":{"iopub.execute_input":"2023-03-25T09:48:45.178463Z","iopub.status.busy":"2023-03-25T09:48:45.178167Z","iopub.status.idle":"2023-03-25T09:48:47.553462Z","shell.execute_reply":"2023-03-25T09:48:47.552699Z","shell.execute_reply.started":"2023-03-25T09:48:45.178431Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building a transfer learning model using Keras\nbase_model = tf.keras.applications.inception_resnet_v2.InceptionResNetV2(include_top= False, classes =2)\n\n\n\n# 2. Freeze the base model(so the underlying pre-trained patterns aren't updated during training)\nbase_model.trainable = False\n\n# 3. Create Inputs into our model\ninputs = tf.keras.layers.Input(shape=(config.IMAGE_HEIGHT, config.IMAGE_WIDTH, config.NO_CHANNELS),name='input_layer')\n\n# 4. If using ResNet50V2, add this to speed up convergence, remove for EfficientNet\nx = tf.keras.layers.experimental.preprocessing.Rescaling(1./255)(inputs)\n\n# 5. Pass the inputs to the base_model\nx = base_model(inputs)\nprint(f\"shape after passing inputs throught base modelL{x.shape}\")\n\n# 6. Average pool the outputs of the base model(aggreate all the most important information, reduce number of computation)\nx = tf.keras.layers.GlobalAveragePooling2D(name = \"global_average_pooling\") (x)\nprint(f\"Shape after GlobalAveragingPooling2D: {x.shape}\")\n\n# 7. Create output activation layer\noutputs = tf.keras.layers.Dense(1, activation ='sigmoid',name = 'output_layer')(x)\n\n# 8. Combine the inputs and outputs into a model\nmodel_0 = tf.keras.Model(inputs, outputs)\n\n#9. Compile the model\nmodel_0.compile(optimizer='adam', \n              loss=keras.losses.BinaryCrossentropy(),\n              metrics=METRICS)","metadata":{"execution":{"iopub.execute_input":"2023-03-24T09:10:36.600428Z","iopub.status.busy":"2023-03-24T09:10:36.600170Z","iopub.status.idle":"2023-03-24T09:10:49.475999Z","shell.execute_reply":"2023-03-24T09:10:49.474458Z","shell.execute_reply.started":"2023-03-24T09:10:36.600397Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compiling the transfer learning model\nmodel_0.compile(optimizer=keras.optimizers.Adam(), \n              loss=keras.losses.BinaryCrossentropy(),\n              metrics=METRICS)","metadata":{"execution":{"iopub.execute_input":"2023-03-24T09:11:22.321383Z","iopub.status.busy":"2023-03-24T09:11:22.321092Z","iopub.status.idle":"2023-03-24T09:11:22.341641Z","shell.execute_reply":"2023-03-24T09:11:22.340803Z","shell.execute_reply.started":"2023-03-24T09:11:22.321350Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training the model\nhistory = model_0.fit(train_generator, epochs=config.EPOCHS,\n          validation_data=validation_generator,\n          validation_freq=1)","metadata":{"execution":{"iopub.execute_input":"2023-03-24T09:11:32.368419Z","iopub.status.busy":"2023-03-24T09:11:32.368145Z","iopub.status.idle":"2023-03-24T12:04:29.742917Z","shell.execute_reply":"2023-03-24T12:04:29.742161Z","shell.execute_reply.started":"2023-03-24T09:11:32.368387Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building a transfer learning model using Keras\nbase_model = tf.keras.applications.DenseNet121 (include_top= False, classes =2)\n\nbase_model.trainable = False\n\ninputs = tf.keras.layers.Input(shape=(config.IMAGE_HEIGHT, config.IMAGE_WIDTH, config.NO_CHANNELS),name='input_layer')\n\nx = tf.keras.layers.experimental.preprocessing.Rescaling(1./255)(inputs)\n\nx = base_model(inputs)\nprint(f\"shape after passing inputs throught base modelL{x.shape}\")\n\nx = tf.keras.layers.GlobalAveragePooling2D(name = \"global_average_pooling\") (x)\nprint(f\"Shape after GlobalAveragingPooling2D: {x.shape}\")\n\noutputs = tf.keras.layers.Dense(1, activation ='sigmoid',name = 'output_layer')(x)\n\nmodel_1 = tf.keras.Model(inputs, outputs)\n\nmodel_1.compile(optimizer='adam', \n              loss=keras.losses.BinaryCrossentropy(),\n              metrics=METRICS)","metadata":{"execution":{"iopub.execute_input":"2023-03-25T09:49:50.026345Z","iopub.status.busy":"2023-03-25T09:49:50.026032Z","iopub.status.idle":"2023-03-25T09:49:56.088558Z","shell.execute_reply":"2023-03-25T09:49:56.087714Z","shell.execute_reply.started":"2023-03-25T09:49:50.026310Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training the model\nhistory = model_1.fit(train_generator, epochs=config.EPOCHS,\n          validation_data=validation_generator,\n          validation_freq=1)","metadata":{"execution":{"iopub.execute_input":"2023-03-25T09:49:56.094803Z","iopub.status.busy":"2023-03-25T09:49:56.092631Z","iopub.status.idle":"2023-03-25T11:15:19.790119Z","shell.execute_reply":"2023-03-25T11:15:19.789339Z","shell.execute_reply.started":"2023-03-25T09:49:56.094762Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Building a transfer learning model using Keras\nbase_model = tf.keras.applications.VGG16 (include_top= False, classes =2)\n\nbase_model.trainable = False\n\ninputs = tf.keras.layers.Input(shape=(config.IMAGE_HEIGHT, config.IMAGE_WIDTH, config.NO_CHANNELS),name='input_layer')\n\nx = tf.keras.layers.experimental.preprocessing.Rescaling(1./255)(inputs)\n\nx = base_model(inputs)\nprint(f\"shape after passing inputs throught base modelL{x.shape}\")\n\nx = tf.keras.layers.GlobalAveragePooling2D(name = \"global_average_pooling\") (x)\nprint(f\"Shape after GlobalAveragingPooling2D: {x.shape}\")\n\noutputs = tf.keras.layers.Dense(1, activation ='sigmoid',name = 'output_layer')(x)\n\nmodel_2 = tf.keras.Model(inputs, outputs)\n\nmodel_2.compile(optimizer='adam', \n              loss=keras.losses.BinaryCrossentropy(),\n              metrics=METRICS)","metadata":{"execution":{"iopub.execute_input":"2023-03-25T11:15:19.797739Z","iopub.status.busy":"2023-03-25T11:15:19.797504Z","iopub.status.idle":"2023-03-25T11:15:23.875728Z","shell.execute_reply":"2023-03-25T11:15:23.874990Z","shell.execute_reply.started":"2023-03-25T11:15:19.797699Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training the model\nhistory = model_2.fit(train_generator, epochs=config.EPOCHS,\n          validation_data=validation_generator,\n          validation_freq=1)","metadata":{"execution":{"iopub.execute_input":"2023-03-25T11:15:23.878104Z","iopub.status.busy":"2023-03-25T11:15:23.877748Z","iopub.status.idle":"2023-03-25T12:41:20.106504Z","shell.execute_reply":"2023-03-25T12:41:20.105750Z","shell.execute_reply.started":"2023-03-25T11:15:23.878064Z"}},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(test_imgs)\ncm=confusion_matrix(test_labels, y_pred)  \n                       \ncm_display = ConfusionMatrixDisplay(confusion_matrix = cm, display_labels = [\"Melanoma\", \"Non-Melanoma\"])\n\ncm_display.plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-08T12:56:43.664375Z","iopub.execute_input":"2023-04-08T12:56:43.664737Z","iopub.status.idle":"2023-04-08T12:56:43.918123Z","shell.execute_reply.started":"2023-04-08T12:56:43.664704Z","shell.execute_reply":"2023-04-08T12:56:43.917346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total1=sum(sum(cm))\n#####from confusion matrix calculate accuracy\naccuracy1=(cm[0,0]+cm[1,1])/total1\nprint ('Accuracy : ', accuracy1)\n\nsensitivity1 = cm[0,0]/(cm[0,0]+cm[0,1])\nprint('Sensitivity : ', sensitivity1 )\n\nspecificity1 = cm[1,1]/(cm[1,0]+cm[1,1])\nprint('Specificity : ', specificity1)","metadata":{"execution":{"iopub.status.busy":"2023-04-08T12:56:44.002067Z","iopub.execute_input":"2023-04-08T12:56:44.002973Z","iopub.status.idle":"2023-04-08T12:56:44.010630Z","shell.execute_reply.started":"2023-04-08T12:56:44.002933Z","shell.execute_reply":"2023-04-08T12:56:44.009714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_0.predict(test_imgs)\ncm=confusion_matrix(test_labels, y_pred)  \n\ncm_display = ConfusionMatrixDisplay(confusion_matrix = cm, display_labels = [\"Melanoma\", \"Non-Melanoma\"])\n\ncm_display.plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-08T12:56:44.265131Z","iopub.execute_input":"2023-04-08T12:56:44.265434Z","iopub.status.idle":"2023-04-08T12:56:44.497552Z","shell.execute_reply.started":"2023-04-08T12:56:44.265397Z","shell.execute_reply":"2023-04-08T12:56:44.496659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total1=sum(sum(cm))\n#####from confusion matrix calculate accuracy\naccuracy1=(cm[0,0]+cm[1,1])/total1\nprint ('Accuracy : ', accuracy1)\n\nsensitivity1 = cm[0,0]/(cm[0,0]+cm[0,1])\nprint('Sensitivity : ', sensitivity1 )\n\nspecificity1 = cm[1,1]/(cm[1,0]+cm[1,1])\nprint('Specificity : ', specificity1)","metadata":{"execution":{"iopub.status.busy":"2023-04-08T12:56:44.633887Z","iopub.execute_input":"2023-04-08T12:56:44.634386Z","iopub.status.idle":"2023-04-08T12:56:44.643983Z","shell.execute_reply.started":"2023-04-08T12:56:44.634321Z","shell.execute_reply":"2023-04-08T12:56:44.642839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_1.predict(test_imgs)\ncm=confusion_matrix(test_labels, y_pred)  \n\ncm_display =  ConfusionMatrixDisplay(confusion_matrix = cm, display_labels = [\"Melanoma\", \"Non-Melanoma\"])\n\ncm_display.plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-08T12:56:46.259621Z","iopub.execute_input":"2023-04-08T12:56:46.260602Z","iopub.status.idle":"2023-04-08T12:56:46.509537Z","shell.execute_reply.started":"2023-04-08T12:56:46.260557Z","shell.execute_reply":"2023-04-08T12:56:46.508787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total1=sum(sum(cm))\n#####from confusion matrix calculate accuracy\naccuracy1=(cm[0,0]+cm[1,1])/total1\nprint ('Accuracy : ', accuracy1)\n\nsensitivity1 = cm[0,0]/(cm[0,0]+cm[0,1])\nprint('Sensitivity : ', sensitivity1 )\n\nspecificity1 = cm[1,1]/(cm[1,0]+cm[1,1])\nprint('Specificity : ', specificity1)","metadata":{"execution":{"iopub.status.busy":"2023-04-08T12:56:47.253153Z","iopub.execute_input":"2023-04-08T12:56:47.253479Z","iopub.status.idle":"2023-04-08T12:56:47.262121Z","shell.execute_reply.started":"2023-04-08T12:56:47.253446Z","shell.execute_reply":"2023-04-08T12:56:47.261297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model_2.predict(test_imgs)\ncm=confusion_matrix(test_labels, y_pred)  \n\ncm_display =  ConfusionMatrixDisplay(confusion_matrix = cm, display_labels = [\"Melanoma\", \"Non-Melanoma\"])\n\ncm_display.plot()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-04-08T12:56:47.870807Z","iopub.execute_input":"2023-04-08T12:56:47.871440Z","iopub.status.idle":"2023-04-08T12:56:48.100091Z","shell.execute_reply.started":"2023-04-08T12:56:47.871407Z","shell.execute_reply":"2023-04-08T12:56:48.099164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"total1=sum(sum(cm))\n#####from confusion matrix calculate accuracy\naccuracy1=(cm[0,0]+cm[1,1])/total1\nprint ('Accuracy : ', accuracy1)\n\nsensitivity1 = cm[0,0]/(cm[0,0]+cm[0,1])\nprint('Sensitivity : ', sensitivity1 )\n\nspecificity1 = cm[1,1]/(cm[1,0]+cm[1,1])\nprint('Specificity : ', specificity1)","metadata":{"execution":{"iopub.status.busy":"2023-04-08T13:03:47.244289Z","iopub.execute_input":"2023-04-08T13:03:47.244711Z","iopub.status.idle":"2023-04-08T13:03:47.254026Z","shell.execute_reply.started":"2023-04-08T13:03:47.244667Z","shell.execute_reply":"2023-04-08T13:03:47.252832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}