{"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 cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport glob\nimport os\nimport tensorflow as tf\n\nfrom tensorflow.keras.models import Model, load_model\nfrom tqdm import tqdm\n","metadata":{"execution":{"iopub.status.busy":"2021-10-12T14:19:20.632348Z","iopub.execute_input":"2021-10-12T14:19:20.632740Z","iopub.status.idle":"2021-10-12T14:19:26.656427Z","shell.execute_reply.started":"2021-10-12T14:19:20.632618Z","shell.execute_reply":"2021-10-12T14:19:26.654889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img = np.load('/kaggle/input/rsna-miccai-voxel-256-dataset/voxel/train/00000/FLAIR.npy')","metadata":{"execution":{"iopub.status.busy":"2021-10-12T14:19:26.659018Z","iopub.execute_input":"2021-10-12T14:19:26.659251Z","iopub.status.idle":"2021-10-12T14:19:27.193026Z","shell.execute_reply.started":"2021-10-12T14:19:26.659225Z","shell.execute_reply":"2021-10-12T14:19:27.192069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(100,256):\n    plt.imshow(img[i,:,:],cmap='gray')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-12T12:40:39.573397Z","iopub.execute_input":"2021-10-12T12:40:39.57368Z","iopub.status.idle":"2021-10-12T12:41:08.189504Z","shell.execute_reply.started":"2021-10-12T12:40:39.573652Z","shell.execute_reply":"2021-10-12T12:41:08.188727Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DataLoader(tf.keras.utils.Sequence):\n    def __init__(self,base_dir='/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/test',\\\n                mods=['FLAIR']):\n        self.batch_size = 1 \n        self.base_dir = base_dir\n        self.pat_ids = sorted(glob.glob(os.path.join(base_dir, '*')))\n        self.modalities = mods\n        print('PAT IDS:',len(self.pat_ids),' | Modalities:',self.modalities)\n    \n    def __getitem__(self,index):\n        batch_patids = self.pat_ids[index:index+self.batch_size]\n        all_images = {}\n        for K in self.modalities:\n            all_images[K] = {'images':[],'ids':[]}\n        for patid in batch_patids:\n            for MOD in all_images.keys():\n                all_images[MOD]['images'].append(np.load(os.path.join(patid, MOD+'.npy')))\n                all_images[MOD]['ids'].append(patid.replace('\\\\','/').split('/')[-1])\n        return all_images\n   \n    def __len__(self):\n        return int(len(self.pat_ids)/self.batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-10-12T14:19:27.194318Z","iopub.execute_input":"2021-10-12T14:19:27.194525Z","iopub.status.idle":"2021-10-12T14:19:27.205406Z","shell.execute_reply.started":"2021-10-12T14:19:27.194500Z","shell.execute_reply":"2021-10-12T14:19:27.204828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class StageOne:\n    def __init__(self,modelpath='',height=256,width=256,score_min_thresh=0.5):\n        self.model = load_model(modelpath)\n        self.score_min_thresh = score_min_thresh\n        self.height = height\n        self.width = width\n        self.offset_perc = 0.1\n    \n    def infer(self,image_batch,filter_batchsize=16):\n        filtered_images = {}\n        for K in image_batch.keys():\n            filtered_images[K] = {'images':[],'ids':[]}\n            for imagesbatch, patid in zip(image_batch[K]['images'],image_batch[K]['ids']):\n                div,mod = divmod(len(imagesbatch),filter_batchsize)\n                if mod!=0:\n                    div+=1\n                dset = tqdm(range(0,len(imagesbatch),filter_batchsize),total=div,position=0, leave=True)\n                dset.set_description(f'{patid}|Filtering')\n                filtered_batch_images = []\n                for i in dset:\n                    org_batchimgs = imagesbatch[i:i+filter_batchsize]\n                    batchimgs = org_batchimgs\n                    batchimgs = np.array([cv2.resize(img,(self.width,self.height))/255. for img in batchimgs])\n                    out = self.model.predict(batchimgs)\n                    maxindexes = np.argmax(out,axis=1)\n                    for j in range(len(maxindexes)):\n                        if maxindexes[j] == 1 and out[j][maxindexes[j]] >= self.score_min_thresh:\n                            filtered_batch_images.append(org_batchimgs[j])\n                            \n                if len(filtered_batch_images)==0:\n                    offset = math.ceil(len(imagesbatch)*self.offset_perc)\n                    filtered_batch_images = imagesbatch[offset:-offset]\n                filtered_images[K]['images'].append(filtered_batch_images)\n                filtered_images[K]['ids'].append(patid)\n                filtered_batch_images = None\n                dset = None\n                \n            return filtered_images\n              ","metadata":{"execution":{"iopub.status.busy":"2021-10-12T14:19:27.207151Z","iopub.execute_input":"2021-10-12T14:19:27.207698Z","iopub.status.idle":"2021-10-12T14:19:27.221393Z","shell.execute_reply.started":"2021-10-12T14:19:27.207663Z","shell.execute_reply":"2021-10-12T14:19:27.220745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mods = ['FLAIR']\ngenerator = DataLoader(base_dir='/kaggle/input/rsna-miccai-voxel-256-dataset/voxel/train/')\nstage_one = StageOne(modelpath='/kaggle/input/models/FINAL_MODELALL_acc0.9825_ep26.h5')","metadata":{"execution":{"iopub.status.busy":"2021-10-12T14:19:27.222385Z","iopub.execute_input":"2021-10-12T14:19:27.222934Z","iopub.status.idle":"2021-10-12T14:19:42.675444Z","shell.execute_reply.started":"2021-10-12T14:19:27.222900Z","shell.execute_reply":"2021-10-12T14:19:42.674807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dset = tqdm(enumerate(generator),total=len(generator),position=0, leave=True)\ndset.set_description('Loading_test')\nall_results = {}\nfor i,sample in dset:\n    #if i > 2:\n    #    break\n    filtered_images = stage_one.infer(sample)\n    print(np.array(filtered_images['FLAIR']['images']).shape)","metadata":{"execution":{"iopub.status.busy":"2021-10-12T14:20:50.673093Z","iopub.execute_input":"2021-10-12T14:20:50.673438Z","iopub.status.idle":"2021-10-12T14:49:33.544669Z","shell.execute_reply.started":"2021-10-12T14:20:50.673407Z","shell.execute_reply":"2021-10-12T14:49:33.543516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in filtered_images['FLAIR']['images'][0]:\n    plt.imshow(i,cmap='gray')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-12T13:22:39.842315Z","iopub.execute_input":"2021-10-12T13:22:39.842606Z","iopub.status.idle":"2021-10-12T13:23:02.223448Z","shell.execute_reply.started":"2021-10-12T13:22:39.84258Z","shell.execute_reply":"2021-10-12T13:23:02.222657Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample['FLAIR']['images'][0].shape","metadata":{"execution":{"iopub.status.busy":"2021-10-12T13:09:44.965732Z","iopub.execute_input":"2021-10-12T13:09:44.966009Z","iopub.status.idle":"2021-10-12T13:09:44.9713Z","shell.execute_reply.started":"2021-10-12T13:09:44.965981Z","shell.execute_reply":"2021-10-12T13:09:44.970548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}