{"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":"#Define todas as bibliotecas que serão posteriormente utilizadas\n\n#pacotes\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport os\nimport cv2\nimport concurrent.futures\nimport tensorflow as tf\n\n#pacote de leitura dos arquivos dcm\nimport pydicom\n\n#Pacote para salvar as imagens como .zip\nfrom IPython.display import FileLink\n\n#outros imports\nfrom sklearn import model_selection as sk_model_selection\nfrom sklearn.metrics import roc_curve\nfrom sklearn.metrics import roc_auc_score\nfrom operator import itemgetter\nfrom scipy import ndimage\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.utils import Sequence\nfrom tensorflow.keras import layers\nfrom tensorflow.keras import activations\nfrom tensorflow.keras import Model\nfrom tensorflow.keras.callbacks import EarlyStopping","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-17T01:09:25.427083Z","iopub.execute_input":"2021-12-17T01:09:25.427588Z","iopub.status.idle":"2021-12-17T01:09:31.226580Z","shell.execute_reply.started":"2021-12-17T01:09:25.427500Z","shell.execute_reply":"2021-12-17T01:09:31.225808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Getting to know the data","metadata":{}},{"cell_type":"code","source":"#Lista o conteudo das pastas usadas\n!ls ../input/rsna-miccai-brain-tumor-radiogenomic-classification/\n!ls ../input/rsna-miccai-png/train/","metadata":{"execution":{"iopub.status.busy":"2021-12-17T01:09:31.228497Z","iopub.execute_input":"2021-12-17T01:09:31.228780Z","iopub.status.idle":"2021-12-17T01:09:32.621942Z","shell.execute_reply.started":"2021-12-17T01:09:31.228732Z","shell.execute_reply":"2021-12-17T01:09:32.621042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Original da baseline\n#Definindo os exames problemáticos do conjunto de dados\nEXCLUDED_STR = ['00109', '00123', '00709']\nEXCLUDED_INT = [109, 123, 709]\n\n#Definindo o tamanho das imagens\nIMG_SIZE = 256","metadata":{"execution":{"iopub.status.busy":"2021-12-17T01:09:32.631706Z","iopub.execute_input":"2021-12-17T01:09:32.632264Z","iopub.status.idle":"2021-12-17T01:09:32.637424Z","shell.execute_reply.started":"2021-12-17T01:09:32.632217Z","shell.execute_reply":"2021-12-17T01:09:32.636692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Mostra o header de um dado corte\nds = pydicom.dcmread(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00008/FLAIR/Image-12.dcm\")\nds","metadata":{"execution":{"iopub.status.busy":"2021-12-17T01:09:32.638983Z","iopub.execute_input":"2021-12-17T01:09:32.639531Z","iopub.status.idle":"2021-12-17T01:09:32.864108Z","shell.execute_reply.started":"2021-12-17T01:09:32.639455Z","shell.execute_reply":"2021-12-17T01:09:32.863361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Acessa um dado do header. Debugging\nprint(ds.ImagePositionPatient[2])\nelem = ds[0x0020, 0x0013].value\nprint(type(elem))\nprint(elem)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T01:09:32.865811Z","iopub.execute_input":"2021-12-17T01:09:32.866353Z","iopub.status.idle":"2021-12-17T01:09:32.872881Z","shell.execute_reply.started":"2021-12-17T01:09:32.866312Z","shell.execute_reply":"2021-12-17T01:09:32.872057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Original da baseline:\n#Lendo dataset \ntrain_df = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\")\ntrain_df = train_df[~train_df['BraTS21ID'].isin(EXCLUDED_INT)]\ntrain_df.head(50)\n\n#Criando variavel com id do paciente\ntrain_df['BraTS21ID5'] = [format(x, '05d') for x in train_df.BraTS21ID]\nprint(len(train_df))\ntrain_df = train_df[:int(len(train_df))]\n\n#Debugging: usando 1/10 do dataset\n#train_df = train_df[:int(len(train_df)/10)]\n\n#Mostra o header\ntrain_df.head(3)\nprint(train_df.head(3))","metadata":{"execution":{"iopub.status.busy":"2021-12-17T01:09:32.874629Z","iopub.execute_input":"2021-12-17T01:09:32.875227Z","iopub.status.idle":"2021-12-17T01:09:32.912372Z","shell.execute_reply.started":"2021-12-17T01:09:32.875127Z","shell.execute_reply":"2021-12-17T01:09:32.911454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Config:\n    INPUT_PATH_DCM = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/'\n    INPUT_PATH_PNG = '../input/rsna-miccai-png/'\n    TENSORBOARD_LOG_DIR = '../working/log_tensorboard/'\n    SEED = 42\n    #These should be removed from the dataset\n    EXCLUDED_STR = ['00109', '00123', '00709']\n    EXCLUDED_INT = [109, 123, 709]\n\n    #Defining target size of image\n    IMG_SIZE = 224\n    NUM_SLICES_3D = 64\n    MIN_SLICES = 12\n    \n    BATCH_SIZE = 64\n    \n    CLASS_MODE = 'binary'\n    COLOR_MODE = 'rgb'\n    TARGET_SIZE = (224, 224)\n    def __self__():\n        pass\n    @staticmethod\n    def set_seed(seed_val):\n        tf.random.set_seed(seed_val)\n        random.seed(seed_val)\n        os.environ['PYTHONHASHSEED'] = str(seed_val)\n        np.random.seed(seed_val)\n        ","metadata":{"execution":{"iopub.status.busy":"2021-12-17T01:16:44.485236Z","iopub.execute_input":"2021-12-17T01:16:44.485506Z","iopub.status.idle":"2021-12-17T01:16:44.492776Z","shell.execute_reply.started":"2021-12-17T01:16:44.485478Z","shell.execute_reply":"2021-12-17T01:16:44.491833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Divisao estratificada em treino, teste e validação\ndf = train_df\ndf_trainval, df_test = sk_model_selection.train_test_split(\n    df, \n    test_size=0.15, \n    random_state=Config.SEED, \n    stratify=df[\"MGMT_value\"],\n)\ndf_train, df_val = sk_model_selection.train_test_split(\n    df_trainval, \n    test_size=0.2, \n    random_state=Config.SEED, \n    stratify=df_trainval[\"MGMT_value\"],\n)\n\nprint(df_train.shape)\nprint(df_val.shape)\nprint(df_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T01:16:45.380567Z","iopub.execute_input":"2021-12-17T01:16:45.381099Z","iopub.status.idle":"2021-12-17T01:16:45.398452Z","shell.execute_reply.started":"2021-12-17T01:16:45.381066Z","shell.execute_reply":"2021-12-17T01:16:45.397640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Divisao estratificada em treino, teste e validação\n#df_trainval, df_test = sk_model_selection.train_test_split(\n#    df, \n#    test_size=0.15, \n#    random_state=Config.SEED, \n#    stratify=df[\"MGMT_value\"],\n#)\n#df_train, df_val = sk_model_selection.train_test_split(\n#    df_trainval, \n#    test_size=0.2, \n#    random_state=Config.SEED, \n#    stratify=df_trainval[\"MGMT_value\"],\n#)\n\n#print(df_train.shape)\n#print(df_val.shape)\n#print(df_test.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T01:16:48.259658Z","iopub.execute_input":"2021-12-17T01:16:48.260155Z","iopub.status.idle":"2021-12-17T01:16:48.264520Z","shell.execute_reply.started":"2021-12-17T01:16:48.260120Z","shell.execute_reply":"2021-12-17T01:16:48.263825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize_img(img, option = 'minmax'):\n        \n    if option == 'minmax' :\n        mi = np.min( img.ravel() )\n        ma = np.max( img.ravel() )\n\n        img = ( img - mi ) / ( ma - mi )\n        img = 255 * img \n    if option == 'std' :\n        print('not implemented')       \n    img = img.astype(np.uint8)\n    \n    return img\n\ndef resize_volume(img, target_x, target_y, target_z):\n    # Set the desired depth\n    desired_depth = target_z\n    desired_width = target_x\n    desired_height = target_y\n    # Get current depth\n    current_depth = img.shape[-1]\n    current_width = img.shape[0]\n    current_height = img.shape[1]\n    # Compute depth factor\n    depth_ratio  = current_depth / desired_depth\n    width_ratio  = current_width / desired_width\n    height_ratio = current_height / desired_height\n    depth_factor  = 1 / depth_ratio\n    width_factor  = 1 / width_ratio\n    height_factor = 1 / height_ratio\n    # Rotate\n    #img = ndimage.rotate(img, 90, reshape=False)\n    # Resize across z-axis\n    img = ndimage.zoom(img, (width_factor, height_factor, depth_factor), order=1)\n    return img\n\ndef read_patient_dcm( p_id = '00000', t_type = 'FLAIR', dim = (32,32,32) , norm = None):\n    base = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/'\n    path = base + '/' + p_id + '/' + t_type + '/'\n    path, dirs, files = next(os.walk(path))\n    file_count = len(files)\n    path_list = [0]*(file_count+1)\n    \n    (target_x,target_y,target_z) = dim\n    \n    dcms = []\n    \n    for (i,file) in zip(range(len(path_list)),files):   \n        #print(file)\n        path_list[i+1] = path + file\n        #print( path_list[i+1] )\n        try:\n            path_list[i] = path + file\n            #print( path_list[i+1] )\n            i_dcm = pydicom.dcmread(path_list[i+1])\n            #plt.imshow(pydicom.dcmread(path_list[i+1]).pixel_array, cmap=plt.cm.bone)\n            #plt.show()\n            #print(path_list[i+1])\n            \n            img = i_dcm.pixel_array\n            dcms.append( [i_dcm[0x0020, 0x0013].value , img] )\n        except:\n            pass\n    \n    dcms = sorted(dcms,key=itemgetter(0))\n    \n    slices = []\n    for i in range(len(dcms)):\n        slices.append( dcms[i][1])\n    arr3d = np.stack(slices)\n       \n    arr3d = resize_volume(arr3d,target_x,target_y,target_z)\n    \n    if norm is not None:\n        arr3d = normalize_img( arr3d , norm )\n    \n    return arr3d\n\ndef load_slices(path):\n    filelist = os.listdir(path)\n    filelist = [s[6:] for s in filelist]\n    filelist = sorted(filelist,key=lambda x: int(os.path.splitext(x)[0]))\n    \n    imgs = [mpimg.imread(path + '/Image-' + s) for s in (filelist)]\n    \n    \n    #sorting ?\n    #dcms.sort(key = lambda x: int(x[0x0020, 0x0013].value ))\n\n    slices = imgs # [item.pixel_array for item in dcms]\n    return slices\n\n\ndef read_patient_png_3d( p_id = '00000', t_type = 'FLAIR', dim = (IMG_SIZE,IMG_SIZE,IMG_SIZE) , norm = None):\n    base = '../input/rsna-miccai-png/train/'\n    path = base + '/' + p_id + '/' + t_type + '/'\n    path, dirs, files = next(os.walk(path))\n\n    file_count = len(files)\n    path_list = [0]*(file_count+1)\n    \n    (target_x,target_y,target_z) = dim\n    \n    slices = load_slices(path)\n    arr3d = np.stack(slices)\n       \n    arr3d = resize_volume(arr3d,target_x,target_y,target_z)\n        \n    return arr3d\n\n\ndef read_patient_dcm_2d( p_id = '00000', t_type = 'FLAIR', dim = (32,32,32) , norm = None):\n    (target_x , target_y , target_z) = dim\n    base = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/'\n    path = base + '/' + p_id + '/' + t_type + '/'\n   \n    slices = load_slices(path)\n    arr3d = np.stack(slices)\n       \n    arr3d = resize_volume(arr3d,target_x,target_y,target_z)\n    \n    if norm is not None:\n        arr3d = normalize_img( arr3d , norm )\n    \n    #Calcular RMS pra retonar o melhor slice\n    max_rms = 0\n    i_max_rms = 0\n    #for i in range(target_x):\n    for i in range(target_y):\n        #i_slice = arr3d[i,:,:]\n        i_slice = arr3d[:,i,:]\n        rms = np.sqrt(np.mean(i_slice))\n        if rms > max_rms:\n            max_rms = rms\n            i_max_rms = i\n            \n    arr2d = arr3d[:,i_max_rms,:]\n    #arr2d = arr3d[i_max_rms,:,:]\n    #print(np.shape(arr2d))\n    return arr2d\n\ndef get_patient_list():\n    path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/'\n    \n    _, dirs, _ = next(os.walk(path))\n\n    patients = sorted(dirs)\n\n    for p in EXCLUDED_STR:\n        patients.remove(p)\n        \n    return patients\n\ndef print_from_list( dcms):\n    for (i,ds) in dcms:\n        plt.imshow(ds, cmap=plt.cm.bone)\n        plt.show()\n        \ndef retrieve_labels():\n    df = pd.read_csv ('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv')\n    df = df[~df.BraTS21ID5.isin(EXCLUDED_INT)]\n    y = df['MGMT_value']\n    return y\n\ndef create_feat_vec( img ):\n    arr = img.ravel()\n    hist = np.histogram(arr,bins = 255)\n    return hist\n    \n\ndef read_patient_png_2d( p_id = '00000', t_type = 'FLAIR', dim = (IMG_SIZE,IMG_SIZE,IMG_SIZE) , norm = None):\n    base = '../input/rsna-miccai-png/train/'\n    path = base + '/' + p_id + '/' + t_type + '/'\n    path, dirs, files = next(os.walk(path))\n\n    file_count = len(files)\n    path_list = [0]*(file_count+1)\n    \n    (target_x,target_y,target_z) = dim\n    \n    slices = load_slices(path)\n    arr3d = np.stack(slices)\n       \n    arr3d = resize_volume(arr3d,target_x,target_y,target_z)\n    \n    #if norm is not None:\n    #    arr3d = normalize_img( arr3d , norm )\n    \n    #Calcular RMS pra retonar o melhor slice\n    max_rms = 0\n    i_max_rms = 0\n    #for i in range(target_x):\n    for i in range(target_y):\n        #i_slice = arr3d[i,:,:]\n        i_slice = arr3d[:,i,:]\n        rms = np.sqrt(np.mean(i_slice))\n        if rms > max_rms:\n            max_rms = rms\n            i_max_rms = i\n            \n    arr2d = arr3d[:,i_max_rms,:]\n    \n    return arr2d","metadata":{"execution":{"iopub.status.busy":"2021-12-17T01:16:58.192692Z","iopub.execute_input":"2021-12-17T01:16:58.193004Z","iopub.status.idle":"2021-12-17T01:16:58.263091Z","shell.execute_reply.started":"2021-12-17T01:16:58.192972Z","shell.execute_reply":"2021-12-17T01:16:58.258745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Mostra agora mais imagens para diferentes pacientes, mas mudando o shape dos cortes\n\narr  = read_patient_dcm('00000','FLAIR',(IMG_SIZE,IMG_SIZE,IMG_SIZE))\nprint(np.shape(arr))\n\nfig, ax = plt.subplots(figsize=(3,3))\nplt.imshow(arr[:,IMG_SIZE//2,:],cmap=plt.cm.afmhot,aspect='auto')\n\nfig, ax = plt.subplots(figsize=(3,3))\nplt.imshow(arr[:,:,IMG_SIZE//2],cmap=plt.cm.afmhot,aspect='auto')\n\nfig, ax = plt.subplots(figsize=(3,3))\nplt.imshow(arr[IMG_SIZE//2,:,:],cmap=plt.cm.afmhot,aspect='auto')","metadata":{"execution":{"iopub.status.busy":"2021-12-17T01:16:58.268797Z","iopub.execute_input":"2021-12-17T01:16:58.269068Z","iopub.status.idle":"2021-12-17T01:17:04.279506Z","shell.execute_reply.started":"2021-12-17T01:16:58.269034Z","shell.execute_reply":"2021-12-17T01:17:04.278714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Agora, com a função read_patient_dcm_2d\nfor id in  ['00000', '00002', '00003','00009','00789','00044','00740','00360']:\n    arr  = read_patient_png_2d(id,'T2w',(IMG_SIZE,IMG_SIZE,IMG_SIZE))\n    fig, ax = plt.subplots(figsize=(3,3))\n    plt.imshow(arr,cmap=plt.cm.afmhot,aspect='auto')\nprint(np.shape(arr))","metadata":{"execution":{"iopub.status.busy":"2021-12-17T01:17:04.281050Z","iopub.execute_input":"2021-12-17T01:17:04.281334Z","iopub.status.idle":"2021-12-17T01:17:27.583267Z","shell.execute_reply.started":"2021-12-17T01:17:04.281296Z","shell.execute_reply":"2021-12-17T01:17:27.582436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Definindo o gerador de dados","metadata":{}},{"cell_type":"code","source":"class DataGenerator_2d_png(Sequence):\n    'Generates data for Keras'\n    def __init__(self, list_IDs, labels, batch_size=4, dim=(IMG_SIZE,IMG_SIZE), n_channels=1,\n                 n_classes=2, shuffle=True, data_type = 'train',):\n        'Initialization'\n        self.dim = dim\n        self.batch_size = batch_size\n        self.labels = labels\n        self.list_IDs = list_IDs\n        self.n_channels = n_channels\n        self.n_classes = n_classes\n        self.shuffle = shuffle\n        self.datatype = data_type\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        indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]\n\n        # Find list of IDs\n        list_IDs_temp = [self.list_IDs[k] for k in indexes]\n        list_labels_temp = [self.labels[k] for k in indexes]\n\n        # Generate data\n        X, y = self.__data_generation(list_IDs_temp, list_labels_temp)\n\n        return X, y\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 __data_generation(self, list_IDs_temp, list_labels_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, self.n_channels))\n        y = np.empty((self.batch_size), dtype=int)\n        # Generate data\n        \n        for i, ID in enumerate(list_IDs_temp):\n            img = read_patient_png_2d( p_id = ID, t_type = 'FLAIR', dim = (IMG_SIZE,IMG_SIZE,IMG_SIZE), norm = 'minmax')\n            img = np.expand_dims(img, -1)\n            X[i,] = img\n            y[i] = list_labels_temp[i]\n            \n        return X, y","metadata":{"execution":{"iopub.status.busy":"2021-12-17T01:17:27.584612Z","iopub.execute_input":"2021-12-17T01:17:27.584942Z","iopub.status.idle":"2021-12-17T01:17:27.598065Z","shell.execute_reply.started":"2021-12-17T01:17:27.584903Z","shell.execute_reply":"2021-12-17T01:17:27.597094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE = 4\n\ntrain_id = df_train.BraTS21ID5.tolist()\ntrain_labels = df_train.MGMT_value.tolist()\nval_id     = df_val.BraTS21ID5.tolist()\nval_labels = df_val.MGMT_value.tolist()\n\nprint(\"--- Header do treino ---\")\nprint(df_train.head())\nprint(\"--- Header da validação ---\")\nprint(df_val.head())\n\n\ntrain_gen_2d = DataGenerator_2d_png(data_type = 'train', list_IDs = train_id, labels = train_labels,batch_size = BATCH_SIZE)\nvalid_gen_2d = DataGenerator_2d_png(data_type = 'val', list_IDs = val_id, labels = val_labels,batch_size = BATCH_SIZE)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T01:17:27.600597Z","iopub.execute_input":"2021-12-17T01:17:27.601604Z","iopub.status.idle":"2021-12-17T01:17:27.618179Z","shell.execute_reply.started":"2021-12-17T01:17:27.601488Z","shell.execute_reply":"2021-12-17T01:17:27.616951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_resnet = tf.keras.applications.ResNet50(weights='imagenet', include_top=False)\n\nipt = layers.Input(shape=(IMG_SIZE, IMG_SIZE, 1), name=\"input\")\nx = tf.keras.layers.Concatenate()([ipt, ipt, ipt])\nx = tf.cast(x, tf.float32)\nx = tf.keras.applications.resnet50.preprocess_input(x)\nx = model_resnet(x)\nx = layers.GlobalAveragePooling2D()(x)\nx = layers.Dense(512, activation='relu')(x)\nx = layers.Dropout(0.1)(x)\nout = layers.Dense(1, activation='sigmoid')(x)\n\nmodel_ResNet50_2d = Model(inputs=ipt, outputs=out)\nmodel_ResNet50_2d.summary()","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-12-17T01:17:27.619745Z","iopub.execute_input":"2021-12-17T01:17:27.620205Z","iopub.status.idle":"2021-12-17T01:17:33.013518Z","shell.execute_reply.started":"2021-12-17T01:17:27.620157Z","shell.execute_reply":"2021-12-17T01:17:33.012805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train model 2D on dataset\nNUM_EPOCHS = 25\n\nopt = tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9, decay=0.001, nesterov=True)\n\nearly_stopping = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=20, restore_best_weights=True)\nmodel_ResNet50_2d.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])\n\nhistory = model_ResNet50_2d.fit(train_gen_2d, validation_data=valid_gen_2d,\n                    epochs = NUM_EPOCHS,\n                    verbose = 1,\n                    callbacks=[early_stopping],\n                    workers = 4\n                    )","metadata":{"execution":{"iopub.status.busy":"2021-12-17T01:17:33.014964Z","iopub.execute_input":"2021-12-17T01:17:33.015220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print( np.mean( train_labels ))\nprint( np.mean( val_labels ))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_predictions = model_ResNet50_2d.predict(valid_gen_2d)\nval_labels = val_labels[:len(val_predictions)]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Definição do cálculo da curva ROC e da área sob esta curva\n\nns_probs = [0 for _ in range(len(val_labels))]\n\nns_auc = roc_auc_score(val_labels, ns_probs)\nlr_auc = roc_auc_score(val_labels, val_predictions)\nprint('No skill: ROC AUC=%.3f' % (ns_auc))\nprint('CNN: ROC AUC=%.3f' % (lr_auc))\nlr_fpr, lr_tpr, _ = roc_curve(val_labels, val_predictions)\nplt.plot(lr_fpr, lr_tpr, marker='.', label='CNN')\n\nns_fpr, ns_tpr, _ = roc_curve(val_labels, ns_probs)\nplt.plot(ns_fpr, ns_tpr, marker='.', label='No Skill')\n\nplt.xlabel('False Positive Rate')\nplt.ylabel('True Positive Rate')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}