{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport seaborn as sns \nimport matplotlib.pyplot as plt\nimport pydicom # A library that loads dicom(dcm) files \nimport os\nimport glob\nfrom IPython.display import Markdown # we will require this to print Markdown in the console\nfrom tensorflow.keras.applications.resnet50 import ResNet50 \n\nfrom tensorflow.keras.models import Model,Sequential\nfrom tensorflow.keras.layers import Input, GRU\nfrom tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPool2D, MaxPooling2D,BatchNormalization, TimeDistributed, LSTM\nfrom tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping\n\nimport datetime\n\nfrom pathlib import Path\nimport re\nfrom imageio import imread\nimport cv2\nfrom sklearn.model_selection import train_test_split\n","metadata":{"_uuid":"2f022e89-de43-404f-a275-f5c7853756ab","_cell_guid":"7f57fad8-809f-4ee7-ba54-d9b57ee8d15f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2021-09-17T15:15:03.014919Z","iopub.execute_input":"2021-09-17T15:15:03.015302Z","iopub.status.idle":"2021-09-17T15:15:03.024152Z","shell.execute_reply.started":"2021-09-17T15:15:03.015272Z","shell.execute_reply":"2021-09-17T15:15:03.023042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_df = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\"\nsample = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv\"\n ","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:03.050525Z","iopub.execute_input":"2021-09-17T15:15:03.051052Z","iopub.status.idle":"2021-09-17T15:15:03.054709Z","shell.execute_reply.started":"2021-09-17T15:15:03.051019Z","shell.execute_reply":"2021-09-17T15:15:03.054001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### This code has been inspired from https://keeganfdes03.medium.com/making-an-eda-on-medical-images-b823693a517a","metadata":{}},{"cell_type":"code","source":"!pip install celluloid","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:03.055860Z","iopub.execute_input":"2021-09-17T15:15:03.056272Z","iopub.status.idle":"2021-09-17T15:15:10.229803Z","shell.execute_reply.started":"2021-09-17T15:15:03.056241Z","shell.execute_reply":"2021-09-17T15:15:10.228762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from celluloid import Camera","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.231909Z","iopub.execute_input":"2021-09-17T15:15:10.232288Z","iopub.status.idle":"2021-09-17T15:15:10.237175Z","shell.execute_reply.started":"2021-09-17T15:15:10.232254Z","shell.execute_reply":"2021-09-17T15:15:10.235991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(input_df)\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.239100Z","iopub.execute_input":"2021-09-17T15:15:10.239439Z","iopub.status.idle":"2021-09-17T15:15:10.267030Z","shell.execute_reply.started":"2021-09-17T15:15:10.239408Z","shell.execute_reply":"2021-09-17T15:15:10.265942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df = pd.read_csv(sample)\nsample_df","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.269070Z","iopub.execute_input":"2021-09-17T15:15:10.269490Z","iopub.status.idle":"2021-09-17T15:15:10.287086Z","shell.execute_reply.started":"2021-09-17T15:15:10.269449Z","shell.execute_reply":"2021-09-17T15:15:10.286060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom(path):\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array   \n    #print(data.shape)\n    data = data - np.min(data)\n    if np.max(data) != 0:\n        data = data/np.max(data)\n    data = (data*255).astype(np.uint8)   \n    return data    ","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.288397Z","iopub.execute_input":"2021-09-17T15:15:10.289089Z","iopub.status.idle":"2021-09-17T15:15:10.295525Z","shell.execute_reply.started":"2021-09-17T15:15:10.289047Z","shell.execute_reply":"2021-09-17T15:15:10.294883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_sample(ID,path, type_=\"flair\"):\n    plt.figure(figsize=(16,5))\n    data = load_dicom(path)\n    plt.imshow(data)\n    label = train_df[train_df['BraTS21ID'] == ID][\"MGMT_value\"].item()\n    plt.title(str(ID) + \" \" + type_ + \" MGMT_value: \" + str(label))\n    plt.axis(\"off\")   ","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.296775Z","iopub.execute_input":"2021-09-17T15:15:10.297358Z","iopub.status.idle":"2021-09-17T15:15:10.306890Z","shell.execute_reply.started":"2021-09-17T15:15:10.297317Z","shell.execute_reply":"2021-09-17T15:15:10.305951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_sample(0, \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/FLAIR/Image-104.dcm\", \"FLAIR\")","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.308150Z","iopub.execute_input":"2021-09-17T15:15:10.308732Z","iopub.status.idle":"2021-09-17T15:15:10.456608Z","shell.execute_reply.started":"2021-09-17T15:15:10.308688Z","shell.execute_reply":"2021-09-17T15:15:10.455317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.459145Z","iopub.execute_input":"2021-09-17T15:15:10.459464Z","iopub.status.idle":"2021-09-17T15:15:10.470610Z","shell.execute_reply.started":"2021-09-17T15:15:10.459428Z","shell.execute_reply":"2021-09-17T15:15:10.469654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df[~train_df['BraTS21ID'].isin([109, 123, 709])]","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.472465Z","iopub.execute_input":"2021-09-17T15:15:10.473011Z","iopub.status.idle":"2021-09-17T15:15:10.481294Z","shell.execute_reply.started":"2021-09-17T15:15:10.472978Z","shell.execute_reply":"2021-09-17T15:15:10.480238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.482977Z","iopub.execute_input":"2021-09-17T15:15:10.483675Z","iopub.status.idle":"2021-09-17T15:15:10.497735Z","shell.execute_reply.started":"2021-09-17T15:15:10.483548Z","shell.execute_reply":"2021-09-17T15:15:10.496953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_valid, y_train, y_valid = train_test_split(train_df['BraTS21ID'], train_df['MGMT_value'], stratify = train_df['MGMT_value'], random_state = 42, test_size = 0.2)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.499164Z","iopub.execute_input":"2021-09-17T15:15:10.499891Z","iopub.status.idle":"2021-09-17T15:15:10.508902Z","shell.execute_reply.started":"2021-09-17T15:15:10.499794Z","shell.execute_reply":"2021-09-17T15:15:10.508016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(X_train)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.509980Z","iopub.execute_input":"2021-09-17T15:15:10.510249Z","iopub.status.idle":"2021-09-17T15:15:10.521498Z","shell.execute_reply.started":"2021-09-17T15:15:10.510224Z","shell.execute_reply":"2021-09-17T15:15:10.520510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.522780Z","iopub.execute_input":"2021-09-17T15:15:10.523084Z","iopub.status.idle":"2021-09-17T15:15:10.536357Z","shell.execute_reply.started":"2021-09-17T15:15:10.523058Z","shell.execute_reply":"2021-09-17T15:15:10.535221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(X_train)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.537930Z","iopub.execute_input":"2021-09-17T15:15:10.538366Z","iopub.status.idle":"2021-09-17T15:15:10.548154Z","shell.execute_reply.started":"2021-09-17T15:15:10.538317Z","shell.execute_reply":"2021-09-17T15:15:10.547181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_valid.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.549261Z","iopub.execute_input":"2021-09-17T15:15:10.549537Z","iopub.status.idle":"2021-09-17T15:15:10.560132Z","shell.execute_reply.started":"2021-09-17T15:15:10.549512Z","shell.execute_reply":"2021-09-17T15:15:10.559077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.561383Z","iopub.execute_input":"2021-09-17T15:15:10.561718Z","iopub.status.idle":"2021-09-17T15:15:10.578628Z","shell.execute_reply.started":"2021-09-17T15:15:10.561689Z","shell.execute_reply":"2021-09-17T15:15:10.577531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from matplotlib import animation, rc\nrc('animation', html='jshtml')\n\n\ndef create_animation(ims):\n    fig = plt.figure(figsize=(6, 6))\n    plt.axis('off')\n    im = plt.imshow(ims[0], cmap=\"gray\")\n\n    def animate_func(i):\n        im.set_array(ims[i])\n        return [im]\n\n    return animation.FuncAnimation(fig, animate_func, frames = len(ims), interval = 1000//24)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.580023Z","iopub.execute_input":"2021-09-17T15:15:10.580301Z","iopub.status.idle":"2021-09-17T15:15:10.586875Z","shell.execute_reply.started":"2021-09-17T15:15:10.580275Z","shell.execute_reply":"2021-09-17T15:15:10.586124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom_line(path):\n    t_paths = sorted(\n        glob.glob(os.path.join(path, \"*\")), \n        key=lambda x: int(x[:-4].split(\"-\")[-1]),\n    )\n    images = []\n    for filename in t_paths:\n        data = load_dicom(filename)\n        if data.max() == 0:\n            continue\n        images.append(data)\n        \n    return images","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.587894Z","iopub.execute_input":"2021-09-17T15:15:10.588333Z","iopub.status.idle":"2021-09-17T15:15:10.598255Z","shell.execute_reply.started":"2021-09-17T15:15:10.588288Z","shell.execute_reply":"2021-09-17T15:15:10.597264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# images = load_dicom_line(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/T2w\")\n# create_animation(images)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.600521Z","iopub.execute_input":"2021-09-17T15:15:10.601216Z","iopub.status.idle":"2021-09-17T15:15:10.608408Z","shell.execute_reply.started":"2021-09-17T15:15:10.601172Z","shell.execute_reply":"2021-09-17T15:15:10.607503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size = 224","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.609543Z","iopub.execute_input":"2021-09-17T15:15:10.610029Z","iopub.status.idle":"2021-09-17T15:15:10.618136Z","shell.execute_reply.started":"2021-09-17T15:15:10.610001Z","shell.execute_reply":"2021-09-17T15:15:10.617162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def crop_resize_image(img):\n    #if img.shape[0] == 512:\n        #img = crop(img, ((10, 10), (10, 10), (0,0)), copy=False)     \n    img = cv2.resize(img, (size, size)) \n    return img","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.619595Z","iopub.execute_input":"2021-09-17T15:15:10.619910Z","iopub.status.idle":"2021-09-17T15:15:10.627435Z","shell.execute_reply.started":"2021-09-17T15:15:10.619870Z","shell.execute_reply":"2021-09-17T15:15:10.626510Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize(x):\n    dicom = pydicom.read_file(x)\n    data = dicom.pixel_array       \n    #print(data)\n    data = crop_resize_image(data)\n    normalised_data = (data.astype(float) - 128) / 128\n    #plt.imshow(normalised_data)\n    #plt.show()\n    return normalised_data     ","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:10.629420Z","iopub.execute_input":"2021-09-17T15:15:10.629814Z","iopub.status.idle":"2021-09-17T15:15:10.637919Z","shell.execute_reply.started":"2021-09-17T15:15:10.629780Z","shell.execute_reply":"2021-09-17T15:15:10.637076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" \nSIZE = 256\nNUM_IMAGES = 64\ndata_directory = '../input/rsna-miccai-brain-tumor-radiogenomic-classification'\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\ndef load_dicom_image(path, img_size=SIZE, voi_lut=True, rotate=0):\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n        \n    if rotate > 0:\n        rot_choices = [0, cv2.ROTATE_90_CLOCKWISE, cv2.ROTATE_90_COUNTERCLOCKWISE, cv2.ROTATE_180]\n        data = cv2.rotate(data, rot_choices[rotate])\n        \n    data = cv2.resize(data, (img_size, img_size))\n    return data\n\ndef load_dicom_images_3d(scan_id, num_imgs=NUM_IMAGES, img_size=SIZE, mri_type=\"FLAIR\", split=\"train\", rotate=0):\n\n    files = sorted(glob.glob(f\"{data_directory}/{split}/{scan_id}/{mri_type}/*.dcm\"), \n               key=lambda var:[int(x) if x.isdigit() else x for x in re.findall(r'[^0-9]|[0-9]+', var)]) \n    #print(len(files))\n    middle = len(files)//2\n    #print(middle)\n    #print(num_imgs)\n    num_imgs2 = num_imgs//2\n    p1 = max(0, middle - num_imgs2)\n    #print(p1)\n    p2 = min(len(files), middle + num_imgs2)\n    #print(p2)\n    img3d = np.stack([load_dicom_image(f, rotate=rotate) for f in files[p1:p2]]).T \n    if img3d.shape[-1] < num_imgs:\n        n_zero = np.zeros((img_size, img_size, num_imgs - img3d.shape[-1]))\n        img3d = np.concatenate((img3d,  n_zero), axis = -1)\n\n    if np.min(img3d) < np.max(img3d):\n        img3d = img3d - np.min(img3d)\n        img3d = img3d / np.max(img3d)\n\n    return np.expand_dims(img3d,0)\n\n#a = load_dicom_images_3d(\"00000\")\n#print(a)\n#print(a.shape)\n#print(np.min(a), np.max(a), np.mean(a), np.median(a))","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:15:16.280072Z","iopub.execute_input":"2021-09-17T15:15:16.280431Z","iopub.status.idle":"2021-09-17T15:15:16.294664Z","shell.execute_reply.started":"2021-09-17T15:15:16.280401Z","shell.execute_reply":"2021-09-17T15:15:16.293562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generator(source_path, batch_size,y_data):\n    path = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/\"\n    types = [\"FLAIR\", \"T1w\" , \"T1wCE\" , \"T2w\"]\n    \n    run = True\n    i = 0 \n    while run:\n        j = 0\n        batch_data = np.zeros((batch_size*4, SIZE, SIZE, NUM_IMAGES))\n        batch_label = np.zeros(batch_size*4)\n        for folder_num in source_path:  \n            fullfilename = str(folder_num).zfill(5) \n            for t in types:\n                a =load_dicom_images_3d(fullfilename, mri_type=t)  \n                batch_data[j,:,:,:] = a  \n                j+=1 \n            batch_label[i] = y_data.iloc[i]   \n            i+=1\n            print(i)   \n            if (i+1) % batch_size == 0:\n                yield batch_data, batch_label\n                batch_data = np.zeros((batch_size*4, SIZE, SIZE, NUM_IMAGES))\n                batch_label = np.zeros(batch_size*4)\n                j = 0 \n                \n            \n                \n             \n","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:25:00.850224Z","iopub.execute_input":"2021-09-17T15:25:00.850692Z","iopub.status.idle":"2021-09-17T15:25:00.860935Z","shell.execute_reply.started":"2021-09-17T15:25:00.850653Z","shell.execute_reply":"2021-09-17T15:25:00.859546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = generator(X_train, 5, y_train)\nval_generator = generator(X_valid, 5, y_valid)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:25:02.894011Z","iopub.execute_input":"2021-09-17T15:25:02.894388Z","iopub.status.idle":"2021-09-17T15:25:02.898935Z","shell.execute_reply.started":"2021-09-17T15:25:02.894357Z","shell.execute_reply":"2021-09-17T15:25:02.897653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for t, s in train_generator:\n    print(t.shape)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T15:25:04.894227Z","iopub.execute_input":"2021-09-17T15:25:04.894612Z","iopub.status.idle":"2021-09-17T15:25:43.595844Z","shell.execute_reply.started":"2021-09-17T15:25:04.894580Z","shell.execute_reply":"2021-09-17T15:25:43.594002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 1","metadata":{"execution":{"iopub.status.busy":"2021-09-17T13:35:18.920973Z","iopub.execute_input":"2021-09-17T13:35:18.921517Z","iopub.status.idle":"2021-09-17T13:35:18.925408Z","shell.execute_reply.started":"2021-09-17T13:35:18.921483Z","shell.execute_reply":"2021-09-17T13:35:18.9244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# resnet = ResNet50(include_top=False, weights='imagenet', input_shape=(size,size,3))  \n# #cnn = Sequential([resnet])\n# cnn = Sequential()\n# cnn.add(Conv2D(16, 3, input_shape=(size,size, 64)))\n# cnn.add(Conv2D(16,(2,2), strides=(1,1)))\n# cnn.add(BatchNormalization())\n\n# cnn.add(Conv2D(32,(2,2), strides=(1,1)))\n# cnn.add(BatchNormalization()) \n\n# cnn.add(Conv2D(64,(2,2), strides=(1,1)))\n# cnn.add(BatchNormalization()) \n\n# cnn.add(Flatten())\n# cnn.add(Dropout(0.5))\n\n# model= Sequential()\n# model.add(TimeDistributed(cnn, input_shape=(20,size,size,64)))\n# model.add(GRU(16,input_shape=(None,30,256), return_sequences=True))\n# model.add(GRU(8))\n# model.add(Dense(2, activation='softmax')) \n# #model = Model(inputs=input_tensor,outputs=out)\n","metadata":{"execution":{"iopub.status.busy":"2021-09-17T13:41:40.417648Z","iopub.execute_input":"2021-09-17T13:41:40.418053Z","iopub.status.idle":"2021-09-17T13:41:44.040708Z","shell.execute_reply.started":"2021-09-17T13:41:40.418017Z","shell.execute_reply":"2021-09-17T13:41:44.039719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, GRU, Dropout, Flatten, BatchNormalization, Activation\nfrom keras.layers.convolutional import Conv3D, MaxPooling3D\nfrom keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\nfrom keras import optimizers\n\nmodel = Sequential()\nmodel.add(Conv3D(64, (3,3,3), strides=(1,1,1), padding='same', input_shape=(20,size,size,64)))\nmodel.add(BatchNormalization())\nmodel.add(Activation('elu'))\nmodel.add(MaxPooling3D(pool_size=(2,2,1), strides=(2,2,1)))\n\nmodel.add(Conv3D(128, (3,3,3), strides=(1,1,1), padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('elu'))\nmodel.add(MaxPooling3D(pool_size=(2,2,2), strides=(2,2,2)))\n\n# model.add(Dropout(0.25))\n\nmodel.add(Conv3D(256, (3,3,3), strides=(1,1,1), padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('elu'))\nmodel.add(MaxPooling3D(pool_size=(2,2,2), strides=(2,2,2)))\n\n# model.add(Dropout(0.25))\n\nmodel.add(Conv3D(256, (3,3,3), strides=(1,1,1), padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(Activation('elu'))\nmodel.add(MaxPooling3D(pool_size=(2,2,2), strides=(2,2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(512, activation='elu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(2, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2021-09-17T13:58:38.681535Z","iopub.execute_input":"2021-09-17T13:58:38.682033Z","iopub.status.idle":"2021-09-17T13:58:38.748367Z","shell.execute_reply.started":"2021-09-17T13:58:38.68199Z","shell.execute_reply":"2021-09-17T13:58:38.746248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgd = optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.7, nesterov=True)\nmodel.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['categorical_accuracy'])\nprint (model.summary())","metadata":{"execution":{"iopub.status.busy":"2021-09-17T13:44:40.697226Z","iopub.execute_input":"2021-09-17T13:44:40.697591Z","iopub.status.idle":"2021-09-17T13:44:40.723727Z","shell.execute_reply.started":"2021-09-17T13:44:40.697561Z","shell.execute_reply":"2021-09-17T13:44:40.72282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"curr_dt_time = datetime.datetime.now()","metadata":{"execution":{"iopub.status.busy":"2021-09-17T13:43:40.307056Z","iopub.execute_input":"2021-09-17T13:43:40.307446Z","iopub.status.idle":"2021-09-17T13:43:40.311792Z","shell.execute_reply.started":"2021-09-17T13:43:40.307415Z","shell.execute_reply":"2021-09-17T13:43:40.311045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_name = 'model_init' + '_' + str(curr_dt_time).replace(' ','').replace(':','_') + '/'\n    \nif not os.path.exists(model_name):\n    os.mkdir(model_name)\n        \nfilepath = model_name + 'model-{epoch:05d}-{loss:.5f}-{categorical_accuracy:.5f}-{val_loss:.5f}-{val_categorical_accuracy:.5f}.h5'\n\ncheckpoint = ModelCheckpoint(filepath, monitor='val_loss', verbose=1, save_best_only=False, save_weights_only=False, mode='auto', period=1)\n\nLR = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=2, verbose=1, mode='min', epsilon=0.0001, cooldown=0, min_lr=0.00001)\ncallbacks_list = [checkpoint, LR] ","metadata":{"execution":{"iopub.status.busy":"2021-09-17T13:45:00.136494Z","iopub.execute_input":"2021-09-17T13:45:00.136858Z","iopub.status.idle":"2021-09-17T13:45:00.148786Z","shell.execute_reply.started":"2021-09-17T13:45:00.136828Z","shell.execute_reply":"2021-09-17T13:45:00.147885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit_generator(train_generator, epochs=num_epochs, verbose=1, \n                    callbacks=callbacks_list, validation_data=val_generator \n                    , class_weight=None, workers=1, initial_epoch=0)","metadata":{"execution":{"iopub.status.busy":"2021-09-17T13:46:11.501403Z","iopub.execute_input":"2021-09-17T13:46:11.501915Z","iopub.status.idle":"2021-09-17T13:46:17.461011Z","shell.execute_reply.started":"2021-09-17T13:46:11.501884Z","shell.execute_reply":"2021-09-17T13:46:17.459025Z"},"trusted":true},"execution_count":null,"outputs":[]}]}