{"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/how-to-use-pyvips-offline/*.tar.bz2","metadata":{"execution":{"iopub.status.busy":"2022-08-11T03:03:15.126742Z","iopub.execute_input":"2022-08-11T03:03:15.127577Z","iopub.status.idle":"2022-08-11T03:04:12.945849Z","shell.execute_reply.started":"2022-08-11T03:03:15.127391Z","shell.execute_reply":"2022-08-11T03:04:12.943909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\n\n# Copy Efficientnet source code to a directory with write access\nif os.path.isdir('/kaggle/efficientnet-keras-source-code/')==False:\n    shutil.copytree('/kaggle/input/efficientnet-keras-source-code', '/kaggle/efficientnet-keras-source-code/')\n# Pip install required packages\nos.system(\"pip install -q /kaggle/input/keras-applications/Keras_Applications-1.0.8-py3-none-any.whl\")\nos.system(\"pip install -q /kaggle/efficientnet-keras-source-code\")","metadata":{"execution":{"iopub.status.busy":"2022-08-11T03:04:12.95232Z","iopub.execute_input":"2022-08-11T03:04:12.952886Z","iopub.status.idle":"2022-08-11T03:05:18.436333Z","shell.execute_reply.started":"2022-08-11T03:04:12.95284Z","shell.execute_reply":"2022-08-11T03:05:18.434864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\nimport cv2\nimport pyvips\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport efficientnet.tfkeras as efn\nfrom tensorflow.python.ops.numpy_ops import np_config","metadata":{"execution":{"iopub.status.busy":"2022-08-11T03:05:18.438259Z","iopub.execute_input":"2022-08-11T03:05:18.438689Z","iopub.status.idle":"2022-08-11T03:05:25.93188Z","shell.execute_reply.started":"2022-08-11T03:05:18.438653Z","shell.execute_reply":"2022-08-11T03:05:25.930639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class cfg:\n    data_dir = '../input/mayo-clinic-strip-ai/'\n    tiles = 49\n    image_size = 300","metadata":{"execution":{"iopub.status.busy":"2022-08-11T03:05:25.935017Z","iopub.execute_input":"2022-08-11T03:05:25.935933Z","iopub.status.idle":"2022-08-11T03:05:25.942144Z","shell.execute_reply.started":"2022-08-11T03:05:25.935873Z","shell.execute_reply":"2022-08-11T03:05:25.94097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(cfg.data_dir+'test.csv')\nimage_ids = test_df['image_id'].values\nfile_paths = []\nfor image_id in image_ids:\n    path = cfg.data_dir + f'test/{image_id}.tif'\n    file_paths.append(path)\n    \ntest_df['file_path'] = file_paths\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-08-11T03:05:25.944277Z","iopub.execute_input":"2022-08-11T03:05:25.945327Z","iopub.status.idle":"2022-08-11T03:05:26.011509Z","shell.execute_reply.started":"2022-08-11T03:05:25.945254Z","shell.execute_reply":"2022-08-11T03:05:26.010385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MCSAIdataset(tf.keras.utils.Sequence):\n    def __init__(self,cfg,df):\n        self.cfg = cfg\n        self.df = df\n        self.crop_size = 300\n        self.max_size = 20000 \n        self.image_ids = df['image_id'].values\n        self.file_paths = df['file_path'].values\n        \n        self.format_to_dtype = {\n           'uchar': np.uint8,\n           'char': np.int8,\n           'ushort': np.uint16,\n           'short': np.int16,\n           'uint': np.uint32,\n           'int': np.int32,\n           'float': np.float32,\n           'double': np.float64,\n           'complex': np.complex64,\n           'dpcomplex': np.complex128,\n        }\n    def vips2numpy(self,vi):\n        return np.ndarray(\n            buffer=vi.write_to_memory(),\n            dtype=self.format_to_dtype[vi.format],\n            shape=[vi.height, vi.width, vi.bands])\n    \n    def create_tiles(self,img):\n        sz=self.crop_size\n        N=self.cfg.tiles\n        shape = img.shape\n        pad0,pad1 = (sz - shape[0]%sz)%sz, (sz - shape[1]%sz)%sz\n        img = np.pad(img,[[pad0//2,pad0-pad0//2],[pad1//2,pad1-pad1//2],[0,0]],constant_values=255)\n        img = img.reshape(img.shape[0]//sz,sz,img.shape[1]//sz,sz,3)\n        img = img.transpose(0,2,1,3,4).reshape(-1,sz,sz,3)\n        if len(img) < N:\n            img = np.pad(img,[[0,N-len(img)],[0,0],[0,0],[0,0]],constant_values=255)\n        idxs = np.argsort(img.reshape(img.shape[0],-1).sum(-1))[:N]\n        img = img[idxs]\n        return img\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self,idx):\n        file_path = self.file_paths[idx]\n        image = pyvips.Image.thumbnail(file_path,self.max_size)\n        image = self.vips2numpy(image)\n        tiles_ = self.create_tiles(image)\n        n_row_tiles = int(np.sqrt(self.cfg.tiles))\n        images = np.zeros((self.cfg.image_size * n_row_tiles, self.cfg.image_size * n_row_tiles, 3))\n        for h in range(n_row_tiles):\n            for w in range(n_row_tiles):\n                i = h * n_row_tiles + w\n                image = tiles_[i]\n                image = tf.image.per_image_standardization(image)\n                h1 = h * self.cfg.image_size\n                w1 = w * self.cfg.image_size\n                images[h1:h1+self.cfg.image_size, w1:w1+self.cfg.image_size] = image\n                \n        return tf.cast(images,tf.float32).reshape(-1,2100,2100,3)\n    \n    def __call__(self):\n        for i in range(self.__len__()):\n            yield self.__getitem__(i)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T03:05:26.013053Z","iopub.execute_input":"2022-08-11T03:05:26.013704Z","iopub.status.idle":"2022-08-11T03:05:26.035254Z","shell.execute_reply.started":"2022-08-11T03:05:26.013663Z","shell.execute_reply":"2022-08-11T03:05:26.033826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np_config.enable_numpy_behavior()\ntest_dataset = MCSAIdataset(cfg,test_df)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T03:05:26.037519Z","iopub.execute_input":"2022-08-11T03:05:26.038317Z","iopub.status.idle":"2022-08-11T03:05:26.049843Z","shell.execute_reply.started":"2022-08-11T03:05:26.038273Z","shell.execute_reply":"2022-08-11T03:05:26.048537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model('../input/mc-strip-ai-training/saved_models/model_f4.hdf5',compile=False)\npreds = model.predict(test_dataset)\npreds","metadata":{"execution":{"iopub.status.busy":"2022-08-11T03:05:26.051651Z","iopub.execute_input":"2022-08-11T03:05:26.052893Z","iopub.status.idle":"2022-08-11T03:08:35.726378Z","shell.execute_reply.started":"2022-08-11T03:05:26.052848Z","shell.execute_reply":"2022-08-11T03:08:35.725025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_id = test_df['patient_id'].values\nsub = pd.DataFrame({ \"patient_id\" : patient_id,\"CE\" : preds[:,0], \"LAA\" : preds[:,1],}).groupby(\"patient_id\").mean().reset_index()\nsub","metadata":{"execution":{"iopub.status.busy":"2022-08-11T03:08:35.728314Z","iopub.execute_input":"2022-08-11T03:08:35.728709Z","iopub.status.idle":"2022-08-11T03:08:35.768551Z","shell.execute_reply.started":"2022-08-11T03:08:35.728677Z","shell.execute_reply":"2022-08-11T03:08:35.767269Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-11T03:08:35.773754Z","iopub.execute_input":"2022-08-11T03:08:35.774805Z","iopub.status.idle":"2022-08-11T03:08:35.7869Z","shell.execute_reply.started":"2022-08-11T03:08:35.77476Z","shell.execute_reply":"2022-08-11T03:08:35.785846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}