{"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 numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-25T11:11:10.224019Z","iopub.execute_input":"2021-09-25T11:11:10.224755Z","iopub.status.idle":"2021-09-25T11:11:10.233451Z","shell.execute_reply.started":"2021-09-25T11:11:10.224725Z","shell.execute_reply":"2021-09-25T11:11:10.232018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_csv=pd.read_csv('../input/histopathologic-cancer-detection/train_labels.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:11:11.005392Z","iopub.execute_input":"2021-09-25T11:11:11.005755Z","iopub.status.idle":"2021-09-25T11:11:11.214146Z","shell.execute_reply.started":"2021-09-25T11:11:11.005710Z","shell.execute_reply":"2021-09-25T11:11:11.213195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_csv.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:11:12.413769Z","iopub.execute_input":"2021-09-25T11:11:12.414105Z","iopub.status.idle":"2021-09-25T11:11:12.425641Z","shell.execute_reply.started":"2021-09-25T11:11:12.414075Z","shell.execute_reply":"2021-09-25T11:11:12.424484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_csv.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:11:13.597788Z","iopub.execute_input":"2021-09-25T11:11:13.598475Z","iopub.status.idle":"2021-09-25T11:11:13.630780Z","shell.execute_reply.started":"2021-09-25T11:11:13.598442Z","shell.execute_reply":"2021-09-25T11:11:13.629134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='label',data=trian_csv)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:11:15.067393Z","iopub.execute_input":"2021-09-25T11:11:15.067725Z","iopub.status.idle":"2021-09-25T11:11:15.289490Z","shell.execute_reply.started":"2021-09-25T11:11:15.067692Z","shell.execute_reply":"2021-09-25T11:11:15.287962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_csv['label'] = trian_csv['label'].astype('str')","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:11:18.964051Z","iopub.execute_input":"2021-09-25T11:11:18.967435Z","iopub.status.idle":"2021-09-25T11:11:19.400852Z","shell.execute_reply.started":"2021-09-25T11:11:18.967357Z","shell.execute_reply":"2021-09-25T11:11:19.399773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trian_csv[\"id\"] = trian_csv[\"id\"].apply(lambda x: x + \".tif\")\ntrian_csv.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:11:23.373485Z","iopub.execute_input":"2021-09-25T11:11:23.374333Z","iopub.status.idle":"2021-09-25T11:11:23.478956Z","shell.execute_reply.started":"2021-09-25T11:11:23.374286Z","shell.execute_reply":"2021-09-25T11:11:23.477729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\ndatagen = tf.keras.preprocessing.image.ImageDataGenerator(\n            rescale=1./255.0,\n            rotation_range=10,\n            width_shift_range=0.2,\n            height_shift_range=0.2,\n            horizontal_flip=True,\n            vertical_flip=True,\n            validation_split = 0.1\n            )","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:11:24.963313Z","iopub.execute_input":"2021-09-25T11:11:24.964051Z","iopub.status.idle":"2021-09-25T11:11:24.970005Z","shell.execute_reply.started":"2021-09-25T11:11:24.964012Z","shell.execute_reply":"2021-09-25T11:11:24.968702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = datagen.flow_from_dataframe(\n            dataframe=trian_csv,\n            directory=\"../input/histopathologic-cancer-detection/train/\",\n            x_col=\"id\",\n            y_col=\"label\",\n            subset=\"training\",\n            batch_size=128,\n            seed=42,\n            shuffle=True,\n            class_mode=\"binary\",\n            target_size=(224,224))","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:11:26.820909Z","iopub.execute_input":"2021-09-25T11:11:26.821979Z","iopub.status.idle":"2021-09-25T11:22:58.219409Z","shell.execute_reply.started":"2021-09-25T11:11:26.821934Z","shell.execute_reply":"2021-09-25T11:22:58.218213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes=list(train_generator.class_indices.keys())","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:24:53.009380Z","iopub.execute_input":"2021-09-25T11:24:53.009702Z","iopub.status.idle":"2021-09-25T11:24:53.015551Z","shell.execute_reply.started":"2021-09-25T11:24:53.009671Z","shell.execute_reply":"2021-09-25T11:24:53.014209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:25:02.489306Z","iopub.execute_input":"2021-09-25T11:25:02.489605Z","iopub.status.idle":"2021-09-25T11:25:02.497057Z","shell.execute_reply.started":"2021-09-25T11:25:02.489576Z","shell.execute_reply":"2021-09-25T11:25:02.495765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes=list(train_generator.class_indices.keys())\nplt.figure(figsize=(20,20))\nfor X_batch, y_batch in train_generator:\n    # create a grid of 3x3 images\n    for i in range(0,36):\n        plt.subplot(6,6,i+1)\n        plt.imshow(X_batch[i])\n    # show the plot\n    plt.show()\n    break","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:27:11.563556Z","iopub.execute_input":"2021-09-25T11:27:11.563854Z","iopub.status.idle":"2021-09-25T11:27:19.910197Z","shell.execute_reply.started":"2021-09-25T11:27:11.563825Z","shell.execute_reply":"2021-09-25T11:27:19.909325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen_valid = tf.keras.preprocessing.image.ImageDataGenerator(\n            rescale=1./255.,\n            validation_split = 0.1\n            )","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:27:31.172756Z","iopub.execute_input":"2021-09-25T11:27:31.173069Z","iopub.status.idle":"2021-09-25T11:27:31.179658Z","shell.execute_reply.started":"2021-09-25T11:27:31.173039Z","shell.execute_reply":"2021-09-25T11:27:31.178355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_generator = datagen_valid.flow_from_dataframe(\n            dataframe=trian_csv,\n            directory=\"../input/histopathologic-cancer-detection/train\",\n            x_col=\"id\",\n            y_col=\"label\",\n            subset=\"validation\",\n            batch_size=128,\n            seed=42,\n            shuffle=True,\n            class_mode=\"binary\",\n            target_size=(224,224))","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:27:34.693962Z","iopub.execute_input":"2021-09-25T11:27:34.694701Z","iopub.status.idle":"2021-09-25T11:30:30.705397Z","shell.execute_reply.started":"2021-09-25T11:27:34.694653Z","shell.execute_reply":"2021-09-25T11:30:30.704295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,20))\nfor X_batch, y_batch in valid_generator:\n    # create a grid of 3x3 images\n    for i in range(0,36):\n        plt.subplot(6,6,i+1)\n        plt.imshow(X_batch[i])\n    # show the plot\n    plt.show()\n    break","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:30:30.707690Z","iopub.execute_input":"2021-09-25T11:30:30.708198Z","iopub.status.idle":"2021-09-25T11:30:37.510933Z","shell.execute_reply.started":"2021-09-25T11:30:30.708157Z","shell.execute_reply":"2021-09-25T11:30:37.503012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_batch.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:38:57.665551Z","iopub.execute_input":"2021-09-25T11:38:57.665855Z","iopub.status.idle":"2021-09-25T11:38:57.673889Z","shell.execute_reply.started":"2021-09-25T11:38:57.665825Z","shell.execute_reply":"2021-09-25T11:38:57.672478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv=pd.read_csv('../input/histopathologic-cancer-detection/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:22:58.221224Z","iopub.execute_input":"2021-09-25T11:22:58.221782Z","iopub.status.idle":"2021-09-25T11:22:58.382906Z","shell.execute_reply.started":"2021-09-25T11:22:58.221731Z","shell.execute_reply":"2021-09-25T11:22:58.381839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:30:37.512600Z","iopub.execute_input":"2021-09-25T11:30:37.512995Z","iopub.status.idle":"2021-09-25T11:30:37.532961Z","shell.execute_reply.started":"2021-09-25T11:30:37.512954Z","shell.execute_reply":"2021-09-25T11:30:37.531530Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='label',data=test_csv)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:30:37.536375Z","iopub.execute_input":"2021-09-25T11:30:37.536719Z","iopub.status.idle":"2021-09-25T11:30:37.760869Z","shell.execute_reply.started":"2021-09-25T11:30:37.536683Z","shell.execute_reply":"2021-09-25T11:30:37.759988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv['label'] = test_csv['label'].astype('str')","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:30:37.764162Z","iopub.execute_input":"2021-09-25T11:30:37.764885Z","iopub.status.idle":"2021-09-25T11:30:37.838821Z","shell.execute_reply.started":"2021-09-25T11:30:37.764856Z","shell.execute_reply":"2021-09-25T11:30:37.837518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv[\"id\"] = test_csv[\"id\"].apply(lambda x: x + \".tif\")\ntest_csv.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:30:37.840899Z","iopub.execute_input":"2021-09-25T11:30:37.841166Z","iopub.status.idle":"2021-09-25T11:30:37.876376Z","shell.execute_reply.started":"2021-09-25T11:30:37.841137Z","shell.execute_reply":"2021-09-25T11:30:37.875323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen_test = tf.keras.preprocessing.image.ImageDataGenerator(\n            rescale=1./255.\n            )","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:30:37.877917Z","iopub.execute_input":"2021-09-25T11:30:37.878614Z","iopub.status.idle":"2021-09-25T11:30:37.886215Z","shell.execute_reply.started":"2021-09-25T11:30:37.878571Z","shell.execute_reply":"2021-09-25T11:30:37.884942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = datagen_test.flow_from_dataframe(\n            dataframe=test_csv,\n            directory=\"../input/histopathologic-cancer-detection/test\",\n            x_col=\"id\",\n            y_col=\"label\",\n            batch_size=128,\n            seed=42,\n            shuffle=True,\n            target_size=(224,224))","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:31:46.792761Z","iopub.execute_input":"2021-09-25T11:31:46.793065Z","iopub.status.idle":"2021-09-25T11:34:39.249118Z","shell.execute_reply.started":"2021-09-25T11:31:46.793036Z","shell.execute_reply":"2021-09-25T11:34:39.248063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,20))\nfor X_batch, y_batch in valid_generator:\n    # create a grid of 3x3 images\n    for i in range(0,36):\n        plt.subplot(6,6,i+1)\n        plt.imshow(X_batch[i])\n    # show the plot\n    plt.show()\n    break","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:34:39.251430Z","iopub.execute_input":"2021-09-25T11:34:39.252130Z","iopub.status.idle":"2021-09-25T11:34:45.781956Z","shell.execute_reply.started":"2021-09-25T11:34:39.252088Z","shell.execute_reply":"2021-09-25T11:34:45.780966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense ,Flatten ,Conv2D ,MaxPooling2D ,Dropout ,BatchNormalization ,GlobalMaxPool2D\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping ,ReduceLROnPlateau ,ModelCheckpoint","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:35:37.470297Z","iopub.execute_input":"2021-09-25T11:35:37.470662Z","iopub.status.idle":"2021-09-25T11:35:37.478477Z","shell.execute_reply.started":"2021-09-25T11:35:37.470633Z","shell.execute_reply":"2021-09-25T11:35:37.475480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#optimizer\noptimizer_adam=Adam(learning_rate=0.001,beta_1=0.9,beta_2=0.99)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:36:08.719169Z","iopub.execute_input":"2021-09-25T11:36:08.719698Z","iopub.status.idle":"2021-09-25T11:36:08.732335Z","shell.execute_reply.started":"2021-09-25T11:36:08.719665Z","shell.execute_reply":"2021-09-25T11:36:08.731188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#callbacks\n\nEarlyStop=EarlyStopping(patience=10,restore_best_weights=True)\nReduce_LR=ReduceLROnPlateau(monitor='val_accuracy',verbose=2,factor=0.5,min_lr=0.00001)\nmodel_check=ModelCheckpoint('model.hdf5',monitor='val_loss',verbose=1,save_best_only=True)\ncallback=[EarlyStop , Reduce_LR,model_check]","metadata":{"execution":{"iopub.status.busy":"2021-09-25T11:36:10.948442Z","iopub.execute_input":"2021-09-25T11:36:10.949103Z","iopub.status.idle":"2021-09-25T11:36:10.957119Z","shell.execute_reply.started":"2021-09-25T11:36:10.949069Z","shell.execute_reply":"2021-09-25T11:36:10.955299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential([\n                  Conv2D(64,3,activation='relu',kernel_initializer='he_normal',input_shape=(224,224,3)),\n                  BatchNormalization(),\n                  Conv2D(128,3,activation='relu',kernel_initializer='he_normal'),\n                  BatchNormalization(),\n                  MaxPooling2D(3),\n                  Conv2D(256,3,activation='relu'),\n                  BatchNormalization(),\n                  MaxPooling2D(3),\n                  Conv2D(512,3,padding='valid',activation='relu'),\n                  BatchNormalization(),\n                  GlobalMaxPool2D(),\n                  Flatten(),\n                  Dense(512,activation='relu',kernel_regularizer=tf.keras.regularizers.l2(0.0001)),\n                  BatchNormalization(),\n                  Dense(1024,activation='relu',kernel_regularizer=tf.keras.regularizers.l2(0.0001)),\n                  BatchNormalization(),\n                  Dense(1,activation='sigmoid',kernel_initializer='glorot_normal')\n                  \n])\n\nmodel.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-09-25T13:13:22.149630Z","iopub.execute_input":"2021-09-25T13:13:22.150471Z","iopub.status.idle":"2021-09-25T13:13:22.742872Z","shell.execute_reply.started":"2021-09-25T13:13:22.150422Z","shell.execute_reply":"2021-09-25T13:13:22.741726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(train_generator,validation_data=valid_generator,epochs=50,batch_size=128,\n                  steps_per_epoch=len(train_generator)//128,validation_steps=len(valid_generator)//128,\n                  callbacks=callback, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T13:30:39.796434Z","iopub.execute_input":"2021-09-25T13:30:39.796772Z","iopub.status.idle":"2021-09-25T13:43:04.895914Z","shell.execute_reply.started":"2021-09-25T13:30:39.796742Z","shell.execute_reply":"2021-09-25T13:43:04.894316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(test_generator)","metadata":{"execution":{"iopub.status.busy":"2021-09-25T13:43:04.901294Z","iopub.execute_input":"2021-09-25T13:43:04.904181Z","iopub.status.idle":"2021-09-25T13:46:04.071329Z","shell.execute_reply.started":"2021-09-25T13:43:04.904138Z","shell.execute_reply":"2021-09-25T13:46:04.070286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plotting training values\nimport seaborn as sns\nsns.set()\n\nacc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs = range(1, len(loss) + 1)\n\n#accuracy plot\nplt.plot(epochs, acc, color='green', label='Training Accuracy')\nplt.plot(epochs, val_acc, color='blue', label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend()\n\nplt.figure()\n#loss plot\nplt.plot(epochs, loss, color='green', label='Training Loss')\nplt.plot(epochs, val_loss, color='red', label='Validation Loss')\nplt.title('Training and Validation Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-09-25T13:46:04.073052Z","iopub.execute_input":"2021-09-25T13:46:04.073485Z","iopub.status.idle":"2021-09-25T13:46:04.741647Z","shell.execute_reply.started":"2021-09-25T13:46:04.073442Z","shell.execute_reply":"2021-09-25T13:46:04.740703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}