{"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":"!conda install /kaggle/input/pyvips-package/*.tar.bz2","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:27:17.456727Z","iopub.execute_input":"2022-10-14T16:27:17.457234Z","iopub.status.idle":"2022-10-14T16:30:16.624409Z","shell.execute_reply.started":"2022-10-14T16:27:17.457136Z","shell.execute_reply":"2022-10-14T16:30:16.622936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport sys\nimport pandas as pd\nimport numpy as np\nimport math\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport pyvips\n\nimport cv2\nimport PIL\n\nimport random\nimport tensorflow as tf\n#print(\"TensorFlow version:\", tf.__version__)\n\nfrom pathlib import Path\nfrom glob import glob\nfrom random import randrange\n\nfrom collections import defaultdict\nfrom openslide import OpenSlide\n\nfrom tensorflow.keras import layers, models, Model, Input\n#print(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))\nfrom skimage.exposure import is_low_contrast\nfrom collections import defaultdict\nfrom skimage.io import imread\nfrom PIL import Image\nfrom pathlib import Path\nfrom tqdm import tqdm\n\nrandom.seed(19)\ntf.random.set_seed(19)","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:30:16.627685Z","iopub.execute_input":"2022-10-14T16:30:16.628117Z","iopub.status.idle":"2022-10-14T16:30:24.203284Z","shell.execute_reply.started":"2022-10-14T16:30:16.628078Z","shell.execute_reply":"2022-10-14T16:30:24.201902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 224\nIMG_CHANNELS = 3","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:30:24.205008Z","iopub.execute_input":"2022-10-14T16:30:24.205698Z","iopub.status.idle":"2022-10-14T16:30:24.211689Z","shell.execute_reply.started":"2022-10-14T16:30:24.205658Z","shell.execute_reply":"2022-10-14T16:30:24.21005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path ='../input/mayo-clinic-strip-ai/'\ntest_path = data_path + 'test/'","metadata":{"tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:30:36.366497Z","iopub.execute_input":"2022-10-14T16:30:36.366968Z","iopub.status.idle":"2022-10-14T16:30:36.373535Z","shell.execute_reply.started":"2022-10-14T16:30:36.36692Z","shell.execute_reply":"2022-10-14T16:30:36.372189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_csv_data = pd.read_csv(data_path + 'test.csv')","metadata":{"tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:30:36.575978Z","iopub.execute_input":"2022-10-14T16:30:36.576767Z","iopub.status.idle":"2022-10-14T16:30:36.596248Z","shell.execute_reply.started":"2022-10-14T16:30:36.576726Z","shell.execute_reply":"2022-10-14T16:30:36.595058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images_mtdt = glob(test_path + \"*\")\nprint(f\"Number of images in a testing set: {len(test_images_mtdt)}\")","metadata":{"_kg_hide-input":true,"tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:30:36.745893Z","iopub.execute_input":"2022-10-14T16:30:36.746299Z","iopub.status.idle":"2022-10-14T16:30:36.758235Z","shell.execute_reply.started":"2022-10-14T16:30:36.746266Z","shell.execute_reply":"2022-10-14T16:30:36.756598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_images_info(images_mtdt, label_df):\n    img_prop = defaultdict(list)\n    for i, path in enumerate(images_mtdt):\n        img_path = images_mtdt[i]\n        image_id = img_path[-12:-4]\n\n        slide = OpenSlide(img_path)\n\n        big_dim = 'none'\n        max_min_dim_ratio = 1.0\n\n        img_width = slide.dimensions[0]\n        img_height = slide.dimensions[1]\n\n        if(img_width > img_height):\n            big_dim = 'width'\n            max_min_dim_ratio = round(img_width/img_height, 2)\n        elif(img_width < img_height):\n            big_dim = 'height'\n            max_min_dim_ratio = round(img_height/img_width, 2)\n\n        img_prop['image_id'].append(image_id)\n        img_prop['width'].append(img_width)\n        img_prop['height'].append(img_height)\n        img_prop['big_dim'].append(big_dim)\n        img_prop['max_min_dim_ratio'].append(max_min_dim_ratio)\n        \n        split_size = round(max_min_dim_ratio)\n        img_prop['split_size'].append(split_size)\n        img_prop['path'].append(img_path)\n        \n        img_info = pd.DataFrame(img_prop)\n        img_info.sort_values(by='image_id', inplace=True)\n        img_info.reset_index(inplace=True, drop=True)\n        img_info = img_info.merge(label_df, on='image_id')\n\n    return img_info","metadata":{"_kg_hide-input":true,"tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:30:36.871342Z","iopub.execute_input":"2022-10-14T16:30:36.871737Z","iopub.status.idle":"2022-10-14T16:30:36.882647Z","shell.execute_reply.started":"2022-10-14T16:30:36.871705Z","shell.execute_reply":"2022-10-14T16:30:36.881472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images_info = get_images_info(test_images_mtdt, test_csv_data)\ntest_images_count = test_images_info['image_id'].nunique()\ntest_images = np.zeros((test_images_count, IMG_SIZE, IMG_SIZE, 3), dtype=np.uint8)","metadata":{"_kg_hide-input":true,"tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:30:37.025956Z","iopub.execute_input":"2022-10-14T16:30:37.026381Z","iopub.status.idle":"2022-10-14T16:30:37.224395Z","shell.execute_reply.started":"2022-10-14T16:30:37.026345Z","shell.execute_reply":"2022-10-14T16:30:37.223192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# function to check if the image contains useful information after the crop.\ndef get_score(img):\n    imgray = cv2.cvtColor(np.asarray(img), cv2.COLOR_BGR2GRAY)\n    ret, thresh = cv2.threshold(imgray, 127, 255, 0)\n    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)\n    #print(len(hierarchy[0]), len(contours))\n    return len(contours)","metadata":{"_kg_hide-input":true,"tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:30:37.226444Z","iopub.execute_input":"2022-10-14T16:30:37.226962Z","iopub.status.idle":"2022-10-14T16:30:37.23472Z","shell.execute_reply.started":"2022-10-14T16:30:37.226916Z","shell.execute_reply":"2022-10-14T16:30:37.233547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def process_tiles(row, last_img_index, score_dict):\n    image_id = row['image_id']\n    \n    width = row['width']\n    height = row['height']\n    big_dim = row['big_dim']\n    split_size = row['split_size']\n    input_test_path = row['path']\n    center_id = row['center_id']\n    patient_id = row['patient_id']\n    image_num = row['image_num']\n    #label = row['label']\n    \n    n_across = 1\n    n_down = 1\n\n    if(split_size == 1):\n        crop_width = width\n        crop_height = height\n    if(big_dim == 'width'):\n        crop_width = width//split_size\n        crop_height = height\n        n_across = split_size\n    else:\n        crop_height = height//split_size\n        crop_width = width\n        n_down = split_size\n\n    selected_tile = None\n    max_tile_score = 0\n\n    vips_img = pyvips.Image.new_from_file(input_test_path, access='sequential')\n\n    for x in range(n_across):\n        for y in range(n_down):\n            vips_tile = None\n            if(split_size > 1):\n                vips_tile = vips_img.crop(x*crop_width, y*crop_height, crop_width, crop_height)\n            else:\n                vips_tile = vips_img\n                \n            vips_tile = vips_tile.thumbnail_image(IMG_SIZE, height=IMG_SIZE, size='force')\n            tile_score = get_score(vips_tile)\n            print('processing slice: ', (x+y) , crop_width, ' X ', crop_height, 'score', tile_score)\n            \n            if(tile_score > max_tile_score):\n                max_tile_score = tile_score\n                selected_tile = vips_tile\n    \n    test_image = selected_tile.numpy()\n    test_images[last_img_index] = test_image\n    score_dict['last_img_index'].append(last_img_index)\n    score_dict['patient_id'].append(patient_id)\n    score_dict['score'].append(max_tile_score)\n    last_img_index += 1\n    \n    vips_img = None\n    return last_img_index","metadata":{"_kg_hide-input":true,"tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:30:37.343635Z","iopub.execute_input":"2022-10-14T16:30:37.344387Z","iopub.status.idle":"2022-10-14T16:30:37.358354Z","shell.execute_reply.started":"2022-10-14T16:30:37.344331Z","shell.execute_reply":"2022-10-14T16:30:37.357152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_tiles():\n    score_dict = defaultdict(list)\n    last_img_index = 0\n    #change to test_images_info\n    for ind, row in test_images_info.iterrows():\n#         if(ind > 280):\n#             continue\n        print('processing image:', row['image_id'], 'index:', str(ind + 1))\n        last_img_index = process_tiles(row, last_img_index, score_dict)\n        print('====================================')\n    print('processed test images:', last_img_index)\n    return pd.DataFrame(score_dict)","metadata":{"_kg_hide-input":true,"tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:30:37.495925Z","iopub.execute_input":"2022-10-14T16:30:37.496354Z","iopub.status.idle":"2022-10-14T16:30:37.50357Z","shell.execute_reply.started":"2022-10-14T16:30:37.49632Z","shell.execute_reply":"2022-10-14T16:30:37.502282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_df = generate_tiles()","metadata":{"_kg_hide-input":true,"tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:30:37.68604Z","iopub.execute_input":"2022-10-14T16:30:37.686453Z","iopub.status.idle":"2022-10-14T16:32:05.309213Z","shell.execute_reply.started":"2022-10-14T16:30:37.686412Z","shell.execute_reply":"2022-10-14T16:32:05.307924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score_df.head()","metadata":{"_kg_hide-input":true,"tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:32:05.311533Z","iopub.execute_input":"2022-10-14T16:32:05.312625Z","iopub.status.idle":"2022-10-14T16:32:05.332953Z","shell.execute_reply.started":"2022-10-14T16:32:05.312566Z","shell.execute_reply":"2022-10-14T16:32:05.331597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(score_df)","metadata":{"_kg_hide-input":true,"tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:37:33.218445Z","iopub.execute_input":"2022-10-14T16:37:33.219976Z","iopub.status.idle":"2022-10-14T16:37:33.227954Z","shell.execute_reply.started":"2022-10-14T16:37:33.219927Z","shell.execute_reply":"2022-10-14T16:37:33.226984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show(image):\n    print(image.shape)\n    plt.figure()\n    plt.imshow(image)\n    plt.colorbar()\n    plt.grid(False)\n    plt.show()","metadata":{"_kg_hide-input":true,"tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:37:33.536231Z","iopub.execute_input":"2022-10-14T16:37:33.537349Z","iopub.status.idle":"2022-10-14T16:37:33.542644Z","shell.execute_reply.started":"2022-10-14T16:37:33.537306Z","shell.execute_reply":"2022-10-14T16:37:33.541479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = 2\nshow(test_images[index])","metadata":{"_kg_hide-input":true,"tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:37:33.906313Z","iopub.execute_input":"2022-10-14T16:37:33.906992Z","iopub.status.idle":"2022-10-14T16:37:34.229001Z","shell.execute_reply.started":"2022-10-14T16:37:33.906955Z","shell.execute_reply":"2022-10-14T16:37:34.22677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(test_images)","metadata":{"_kg_hide-input":true,"tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:37:35.836123Z","iopub.execute_input":"2022-10-14T16:37:35.83654Z","iopub.status.idle":"2022-10-14T16:37:35.84417Z","shell.execute_reply.started":"2022-10-14T16:37:35.836504Z","shell.execute_reply":"2022-10-14T16:37:35.842991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('../input/mayo-clinic-model/')","metadata":{"_kg_hide-input":true,"tags":[],"execution":{"iopub.status.busy":"2022-10-14T16:37:36.08593Z","iopub.execute_input":"2022-10-14T16:37:36.086353Z","iopub.status.idle":"2022-10-14T16:37:36.104247Z","shell.execute_reply.started":"2022-10-14T16:37:36.08632Z","shell.execute_reply":"2022-10-14T16:37:36.102923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_model = models.load_model('../input/mayo-clinic-model/mc_strip_ai.h5')\nnew_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-14T16:37:36.299613Z","iopub.execute_input":"2022-10-14T16:37:36.300058Z","iopub.status.idle":"2022-10-14T16:37:38.354251Z","shell.execute_reply.started":"2022-10-14T16:37:36.30002Z","shell.execute_reply":"2022-10-14T16:37:38.352869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = test_images/255.0","metadata":{"execution":{"iopub.status.busy":"2022-10-14T16:40:18.7684Z","iopub.execute_input":"2022-10-14T16:40:18.76893Z","iopub.status.idle":"2022-10-14T16:40:18.776095Z","shell.execute_reply.started":"2022-10-14T16:40:18.768889Z","shell.execute_reply":"2022-10-14T16:40:18.774586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images_tensor = tf.convert_to_tensor(test_images)\nresult = new_model.predict(test_images_tensor)\nresult = tf.nn.softmax(result)\nresult = np.round(result, 6)\nresult","metadata":{"execution":{"iopub.status.busy":"2022-10-14T16:43:39.466246Z","iopub.execute_input":"2022-10-14T16:43:39.466693Z","iopub.status.idle":"2022-10-14T16:43:39.643345Z","shell.execute_reply.started":"2022-10-14T16:43:39.466661Z","shell.execute_reply":"2022-10-14T16:43:39.641998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame(result, columns=['CE', 'LAA'])\nsubmission_df.insert(0, 'patient_id', score_df['patient_id'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def maxprediction(data_frame):\n    data_frame = data_frame.assign(prob_diff = abs(data_frame['CE'] - data_frame['LAA']))\n    max_diff = data_frame.loc[data_frame['prob_diff'].idxmax()]\n    \n    return  max_diff[['CE', 'LAA']]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = submission_df[['patient_id','CE', 'LAA']].groupby(['patient_id']).apply(maxprediction).reset_index()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index = False)\n!head submission.csv","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}