{"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 os\n#os.environ[\"OPENCV_IO_MAX_IMAGE_PIXELS\"] = pow(2,40).__str__()\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport gc\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-10-02T21:59:38.263369Z","iopub.execute_input":"2022-10-02T21:59:38.263814Z","iopub.status.idle":"2022-10-02T21:59:38.495548Z","shell.execute_reply.started":"2022-10-02T21:59:38.263712Z","shell.execute_reply":"2022-10-02T21:59:38.49408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!dpkg -i --force-depends /kaggle/input/pyvips-offline-installer/archives/*.deb >/dev/null 2>&1\n!pip3 install /kaggle/input/pyvips-offline-installer/cffi-1.15.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl\n!pip3 install /kaggle/input/pyvips-offline-installer/pycparser-2.21-py2.py3-none-any.whl\n!pip3 install /kaggle/input/pyvips-offline-installer/pyvips-2.2.1.tar.gz\n\n\n#!apt -y install --fix-missing libvips libvips-dev\n#!pip install pyvips\nimport pyvips","metadata":{"execution":{"iopub.status.busy":"2022-10-02T21:59:38.497995Z","iopub.execute_input":"2022-10-02T21:59:38.49839Z","iopub.status.idle":"2022-10-02T22:07:00.849028Z","shell.execute_reply.started":"2022-10-02T21:59:38.498343Z","shell.execute_reply":"2022-10-02T22:07:00.847575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/mayo-clinic-strip-ai/test.csv')\n#df = df[df[\"image_id\"] == \"0bddf9_0\"]\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2022-10-02T22:07:00.852247Z","iopub.execute_input":"2022-10-02T22:07:00.852586Z","iopub.status.idle":"2022-10-02T22:07:00.89612Z","shell.execute_reply.started":"2022-10-02T22:07:00.852555Z","shell.execute_reply":"2022-10-02T22:07:00.894544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\nimport tensorflow as tf\nfrom tensorflow import keras\n#from keras.applications.resnet_v2 import ResNet152V2, preprocess_input\n#from keras.applications.xception import Xception, preprocess_input\n\n#model_names = glob.glob('../input/mayolernedmodels/*.h5')\n#models = [[]] * len(model_names)\n#for i in range(len(model_names)):\n#    models[i] = keras.models.load_model(model_names[i])\nmodel_res152_1 = keras.models.load_model(\"../input/mayopretrainedvarious/resnet152_fold03.h5\")\nmodel_res152_2 = keras.models.load_model(\"../input/mayopretrainedvarious/resnet_fold00.h5\")\nmodel_efB0_1 = keras.models.load_model(\"../input/mayopretrainedvarious/efficientNetB0_fold01.h5\")\nmodel_efB0_2 = keras.models.load_model(\"../input/mayopretrainedvarious/efficientNetB0_fold02.h5\")\nmodel_xception = keras.models.load_model(\"../input/mayopretrainedvarious/xception_fold04.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-10-02T22:07:00.899547Z","iopub.execute_input":"2022-10-02T22:07:00.900134Z","iopub.status.idle":"2022-10-02T22:08:03.434501Z","shell.execute_reply.started":"2022-10-02T22:07:00.90009Z","shell.execute_reply":"2022-10-02T22:08:03.433128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_imgs = 16\npreds = np.zeros(df.shape[0])\nh_size = 512\nw_size = 512\n#img = np.zeros((1, h_size, w_size, 3), dtype=np.uint8)\nfor idx, r in df.iterrows():\n    print(\"processing \" + r[\"image_id\"] + \"...\")\n    fname = '../input/mayo-clinic-strip-ai/test/'+ r[\"image_id\"]+'.tif'\n    out_name_org = 'out/'+r[\"image_id\"]+'.tif'\n    img_vips = pyvips.Image.new_from_file(fname, access='sequential')\n#    img_vips = img_vips.resize(0.25)\n    \n    [H, W, p] = img_vips.height, img_vips.width, img_vips.bands\n\n    # padding\n#    pad_w = (w_size - img_vips.width%w_size)%w_size\n#    pad_h = (w_size - img_vips.height%h_size)%h_size\n#    image = img_vips.embed(\n#        pad_w//2, pad_h//2,\n#        img_vips.width+pad_w, img_vips.height+pad_h,\n#        extend=\"mirror\")\n    \n    i = 0\n    vals = []\n    imgs = []\n    for h in range(H//h_size):\n        for w in range(W//w_size):\n            if i >= max_imgs:\n                break            \n            crop_vips = img_vips.crop(w*w_size, h*h_size, w_size, h_size)\n            if (crop_vips.avg() < 230) and (crop_vips.deviate() > 15):\n#                img[0,:,:,:] = crop_vips.numpy()\n                img = crop_vips.numpy()\n#                img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # to be removed\n                imgs.append(img)\n                i = i+1\n            del crop_vips\n            gc.collect()\n        \n    if len(imgs) == 0:\n        vals.append(0.5)\n    else:\n        imgs = np.stack(imgs)\n        vals.append(model_res152_1.predict(keras.applications.resnet_v2.preprocess_input(imgs)))\n        vals.append(model_res152_2.predict(keras.applications.resnet_v2.preprocess_input(imgs)))\n        vals.append(model_efB0_1.predict(keras.applications.efficientnet.preprocess_input(imgs)))\n        vals.append(model_efB0_2.predict(keras.applications.efficientnet.preprocess_input(imgs)))\n        vals.append(model_xception.predict(keras.applications.xception.preprocess_input(imgs)))\n            \n    preds[idx] = np.array(vals).mean()\n        \n    del img_vips, imgs\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-10-02T22:08:03.436246Z","iopub.execute_input":"2022-10-02T22:08:03.436764Z","iopub.status.idle":"2022-10-02T22:13:51.398934Z","shell.execute_reply.started":"2022-10-02T22:08:03.436721Z","shell.execute_reply":"2022-10-02T22:13:51.397518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_id = df['patient_id'].values\nLAA = preds\nCE = 1- preds\ndata = dict(patient_id=patient_id, CE=CE, LAA=LAA)\nout = pd.DataFrame(data)\nout = out.groupby(\"patient_id\").mean()\n#out.to_csv('submission.csv', index=False)\nout[[\"CE\", \"LAA\"]].round(6).to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-10-02T22:13:51.400642Z","iopub.execute_input":"2022-10-02T22:13:51.401063Z","iopub.status.idle":"2022-10-02T22:13:51.427945Z","shell.execute_reply.started":"2022-10-02T22:13:51.401018Z","shell.execute_reply":"2022-10-02T22:13:51.426576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-10-02T22:17:31.522137Z","iopub.execute_input":"2022-10-02T22:17:31.522943Z","iopub.status.idle":"2022-10-02T22:17:31.550549Z","shell.execute_reply.started":"2022-10-02T22:17:31.522898Z","shell.execute_reply":"2022-10-02T22:17:31.548586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}