{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"'''\nMoedl Used - EB0 and Xception\nFull trainnig of twin backbone\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install tensorflow-addons==0.9.1","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Dense, Dropout, BatchNormalization, concatenate\nfrom keras.engine import Layer, InputSpec\nfrom keras import initializers\nfrom keras import regularizers\nfrom keras import constraints\nfrom keras import backend as K\nfrom keras.activations import elu\nfrom keras.optimizers import Adam\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.callbacks import Callback\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom matplotlib import pyplot as plt\nfrom sklearn.metrics import cohen_kappa_score\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.metrics import Metric\nimport tensorflow.keras.backend as K\nimport tensorflow_addons as tfa\nfrom tensorflow.keras.applications import ResNet50, ResNet101, DenseNet121, InceptionResNetV2, Xception\nfrom tensorflow.keras.applications import InceptionV3,ResNet152V2,ResNet50V2\nimport os\nfrom tqdm import tqdm\nimport seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install efficientnet\nimport efficientnet.tfkeras as efn","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/prostate-cancer-grade-assessment/train.csv')\nprint(train_df.shape)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls ../input","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images_path = '../input/panda-cancer-l2-sqtile32-v1/train_squaretile_images/'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img = plt.imread(train_images_path + train_df.loc[0]['image_id'] + '.png')\nplt.imshow(img)\nprint(img.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df[\"image_path\"] = train_df[\"image_id\"].apply(lambda x: x + '.png')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Creating DataFrame for only available images in train directory","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"new_img_df = pd.DataFrame()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_tile_images = list(os.listdir(train_images_path))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(train_tile_images)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in tqdm(range(train_df.shape[0])):\n    if train_df['image_path'][i] in train_tile_images:\n        new_img_df = new_img_df.append(train_df.loc[i])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_img_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.countplot(new_img_df['isup_grade'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_img_df['isup_grade'][:7000].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_img_df[new_img_df['isup_grade'] == 0].sample(frac=0.451)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_img_df[new_img_df['isup_grade'] == 1].sample(frac=0.488)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_train_df = new_img_df[(new_img_df['isup_grade'] != 0) & (new_img_df['isup_grade'] != 1)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_train_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_train_df = new_train_df.append(new_img_df[new_img_df['isup_grade'] == 0].sample(frac=0.451))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_train_df = new_train_df.append(new_img_df[new_img_df['isup_grade'] == 1].sample(frac=0.488))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_train_df = new_train_df.sample(frac=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_train_df.reset_index(inplace = True) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_train_df = new_train_df.drop(['index'],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_train_df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"xtrain, xval, ytrain, yval = train_test_split(new_train_df[\"image_path\"], new_train_df[\"isup_grade\"], test_size = 0.15, stratify = new_train_df[\"isup_grade\"])\n\ndf_train = pd.DataFrame({\"image_path\":xtrain, \"isup_grade\":ytrain})\ndf_val = pd.DataFrame({\"image_path\":xval, \"isup_grade\":yval})\n\ndf_train[\"isup_grade\"] = df_train[\"isup_grade\"].astype('str')\ndf_val[\"isup_grade\"] = df_val[\"isup_grade\"].astype('str')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(df_train.shape) \nprint(df_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"BATCH_SIZE = 4\nimg_size = 768\nEPOCHS = 16\nnb_classes = 6","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"LR_START = 0.00001\nLR_MAX = 0.0001 * 8\nLR_MIN = 0.00001\nLR_RAMPUP_EPOCHS = 3\nLR_SUSTAIN_EPOCHS = 1\nLR_EXP_DECAY = .8\n\ndef lrfn(epoch):\n    if epoch < LR_RAMPUP_EPOCHS:\n        lr = (LR_MAX - LR_START) / LR_RAMPUP_EPOCHS * epoch + LR_START\n    elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:\n        lr = LR_MAX\n    else:\n        lr = (LR_MAX - LR_MIN) * LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS) + LR_MIN\n    return lr\n    \nlr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose=True)\n\nrng = [i for i in range(EPOCHS)]\ny = [lrfn(x) for x in rng]\nplt.plot(rng, y)\nprint(\"Learning rate schedule: {:.3g} to {:.3g} to {:.3g}\".format(y[0], max(y), y[-1]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Input layer to both backbone layer\ninput_layer = tf.keras.Input(shape=(img_size, img_size, 3))\n\ndef get_model():\n    base_model1 =  ResNet152V2(weights='imagenet', include_top=False, pooling='avg', input_tensor = input_layer,\n                            input_shape=(img_size, img_size, 3))\n    # renaming all layer\n    for i, layer in enumerate(base_model1.layers):\n        layer._name = 'm1_' + str(i)\n    \n    x1 = base_model1.output\n    \n    #base_model2 =  ResNet152V2(weights='imagenet', include_top=False, pooling='avg', input_tensor = input_layer,\n     #                        input_shape=(img_size, img_size, 3))\n   \n    \n    base_model2 =  efn.EfficientNetB0(weights='imagenet', include_top=False, pooling='avg', input_tensor = input_layer,\n                             input_shape=(img_size, img_size, 3))\n    # renaming all layer\n    for i, layer in enumerate(base_model2.layers):\n        layer._name = 'm2_' + str(i)\n\n    x2 = base_model2.output\n           \n    x = concatenate([x1,x2])\n    x = Dense(1024, activation='relu')(x)\n    x = Dropout(0.4)(x)\n    x = Dense(512, activation='relu')(x)\n    x = Dropout(0.3)(x)\n    predictions = Dense(nb_classes, activation=\"softmax\")(x)\n    return Model(inputs = input_layer, outputs=predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = get_model()\nmodel.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])\n#model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.utils import plot_model\nplot_model(model, to_file='model.png')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Image Augmentation","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n        rotation_range=90,  # randomly rotate images in the range (degrees, 0 to 180)\n#         horizontal_flip=True,  # randomly flip images\n        vertical_flip=True)  # randomly flip images\n\nvalid_datagen = ImageDataGenerator(\n        rotation_range=90,  # randomly rotate images in the range (degrees, 0 to 180)\n#         horizontal_flip=True,  # randomly flip images\n        vertical_flip=True)   # randomly flip images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator = train_datagen.flow_from_dataframe(dataframe = df_train,\n                                               directory = train_images_path,\n                                               x_col = \"image_path\",\n                                               y_col = \"isup_grade\",\n                                               batch_size = BATCH_SIZE,\n                                               target_size =  (img_size, img_size),\n                                               class_mode = 'categorical')\n\nvalidation_generator = valid_datagen.flow_from_dataframe(dataframe = df_val,\n                                                    directory = train_images_path,\n                                                    x_col = \"image_path\",\n                                                    y_col = \"isup_grade\",\n                                                    batch_size = BATCH_SIZE, \n                                                    target_size = (img_size, img_size),\n                                                    class_mode = 'categorical')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# checkpoint\nfilepath=\"enemble-{epoch:02d}-{val_accuracy:.4f}.hdf5\"\ncheckpoint_callback = tf.keras.callbacks.ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# %%time\nhistory = model.fit_generator(\n            generator = train_generator,\n            steps_per_epoch = (df_train.shape[0] // BATCH_SIZE),\n            epochs=EPOCHS,\n            validation_data = validation_generator , \n            validation_steps = (df_val.shape[0] // BATCH_SIZE),\n            callbacks=[lr_callback, checkpoint_callback]\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\n# cohens_kappa = history.history['cohen_kappa']\n# val_cohens_kappa = history.history['val_cohen_kappa']\n\nepochs = range(len(acc))\n \nplt.plot(epochs, acc, 'b', label='Training acc')\nplt.plot(epochs, val_acc, 'r', label='Validation acc')\nplt.title('Training and validation accuracy')\nplt.legend()\n \nplt.figure()\n \nplt.plot(epochs, loss, 'b', label='Training loss')\nplt.plot(epochs, val_loss, 'r', label='Validation loss')\nplt.title('Training and validation loss')\nplt.legend()\n\nplt.figure()\n \n# plt.plot(epochs, cohens_kappa, 'b', label='Training Cohen-kappa')\n# plt.plot(epochs, val_cohens_kappa, 'r', label='Validation Cohen-kappa')\n# plt.title('Cohen Kappa - Training and validation score')\n# plt.legend()\n\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#model.save('ensemble_model_7k_data.h5')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}