{"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":"# Basic libraries\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# Neural network libraries\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras import layers\nfrom keras.callbacks import EarlyStopping\n\n# Reading images and creating video libraries\nimport cv2\nfrom IPython.display import HTML\nfrom base64 import b64encode\nimport matplotlib.animation as animation\nimport os\n\nimport SimpleITK as sitk\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.preprocessing import MinMaxScaler","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-22T15:21:27.694822Z","iopub.execute_input":"2021-07-22T15:21:27.695418Z","iopub.status.idle":"2021-07-22T15:21:30.577798Z","shell.execute_reply.started":"2021-07-22T15:21:27.695362Z","shell.execute_reply":"2021-07-22T15:21:30.576856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"target = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv')\npreds = pd.read_csv('../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-22T15:21:30.579295Z","iopub.execute_input":"2021-07-22T15:21:30.579869Z","iopub.status.idle":"2021-07-22T15:21:30.597195Z","shell.execute_reply.started":"2021-07-22T15:21:30.57981Z","shell.execute_reply":"2021-07-22T15:21:30.595587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# specify your image path\nviews = ['FLAIR', 'T1w', 'T1wCE', 'T2w']\ndef load_imgs(idx, ignore_zeros=True, train=True):\n    imgs = {}\n    for view in views:\n        save_ds = []\n        if train:\n            dir_path = os.walk(os.path.join(\n            '../input/rsna-miccai-png/train/', idx, view\n        ))\n        else:\n            dir_path = os.walk(os.path.join(\n            '../input/rsna-miccai-png/test/', idx, view\n        ))\n        for path, subdirs, files in dir_path:\n            for name in files:\n                image_path = os.path.join(path, name) \n                ds = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)\n                save_ds.append(np.array(ds))\n        if len(save_ds) == 0:\n            save_ds = np.zeros((1,256,256))\n        imgs[view] = np.array(save_ds)\n    return imgs","metadata":{"execution":{"iopub.status.busy":"2021-07-22T14:44:15.992022Z","iopub.execute_input":"2021-07-22T14:44:15.992499Z","iopub.status.idle":"2021-07-22T14:44:16.170229Z","shell.execute_reply.started":"2021-07-22T14:44:15.992442Z","shell.execute_reply":"2021-07-22T14:44:16.168945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DataGenerator(keras.utils.Sequence):\n    'Generates data for Keras'\n    def __init__(self, list_IDs, labels=None, batch_size=256, dim=(512,512), n_channels=4,\n                 n_classes=2, shuffle=True, is_train=True, augmentation=False):\n        'Initialization'\n        self.dim = dim\n        self.batch_size = batch_size\n        self.labels = labels\n        self.is_train = (labels is not None)\n        self.list_IDs = list_IDs\n        self.n_channels = n_channels\n        self.n_classes = n_classes\n        self.shuffle = shuffle\n        self.augmentation = augmentation\n        self.on_epoch_end()\n\n    def __len__(self):\n        'Denotes the number of batches per epoch'\n        return int(np.floor(len(self.list_IDs) / self.batch_size))\n\n    def __getitem__(self, index):\n        'Generate one batch of data'\n        # Generate indexes of the batch\n        list_IDs_temp = self.list_IDs[index*self.batch_size:(index+1)*self.batch_size]\n\n        X = np.array(self.__data_generation(list_IDs_temp))\n        if self.augmentation:\n            # Add augmentations\n            augmentations = [layers.experimental.preprocessing.RandomFlip(\"horizontal_and_vertical\"),\n                    layers.experimental.preprocessing.RandomFlip(\"horizontal\"),\n                    layers.experimental.preprocessing.RandomFlip(\"vertical\"),\n                    layers.experimental.preprocessing.RandomRotation(0.2, fill_mode='constant'),\n                    layers.experimental.preprocessing.RandomTranslation(height_factor=0.2, width_factor=0.2, fill_mode='constant'),\n                    layers.experimental.preprocessing.RandomZoom(height_factor=(0, 0.3)),\n                    layers.experimental.preprocessing.RandomContrast(factor=0.2)]\n\n            # Add the augmentations to the dataset\n            aux = [X]\n            for j, f in enumerate(augmentations):\n                # Apply the augmentation\n                aux.append(f(X))\n            X = np.concatenate(aux)\n            \n        # Generate data\n        if self.is_train:\n            y = self.labels[index*self.batch_size:(index+1)*self.batch_size]\n            if self.augmentation:\n                y = np.repeat(y, 8)\n            return np.array(X), np.array(y)\n        else:\n            return np.array(X)\n\n    def on_epoch_end(self):\n        'Updates indexes after each epoch'\n        self.indexes = np.arange(len(self.list_IDs))\n        if self.shuffle == True:\n            np.random.shuffle(self.indexes)\n            \n    def __N4BiasFieldCorrection(self, img_):\n        # Removing radiofrequency inhomogeneity using N4 Bias Field Correction \n        inputImage = sitk.GetImageFromArray(img_)\n        maskImage = sitk.GetImageFromArray((img_ > 0.1) * 1)\n        \n        inputImage = sitk.Cast(inputImage, sitk.sitkFloat32)\n        maskImage = sitk.Cast(maskImage, sitk.sitkUInt8)\n        \n        shrinkFactor = 2\n        inputImage = sitk.Shrink(inputImage, [shrinkFactor] * inputImage.GetDimension())\n        maskImage = sitk.Shrink(maskImage, [shrinkFactor] * maskImage.GetDimension())\n    \n        corrector = sitk.N4BiasFieldCorrectionImageFilter()\n        numberFittingLevels = 4\n        maxIter = 100\n        if maxIter is not None:\n            corrector.SetMaximumNumberOfIterations([maxIter]\n                                                   * numberFittingLevels)\n        corrected_image = corrector.Execute(inputImage, maskImage)\n        return sitk.GetArrayFromImage(corrected_image)\n\n    def __data_generation(self, list_IDs_temp):\n        'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)\n        # Initialization\n        X = np.empty((self.batch_size, self.dim[0]//2, self.dim[1]//2, self.n_channels))\n\n        # Generate data\n        for i, ID in enumerate(list_IDs_temp):\n            # Store sample\n            idx = str(ID).zfill(5)\n            imgs = load_imgs(idx, ignore_zeros=False, train=self.is_train)\n            new_imgs = []\n            for ii in range(2):\n                for jj in range(2):\n                    img_ = imgs[views[2*ii+jj]]\n                    img_ = img_[len(img_) // 2]\n                    img_ = cv2.resize(img_, dsize=self.dim, interpolation=cv2.INTER_LINEAR)\n                    img_ = np.array(img_, dtype='float32') \n                    \n                    # sc = MinMaxScaler(feature_range=(0,255))\n                    sc = StandardScaler()\n                    new_imgs.append(sc.fit_transform(self.__N4BiasFieldCorrection(img_)))\n            new_imgs = np.array(new_imgs).transpose(1,2,0)\n            X[i,] = new_imgs\n        \n        return X","metadata":{"execution":{"iopub.status.busy":"2021-07-22T14:44:16.172515Z","iopub.execute_input":"2021-07-22T14:44:16.172986Z","iopub.status.idle":"2021-07-22T14:44:16.203144Z","shell.execute_reply.started":"2021-07-22T14:44:16.172938Z","shell.execute_reply":"2021-07-22T14:44:16.201845Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DataGeneratorMinMax(keras.utils.Sequence):\n    'Generates data for Keras'\n    def __init__(self, list_IDs, labels=None, batch_size=256, dim=(512,512), n_channels=4,\n                 n_classes=2, shuffle=True, is_train=True, augmentation=False):\n        'Initialization'\n        self.dim = dim\n        self.batch_size = batch_size\n        self.labels = labels\n        self.is_train = (labels is not None)\n        self.list_IDs = list_IDs\n        self.n_channels = n_channels\n        self.n_classes = n_classes\n        self.shuffle = shuffle\n        self.augmentation = augmentation\n        self.on_epoch_end()\n\n    def __len__(self):\n        'Denotes the number of batches per epoch'\n        return int(np.floor(len(self.list_IDs) / self.batch_size))\n\n    def __getitem__(self, index):\n        'Generate one batch of data'\n        # Generate indexes of the batch\n        list_IDs_temp = self.list_IDs[index*self.batch_size:(index+1)*self.batch_size]\n\n        X = np.array(self.__data_generation(list_IDs_temp))\n        if self.augmentation:\n            # Add augmentations\n            augmentations = [layers.experimental.preprocessing.RandomFlip(\"horizontal_and_vertical\"),\n                    layers.experimental.preprocessing.RandomFlip(\"horizontal\"),\n                    layers.experimental.preprocessing.RandomFlip(\"vertical\"),\n                    layers.experimental.preprocessing.RandomRotation(0.2, fill_mode='constant'),\n                    layers.experimental.preprocessing.RandomTranslation(height_factor=0.2, width_factor=0.2, fill_mode='constant'),\n                    layers.experimental.preprocessing.RandomZoom(height_factor=(0, 0.3)),\n                    layers.experimental.preprocessing.RandomContrast(factor=0.2)]\n\n            # Add the augmentations to the dataset\n            aux = [X]\n            for j, f in enumerate(augmentations):\n                # Apply the augmentation\n                aux.append(f(X))\n            X = np.concatenate(aux)\n            \n        # Generate data\n        if self.is_train:\n            y = self.labels[index*self.batch_size:(index+1)*self.batch_size]\n            if self.augmentation:\n                y = np.repeat(y, 8)\n            return np.array(X), np.array(y)\n        else:\n            return np.array(X)\n\n    def on_epoch_end(self):\n        'Updates indexes after each epoch'\n        self.indexes = np.arange(len(self.list_IDs))\n        if self.shuffle == True:\n            np.random.shuffle(self.indexes)\n            \n    def __N4BiasFieldCorrection(self, img_):\n        # Removing radiofrequency inhomogeneity using N4 Bias Field Correction \n        inputImage = sitk.GetImageFromArray(img_)\n        maskImage = sitk.GetImageFromArray((img_ > 0.1) * 1)\n        \n        inputImage = sitk.Cast(inputImage, sitk.sitkFloat32)\n        maskImage = sitk.Cast(maskImage, sitk.sitkUInt8)\n        \n        shrinkFactor = 2\n        inputImage = sitk.Shrink(inputImage, [shrinkFactor] * inputImage.GetDimension())\n        maskImage = sitk.Shrink(maskImage, [shrinkFactor] * maskImage.GetDimension())\n    \n        corrector = sitk.N4BiasFieldCorrectionImageFilter()\n        numberFittingLevels = 4\n        maxIter = 100\n        if maxIter is not None:\n            corrector.SetMaximumNumberOfIterations([maxIter]\n                                                   * numberFittingLevels)\n        corrected_image = corrector.Execute(inputImage, maskImage)\n        return sitk.GetArrayFromImage(corrected_image)\n\n    def __data_generation(self, list_IDs_temp):\n        'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)\n        # Initialization\n        X = np.empty((self.batch_size, self.dim[0]//2, self.dim[1]//2, self.n_channels))\n\n        # Generate data\n        for i, ID in enumerate(list_IDs_temp):\n            # Store sample\n            idx = str(ID).zfill(5)\n            imgs = load_imgs(idx, ignore_zeros=False, train=self.is_train)\n            new_imgs = []\n            for ii in range(2):\n                for jj in range(2):\n                    img_ = imgs[views[2*ii+jj]]\n                    img_ = img_[len(img_) // 2]\n                    img_ = cv2.resize(img_, dsize=self.dim, interpolation=cv2.INTER_LINEAR)\n                    img_ = np.array(img_, dtype='float32') \n                    \n                    sc = MinMaxScaler(feature_range=(0,255))\n                    # sc = StandardScaler()\n                    new_imgs.append(sc.fit_transform(self.__N4BiasFieldCorrection(img_)))\n            new_imgs = np.array(new_imgs).transpose(1,2,0)\n            X[i,] = new_imgs\n        \n        return X","metadata":{"execution":{"iopub.status.busy":"2021-07-22T14:44:16.204601Z","iopub.execute_input":"2021-07-22T14:44:16.205017Z","iopub.status.idle":"2021-07-22T14:44:16.230439Z","shell.execute_reply.started":"2021-07-22T14:44:16.204979Z","shell.execute_reply":"2021-07-22T14:44:16.229227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dim = (256,256)\ntest_dataset = DataGenerator(preds.BraTS21ID, batch_size=1, dim=dim)\nwith tf.device('/gpu:0'):\n\n    # Create a callback that saves the model's weights\n    checkpoint_path = \"../input/models/effNet2D_median_scaled_aug/\"\n    model1 = tf.keras.models.load_model(checkpoint_path)\n    \n    model1.compile(\n        optimizer='adam', \n        loss='binary_crossentropy',\n        metrics=[keras.metrics.AUC()]\n        )\n    \n    preds1 = model1.predict(test_dataset)\n\n    # Create a callback that saves the model's weights\n    checkpoint_path = \"../input/models/effNet2D_median_scaled/\"\n    model2 = tf.keras.models.load_model(checkpoint_path)\n    \n    model2.compile(\n        optimizer='adam', \n        loss='binary_crossentropy',\n        metrics=[keras.metrics.AUC()]\n        )\n    \n    preds2 = model2.predict(test_dataset)\n    \n    preds_ = (preds1 + preds2) / 2","metadata":{"execution":{"iopub.status.busy":"2021-07-22T14:45:24.547973Z","iopub.execute_input":"2021-07-22T14:45:24.54839Z","iopub.status.idle":"2021-07-22T14:45:32.944042Z","shell.execute_reply.started":"2021-07-22T14:45:24.548354Z","shell.execute_reply":"2021-07-22T14:45:32.942902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds.MGMT_value = preds_\npreds.to_csv('submission.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]}]}