{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"'''\nMoedl Used - Xception on Level-2 images with 36 tiles & with Less no. of Class 0 & 1 data\nand rescale\n'''","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.models import load_model\nfrom tensorflow.keras.layers import Dense, Dropout, BatchNormalization\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\n#import tensorflow_addons as tfa\nfrom tensorflow.keras.applications import ResNet152, DenseNet121, InceptionResNetV2, Xception\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":{},"cell_type":"markdown","source":"Error:../input/panda-cancer-l2-sqtile32-v1/train_squaretile_images/28df755fd0a605c432d3342214f8cd65.png\nError:../input/panda-cancer-l2-sqtile32-v1/train_squaretile_images/28e51d6764c6d71b78406be1fb53b76b.png\nError:../input/panda-cancer-l2-sqtile32-v1/train_squaretile_images/28e69ed7b2c2b132b29d50a9ccaf6238.png\nError:../input/panda-cancer-l2-sqtile32-v1/train_squaretile_images/28e7aad04d25346b6e27220e15ffcc11.png\nError:../input/panda-cancer-l2-sqtile32-v1/train_squaretile_images/28eaf92d73381366c785c97a675762a4.png","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"#img = plt.imread(train_images_path + train_df.loc[0]['image_id'] + '.png')\nprint(train_df.loc[0]['image_id'])\nimg = plt.imread('../input/panda-cancer-l2-sqtile32-v1/train_squaretile_images/28df755fd0a605c432d3342214f8cd65.png')\nplt.imshow(img)\nprint(img)","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['reg_prediction'] = 99.0\nnew_img_df.reset_index(inplace = True) \nnew_img_df.info()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Code for Creating New Directories based on classes","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"model = load_model('../input/xceptionregressionmodel/xception-reg-15-0.73.hdf5', compile=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_img_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from PIL import Image\nfrom numpy import asarray\n\nnot_found = []\nfor i, name in enumerate(tqdm(new_img_df['image_path'])):\n    img_path = train_images_path + new_img_df['image_path'][i]\n    try:\n        image = plt.imread(img_path)\n        im1 = image.reshape((1, 768, 768, 3))\n        new_img_df['reg_prediction'][i] = model.predict(im1, batch_size= 1)\n    except:\n        not_found.append(img_path)\n        \n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(new_img_df[new_img_df['reg_prediction']==99.0]))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import seaborn as sns\n\nsns.catplot(x=\"isup_grade\", y=\"reg_prediction\", data=new_img_df);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_img_df.head(30)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_eb2 = load_model('../input/pandaeb2regressionmodel/eb2-reg-15-0.71.hdf5',compile=False)\nnot_found_1 = []\nfor i, name in enumerate(tqdm(new_img_df['image_path'])):\n    img_path = train_images_path + new_img_df['image_path'][i]\n    try:\n        image = plt.imread(img_path)\n        im1 = image.reshape((1, 768, 768, 3))\n        new_img_df['reg_prediction'][i] = model_eb2.predict(im1, batch_size= 1)\n    except:\n        not_found_1.append(img_path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sns.catplot(x=\"isup_grade\", y=\"reg_prediction\", data=new_img_df);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"new_img_df.head(30)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}