{"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":"markdown","source":"# Histopathologic Cancer Detection Re-run\n\nWith this notebook i am re-running the model for 25 more epochs.","metadata":{}},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nimport random\nimport pickle\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import roc_curve, auc, roc_auc_score\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, BatchNormalization, Activation, Conv2D, MaxPool2D\nfrom keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.layers import *\n\nfrom keras_preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-12-03T12:17:24.879080Z","iopub.execute_input":"2021-12-03T12:17:24.879417Z","iopub.status.idle":"2021-12-03T12:17:26.322880Z","shell.execute_reply.started":"2021-12-03T12:17:24.879383Z","shell.execute_reply":"2021-12-03T12:17:26.321840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Parameters","metadata":{}},{"cell_type":"code","source":"BATCH_SIZE = 64\nIMG_SIZE = 96\nRANDOM_SEED = 1982","metadata":{"execution":{"iopub.status.busy":"2021-12-03T12:17:32.239773Z","iopub.execute_input":"2021-12-03T12:17:32.241189Z","iopub.status.idle":"2021-12-03T12:17:32.256455Z","shell.execute_reply.started":"2021-12-03T12:17:32.241136Z","shell.execute_reply":"2021-12-03T12:17:32.254548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Dataset","metadata":{}},{"cell_type":"code","source":"dataset = '/kaggle/input/histopathologic-cancer-detection/'\ntrain_path = dataset+'train/'\ntest_path = dataset+'test/'","metadata":{"execution":{"iopub.status.busy":"2021-12-03T12:17:59.431615Z","iopub.execute_input":"2021-12-03T12:17:59.431959Z","iopub.status.idle":"2021-12-03T12:17:59.439053Z","shell.execute_reply.started":"2021-12-03T12:17:59.431897Z","shell.execute_reply":"2021-12-03T12:17:59.437067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(dataset+'train_labels.csv', dtype=str)\nprint('Training Set Size:', data.shape)\ndata['path'] = data.id + '.tif'\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-03T12:18:07.093631Z","iopub.execute_input":"2021-12-03T12:18:07.094156Z","iopub.status.idle":"2021-12-03T12:18:07.350754Z","shell.execute_reply.started":"2021-12-03T12:18:07.094093Z","shell.execute_reply":"2021-12-03T12:18:07.349645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test Train split by 70/30","metadata":{}},{"cell_type":"code","source":"train, valid = train_test_split(data, test_size=0.2, random_state=RANDOM_SEED, stratify=data.label)\n\nprint(train.shape)\nprint(valid.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-03T12:18:14.024051Z","iopub.execute_input":"2021-12-03T12:18:14.024663Z","iopub.status.idle":"2021-12-03T12:18:14.414864Z","shell.execute_reply.started":"2021-12-03T12:18:14.024628Z","shell.execute_reply":"2021-12-03T12:18:14.413799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Generators","metadata":{}},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1/255)\nvalid_datagen = ImageDataGenerator(rescale=1/255)","metadata":{"execution":{"iopub.status.busy":"2021-12-03T12:18:50.829857Z","iopub.execute_input":"2021-12-03T12:18:50.830223Z","iopub.status.idle":"2021-12-03T12:18:50.837691Z","shell.execute_reply.started":"2021-12-03T12:18:50.830187Z","shell.execute_reply":"2021-12-03T12:18:50.836654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = train_datagen.flow_from_dataframe(\n    dataframe = train,\n    directory = train_path,\n    x_col = 'path',\n    y_col = 'label',\n    batch_size = BATCH_SIZE,\n    seed = RANDOM_SEED,\n    shuffle = True,\n    class_mode = 'categorical',\n    target_size = (IMG_SIZE,IMG_SIZE)\n)\n\nvalid_loader = train_datagen.flow_from_dataframe(\n    dataframe = valid,\n    directory = train_path,\n    x_col = 'path',\n    y_col = 'label',\n    batch_size = BATCH_SIZE,\n    seed = RANDOM_SEED,\n    shuffle = True,\n    class_mode = 'categorical',\n    target_size = (IMG_SIZE,IMG_SIZE)\n)","metadata":{"execution":{"iopub.status.busy":"2021-12-03T12:18:56.371169Z","iopub.execute_input":"2021-12-03T12:18:56.371979Z","iopub.status.idle":"2021-12-03T12:24:11.513693Z","shell.execute_reply.started":"2021-12-03T12:18:56.371928Z","shell.execute_reply":"2021-12-03T12:24:11.512549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CNN Model using ResNet50","metadata":{}},{"cell_type":"code","source":"np.random.seed(RANDOM_SEED)\ntf.random.set_seed(RANDOM_SEED)\n\ncnn = tf.keras.models.load_model('../input/cancerdetection-tl-v02/cancer_model_v01.h5')","metadata":{"execution":{"iopub.status.busy":"2021-12-03T12:26:40.901844Z","iopub.execute_input":"2021-12-03T12:26:40.902434Z","iopub.status.idle":"2021-12-03T12:26:46.429184Z","shell.execute_reply.started":"2021-12-03T12:26:40.902401Z","shell.execute_reply":"2021-12-03T12:26:46.428026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = tf.keras.applications.VGG16(input_shape=(96,96,3),\n                                         include_top=False,\n                                         weights='imagenet')\n\nbase_model.trainable = True\n#K.set_value(cnn.optimizer.learning_rate, 0.0001)","metadata":{"execution":{"iopub.status.busy":"2021-12-03T12:30:09.064893Z","iopub.execute_input":"2021-12-03T12:30:09.065261Z","iopub.status.idle":"2021-12-03T12:30:09.436002Z","shell.execute_reply.started":"2021-12-03T12:30:09.065230Z","shell.execute_reply":"2021-12-03T12:30:09.434968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"opt = tf.keras.optimizers.Adam(0.0001)\ncnn.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy', tf.keras.metrics.AUC()])\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2021-12-03T12:30:26.900206Z","iopub.execute_input":"2021-12-03T12:30:26.900505Z","iopub.status.idle":"2021-12-03T12:30:26.929787Z","shell.execute_reply.started":"2021-12-03T12:30:26.900470Z","shell.execute_reply":"2021-12-03T12:30:26.928651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TR_STEPS = len(train_loader)\nVA_STEPS = len(valid_loader)\n\nprint(TR_STEPS)\nprint(VA_STEPS)","metadata":{"execution":{"iopub.status.busy":"2021-12-03T12:30:48.446366Z","iopub.execute_input":"2021-12-03T12:30:48.446678Z","iopub.status.idle":"2021-12-03T12:30:48.454037Z","shell.execute_reply.started":"2021-12-03T12:30:48.446645Z","shell.execute_reply":"2021-12-03T12:30:48.452870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time \n\n# Complete one or more training runs. \n# Display training curves after each run. \n\nh2 = cnn.fit(\n    x = train_loader, \n    steps_per_epoch = TR_STEPS, \n    epochs = 20,\n    validation_data = valid_loader, \n    validation_steps = VA_STEPS, \n    verbose = 1,\n    use_multiprocessing=True, \n    workers=8\n)","metadata":{"execution":{"iopub.status.busy":"2021-12-03T12:31:06.696772Z","iopub.execute_input":"2021-12-03T12:31:06.697354Z","iopub.status.idle":"2021-12-03T14:09:51.216062Z","shell.execute_reply.started":"2021-12-03T12:31:06.697317Z","shell.execute_reply":"2021-12-03T14:09:51.214734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pickle_file = open(\"../input/cancerdetection-tl-v02/cancer_history_v00.pkl\", \"rb\")\nhistory = pickle.load(pickle_file)\npickle_file.close()","metadata":{"execution":{"iopub.status.busy":"2021-12-03T14:28:36.540984Z","iopub.execute_input":"2021-12-03T14:28:36.541302Z","iopub.status.idle":"2021-12-03T14:28:36.562133Z","shell.execute_reply.started":"2021-12-03T14:28:36.541270Z","shell.execute_reply":"2021-12-03T14:28:36.561156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for k in h2.history.keys():\n    history[k] += h2.history[k]\n    \nepoch_range = range(1, len(history['loss'])+1)\n\nplt.figure(figsize=[14,4])\nplt.subplot(1,3,1)\nplt.plot(epoch_range, history['loss'], label='Training')\nplt.plot(epoch_range, history['val_loss'], label='Validation')\nplt.xlabel('Epoch'); plt.ylabel('Loss'); plt.title('Loss')\nplt.legend()\nplt.subplot(1,3,2)\nplt.plot(epoch_range, history['accuracy'], label='Training')\nplt.plot(epoch_range, history['val_accuracy'], label='Validation')\nplt.xlabel('Epoch'); plt.ylabel('Accuracy'); plt.title('Accuracy')\nplt.legend()\nplt.subplot(1,3,3)\nplt.plot(epoch_range, history['auc'], label='Training')\nplt.plot(epoch_range, history['val_auc'], label='Validation')\nplt.xlabel('Epoch'); plt.ylabel('AUC'); plt.title('AUC')\nplt.legend()\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-03T14:28:55.554877Z","iopub.execute_input":"2021-12-03T14:28:55.555289Z","iopub.status.idle":"2021-12-03T14:28:56.300291Z","shell.execute_reply.started":"2021-12-03T14:28:55.555255Z","shell.execute_reply":"2021-12-03T14:28:56.299361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.save('LK_HCD_CNN_TLV2_Model.h5')\npickle.dump(history, open(f'LP_HCD_CNN_Model_V2_History.pkl', 'wb'))","metadata":{"execution":{"iopub.status.busy":"2021-12-03T14:29:45.295060Z","iopub.execute_input":"2021-12-03T14:29:45.296004Z","iopub.status.idle":"2021-12-03T14:29:45.492715Z","shell.execute_reply.started":"2021-12-03T14:29:45.295929Z","shell.execute_reply":"2021-12-03T14:29:45.491674Z"},"trusted":true},"execution_count":null,"outputs":[]}]}