{"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 numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport gc\nimport time\nfrom IPython.display import clear_output\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.callbacks import ModelCheckpoint as MC\nfrom tensorflow.keras import backend as K\nfrom sklearn.metrics import confusion_matrix, roc_curve, auc, recall_score, accuracy_score, balanced_accuracy_score, precision_score\nimport csv, pickle\nfrom IPython.display import FileLink\n\nroot = '/kaggle/input/rsna-str-pulmonary-embolism-detection'\nfor item in os.listdir(root):\n    path = os.path.join(root, item)\n    if os.path.isfile(path):\n        print(path)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:13:50.701931Z","iopub.execute_input":"2022-01-31T11:13:50.702282Z","iopub.status.idle":"2022-01-31T11:13:55.977049Z","shell.execute_reply.started":"2022-01-31T11:13:50.702182Z","shell.execute_reply":"2022-01-31T11:13:55.976163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Reading train data...')\ndata = pd.read_csv(\"../input/rsna-str-pulmonary-embolism-detection/train.csv\")\nprint(data.shape)\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:14:24.929742Z","iopub.execute_input":"2022-01-31T11:14:24.930006Z","iopub.status.idle":"2022-01-31T11:14:28.658486Z","shell.execute_reply.started":"2022-01-31T11:14:24.929977Z","shell.execute_reply":"2022-01-31T11:14:28.657832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import GroupShuffleSplit\n\n#train,test=train_test_split(data, test_size=0.2, random_state=42, shuffle=False)\n\nsplitter = GroupShuffleSplit(test_size=.20, random_state=42, n_splits=2)\nsplit = splitter.split(data, groups=data.StudyInstanceUID)\ntrain_inds, test_inds = next(split)\n\ntrain = data.iloc[train_inds]\ntest = data.iloc[test_inds]","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:14:34.742224Z","iopub.execute_input":"2022-01-31T11:14:34.742775Z","iopub.status.idle":"2022-01-31T11:14:37.485492Z","shell.execute_reply.started":"2022-01-31T11:14:34.742733Z","shell.execute_reply":"2022-01-31T11:14:37.48475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()\n\nprint('%PE positive images:', round(sum(train.pe_present_on_image)/len(train)*100),'%')\nprint('# of PE positive images:', sum(train.pe_present_on_image))","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:14:47.33911Z","iopub.execute_input":"2022-01-31T11:14:47.339377Z","iopub.status.idle":"2022-01-31T11:14:47.657491Z","shell.execute_reply.started":"2022-01-31T11:14:47.339345Z","shell.execute_reply":"2022-01-31T11:14:47.656591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Separate the labels from the test set and save for later\nkeys=['negative_exam_for_pe', 'qa_motion',\n       'qa_contrast', 'flow_artifact', 'rv_lv_ratio_gte_1', 'rv_lv_ratio_lt_1',\n       'leftsided_pe', 'chronic_pe', 'true_filling_defect_not_pe',\n       'rightsided_pe', 'acute_and_chronic_pe', 'central_pe', 'indeterminate']\n\ny=data['pe_present_on_image'].copy()\nx=data.drop('pe_present_on_image',axis=1)\n\nfor key in keys:\n    y=pd.concat([y,x[key]],axis=1)\n    x=x.drop(key, axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:25:17.821744Z","iopub.execute_input":"2022-01-31T11:25:17.822075Z","iopub.status.idle":"2022-01-31T11:25:19.590937Z","shell.execute_reply.started":"2022-01-31T11:25:17.82204Z","shell.execute_reply":"2022-01-31T11:25:19.589998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()\n\ny_test=test['pe_present_on_image'].copy()\ntest=test.drop('pe_present_on_image',axis=1)\n\nfor key in keys:\n    y_test=pd.concat([y_test,test[key]],axis=1)\n    test=test.drop(key, axis=1)\n    \nprint('%PE positive images:', round(sum(y_test.pe_present_on_image)/len(train)*100),'%\\n',\n      '# of PE positive images:', sum(y_test.pe_present_on_image))","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:25:22.414223Z","iopub.execute_input":"2022-01-31T11:25:22.414477Z","iopub.status.idle":"2022-01-31T11:25:22.809948Z","shell.execute_reply.started":"2022-01-31T11:25:22.414447Z","shell.execute_reply":"2022-01-31T11:25:22.808955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:25:25.920616Z","iopub.execute_input":"2022-01-31T11:25:25.92102Z","iopub.status.idle":"2022-01-31T11:25:25.939912Z","shell.execute_reply.started":"2022-01-31T11:25:25.920969Z","shell.execute_reply":"2022-01-31T11:25:25.938981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize_image(image):\n    min = -1000\n    max = 400\n    image[image < min] = min\n    image[image > max] = max\n    #image = (image - min) / (max - min)\n    #image = image.astype(\"float32\")\n    return image","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:25:29.460949Z","iopub.execute_input":"2022-01-31T11:25:29.461236Z","iopub.status.idle":"2022-01-31T11:25:29.465788Z","shell.execute_reply.started":"2022-01-31T11:25:29.461208Z","shell.execute_reply":"2022-01-31T11:25:29.464909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import vtk\nfrom vtk.util import numpy_support\nimport cv2\n\nreader = vtk.vtkDICOMImageReader()\ndef get_img(path):\n    reader.SetFileName(path)\n    reader.Update()\n    _extent = reader.GetDataExtent()\n    ConstPixelDims = [_extent[1]-_extent[0]+1, _extent[3]-_extent[2]+1, _extent[5]-_extent[4]+1]\n\n    ConstPixelSpacing = reader.GetPixelSpacing()\n    imageData = reader.GetOutput()\n    pointData = imageData.GetPointData()\n    arrayData = pointData.GetArray(0)\n    ArrayDicom = numpy_support.vtk_to_numpy(arrayData)\n    \n    ArrayDicom = ArrayDicom.reshape(ConstPixelDims, order='F')\n    ArrayDicom = cv2.resize(ArrayDicom,(512,512))\n    \n    norm_img = normalize_image(ArrayDicom)\n    \n    \n    return norm_img","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:25:32.213326Z","iopub.execute_input":"2022-01-31T11:25:32.213575Z","iopub.status.idle":"2022-01-31T11:25:33.883136Z","shell.execute_reply.started":"2022-01-31T11:25:32.213546Z","shell.execute_reply":"2022-01-31T11:25:33.882397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test read a dcom file and view it\nfpath = \"../input/rsna-str-pulmonary-embolism-detection/train/6897fa9de148/2bfbb7fd2e8b/be0b7524ffb4.dcm\"\nds = get_img(fpath)\n\nimport matplotlib.pyplot as plt\n\n#Convert dcom file to 8bit color\nfunc = lambda x: int((2**15 + x)*(255/2**16))\nint16_to_uint8 = np.vectorize(func)\n\ndef show_dicom_images(dcom):\n    f, ax = plt.subplots(1,2, figsize=(16,20))\n    data_row_img = int16_to_uint8(ds)\n    ax[0].imshow(data_row_img, cmap=plt.cm.bone)\n    ax[1].imshow(ds, cmap=plt.cm.bone)\n    #print(data_row_img)\n    ax[0].axis('off')\n    ax[0].set_title('8-bit DICOM Image')\n    ax[1].axis('off')\n    ax[1].set_title('16-bit DICOM Image')\n    plt.show()\n    \nshow_dicom_images(ds)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T00:44:40.158034Z","iopub.execute_input":"2022-01-31T00:44:40.158282Z","iopub.status.idle":"2022-01-31T00:44:40.63651Z","shell.execute_reply.started":"2022-01-31T00:44:40.158246Z","shell.execute_reply":"2022-01-31T00:44:40.635755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model Architecture\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.models import Model, Sequential\nfrom tensorflow.keras.layers import Input, Dense, Dropout, Conv2D, MaxPooling2D, Flatten, GlobalAveragePooling2D, Embedding, Dot, Reshape, Multiply\n\n\ninputs = Input((512, 512, 3))\n#inputs = Input((450, 450, 3))\n\nbase_model = keras.applications.Xception(\n    include_top=False,\n    weights=\"imagenet\"\n)\n\nbase_model.trainable = False\n\n# 1st Model - pe, indt\nbase_outputs = base_model(inputs, training=False)\npool_outputs = GlobalAveragePooling2D()(base_outputs) # max pooling?\ndrop_outputs = Dropout(0.25)(pool_outputs)\noutputs = Dense(1024, activation='relu')(drop_outputs)\noutputs = Dense(256, activation='relu')(outputs)\noutputs = Dense(64, activation='relu')(outputs)\nppoi = Dense(1,activation='sigmoid', name='pe_present_on_image')(outputs)\n#indt = Dense(1,activation='sigmoid', name='indeterminate')(outputs)\n\n# 2nd Model - lspe, rspe, cnpe\nbase_outputs = base_model(inputs, training=False)\npool_outputs = GlobalAveragePooling2D()(base_outputs) # max pooling?\ndrop_outputs = Dropout(0.25)(pool_outputs)\noutputs = Dense(1024, activation='relu')(drop_outputs)\noutputs = Dense(256, activation='relu')(outputs)\noutputs = Dense(64, activation='relu')(outputs)\n#pos_pe = Dense(3,activation='softmax', name='pe_position')(outputs)\nlspe = Dense(1,activation='sigmoid',name='leftsided_pe')(outputs)\nrspe = Dense(1,activation='sigmoid',name='rightsided_pe')(outputs)\ncnpe = Dense(1, activation='sigmoid',name='central_pe')(outputs)\n\n# 3rd Model - cpe, (ape), cape\nbase_outputs = base_model(inputs, training=False)\npool_outputs = GlobalAveragePooling2D()(base_outputs) # max pooling?\ndrop_outputs = Dropout(0.25)(pool_outputs)\noutputs = Dense(1024, activation='relu')(drop_outputs)\noutputs = Dense(256, activation='relu')(outputs)\noutputs = Dense(64, activation='relu')(outputs)\n#pos_pe = Dense(3,activation='softmax', name='pe_severity')(outputs)\ncpe = Dense(1,activation='sigmoid',name='chronic_pe')(outputs)\nacpe = Dense(1,activation='sigmoid',name='acute_and_chronic_pe')(outputs)\n\nopt = keras.optimizers.Adam(lr=0.00001)\n\nmodel1 = Model(inputs=inputs, outputs={'pe_present_on_image':ppoi})\n                                      #,'indeterminate':indt})\n\nmodel1.compile(optimizer=opt,\n              loss='binary_crossentropy',\n              metrics=tf.keras.metrics.AUC())\n\nmodel1.summary()\nmodel1.save('is_pe_model.h5')\ndel model1\nK.clear_session()\ngc.collect()\n\n\nmodel2 = Model(inputs=inputs, outputs={'leftsided_pe':lspe,\n                                      'rightsided_pe':rspe,\n                                      'central_pe':cnpe})\n\nmodel2.compile(optimizer=opt,\n              loss='binary_crossentropy',\n              metrics=tf.keras.metrics.AUC())\n\nmodel2.summary()\nmodel2.save('pos_pe_model.h5')\ndel model2\nK.clear_session()\ngc.collect()\n\nmodel3 = Model(inputs=inputs, outputs={'chronic_pe':cpe,\n                                      'acute_and_chronic_pe':acpe})\n\nmodel3.compile(optimizer=opt,\n              loss='binary_crossentropy',\n              metrics=tf.keras.metrics.AUC())\n\nmodel3.summary()\nmodel3.save('sev_pe_model.h5')\ndel model3\nK.clear_session()\ngc.collect() ","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:25:36.558548Z","iopub.execute_input":"2022-01-31T11:25:36.559058Z","iopub.status.idle":"2022-01-31T11:25:42.73263Z","shell.execute_reply.started":"2022-01-31T11:25:36.559022Z","shell.execute_reply":"2022-01-31T11:25:42.731069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_to_rgb(array):\n    array = array.reshape((512, 512, 1))\n    return np.stack([array, array, array], axis=2).reshape((512, 512, 3))\n    #array = array.reshape((450, 450, 1))\n    #return np.stack([array, array, array], axis=2).reshape((450, 450, 3))\n    \ndef custom_dcom_image_generator(batch_size, dataset, model_name, test=False, debug=False):\n    \n    fnames = dataset[['StudyInstanceUID', 'SeriesInstanceUID', 'SOPInstanceUID']]\n    \n    if not test:\n        if model_name=='pe':\n            Y = dataset[['pe_present_on_image']]#,'indeterminate']]\n        elif model_name=='pos':\n            Y = dataset[['leftsided_pe','rightsided_pe','central_pe']]\n        elif model_name=='sev':\n            Y = dataset[['chronic_pe', 'acute_and_chronic_pe']]\n        #Y = dataset[['pe_present_on_image', 'leftsided_pe',\n        #             'chronic_pe', 'rightsided_pe', 'acute_and_chronic_pe', 'central_pe', 'indeterminate'\n        #            ]]\n    \n    prefix = 'input/rsna-str-pulmonary-embolism-detection/train'\n    \n    X = []\n    batch = 0\n    for st, sr, so in fnames.values:\n        if debug:\n            print(f\"Current file: ../{prefix}/{st}/{sr}/{so}.dcm\")\n\n        dicom = get_img(f\"../{prefix}/{st}/{sr}/{so}.dcm\")\n        image = convert_to_rgb(dicom)\n        X.append(image)\n        \n        del st, sr, so\n        \n        #If we reached the end of the batch\n        if len(X) == batch_size:\n            if test:\n                #yield is used to save memory\n                yield np.array(X)\n                del X\n            else:\n                yield np.array(X), Y[batch*batch_size:(batch+1)*batch_size].values\n                del X\n                \n            gc.collect()\n            X = []\n            batch += 1\n        \n    if test:\n        yield np.array(X)\n    else:\n        yield np.array(X), Y[batch*batch_size:(batch+1)*batch_size].values\n        del Y\n    del X\n    gc.collect()\n    return","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:26:02.623074Z","iopub.execute_input":"2022-01-31T11:26:02.62335Z","iopub.status.idle":"2022-01-31T11:26:02.636027Z","shell.execute_reply.started":"2022-01-31T11:26:02.62332Z","shell.execute_reply":"2022-01-31T11:26:02.635174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_pe_model(model_path, train_data, train_size, batch_size, max_train_time, debug):\n    #Train loop\n    for n, (x, y) in enumerate(custom_dcom_image_generator(batch_size, train_data, 'pe', False, debug)):\n        \n        if len(x) < 10: #Tries to filter out empty or short data\n            break\n\n        clear_output(wait=True)\n        print(\"Training batch: %i - %i\" %(batch_size*n, batch_size*(n+1)))\n        model = load_model(model_path)\n        hist = model.fit(\n            x[:train_size], \n            #Y values are in a dict as there's more than one target for training output\n            {'pe_present_on_image':y[:train_size, 0]},#,\n             #'indeterminate':y[:train_size, 1]},\n\n            callbacks = checkpoint,\n\n            validation_split=0.2,\n            epochs=3,\n            batch_size=8,\n            verbose=debug\n        )\n\n        print(\"Metrics for batch validation:\")\n        model.evaluate(x[train_size:],\n                       {'pe_present_on_image':y[train_size:, 0],\n                        'indeterminate':y[train_size:, 1]})\n\n        try:\n            for key in hist.history.keys():\n                history[key] = np.concatenate([history[key], hist.history[key]], axis=0)\n        except:\n            for key in hist.history.keys():\n                history[key] = hist.history[key]\n\n        #To make sure that our model don't train overtime\n        if time.time() - start >= max_train_time:\n            print(\"Time's up!\")\n            break\n\n        model.save('is_pe_model.h5')\n        del model, x, y, hist\n        K.clear_session()\n        gc.collect()\n    \n    return history, model","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:26:24.884253Z","iopub.execute_input":"2022-01-31T11:26:24.884523Z","iopub.status.idle":"2022-01-31T11:26:24.894506Z","shell.execute_reply.started":"2022-01-31T11:26:24.884491Z","shell.execute_reply":"2022-01-31T11:26:24.893854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = {}\nstart = time.time()\ntrain_data=train.sample(frac=1)\nbatch_size = 1000\ndebug = 0\n#90% for training, 10% for validation\ntrain_size = int(batch_size*0.9)\n\nmax_train_time = 3600 * 1 #hours to seconds of training\n\ncheckpoint = MC(filepath='../working/is_pe_model.h5', monitor='val_loss', save_best_only=True, verbose=1)\n#Train loop\nhistory,trained_model = train_pe_model('../working/is_pe_model.h5', train_data, train_size, batch_size, max_train_time, debug)\ntrained_model.save('is_pe_model_trained.h5')\n#history.save('history_is_pe.pkl')\n\nwith open('history.pkl', 'wb') as f:\n    pickle.dump(history, f)\n    \nFileLink(r'is_pe_model_trained.h5')\nFileLink(r'history_pe.pkl')","metadata":{"execution":{"iopub.status.busy":"2022-01-31T00:44:47.061182Z","iopub.execute_input":"2022-01-31T00:44:47.061549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# select training data\n#train_data = train.loc[train['negative_exam_for_pe'] == 0]\ntrain_data = train.loc[train['pe_present_on_image'] == 1]","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:26:48.486074Z","iopub.execute_input":"2022-01-31T11:26:48.486376Z","iopub.status.idle":"2022-01-31T11:26:48.509011Z","shell.execute_reply.started":"2022-01-31T11:26:48.486343Z","shell.execute_reply":"2022-01-31T11:26:48.508353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_pos_model(model_path, train_data, train_size, batch_size, max_train_time, debug):\n    #Train loop\n    for n, (x, y) in enumerate(custom_dcom_image_generator(batch_size, train_data, 'pos', False, debug)):\n        \n        if len(x) < 10: #Tries to filter out empty or short data\n            break\n\n        clear_output(wait=True)\n        print(\"Training batch: %i - %i\" %(batch_size*n, batch_size*(n+1)))\n        model = load_model(model_path)\n        hist = model.fit(\n            x[:train_size], \n            #Y values are in a dict as there's more than one target for training output\n            {'leftsided_pe':y[:train_size, 0],\n             'rightsided_pe':y[:train_size, 1],\n            'central_pe': y[:train_size, 2]},\n\n            callbacks = checkpoint,\n\n            validation_split=0.2,\n            epochs=3,\n            batch_size=8,\n            verbose=debug\n        )\n\n        print(\"Metrics for batch validation:\")\n        model.evaluate(x[train_size:],\n                       {'leftsided_pe':y[train_size:, 0],\n                         'rightsided_pe':y[train_size:, 1],\n                        'central_pe': y[train_size:, 2]})\n\n        try:\n            for key in hist.history.keys():\n                history[key] = np.concatenate([history[key], hist.history[key]], axis=0)\n        except:\n            for key in hist.history.keys():\n                history[key] = hist.history[key]\n\n        #To make sure that our model don't train overtime\n        if time.time() - start >= max_train_time:\n            print(\"Time's up!\")\n            break\n\n        model.save('pos_pe_model.h5')\n        del model, x, y, hist\n        K.clear_session()\n        gc.collect()\n    \n    return history, model","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:26:55.054235Z","iopub.execute_input":"2022-01-31T11:26:55.054935Z","iopub.status.idle":"2022-01-31T11:26:55.066981Z","shell.execute_reply.started":"2022-01-31T11:26:55.054883Z","shell.execute_reply":"2022-01-31T11:26:55.066167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = {}\nstart = time.time()\n#train_data=train.sample(frac=1)\nbatch_size = 100\ndebug = 0\n#90% for training, 10% for validation\ntrain_size = int(batch_size*0.9)\n\nmax_train_time = 3600 * 0.5 #hours to seconds of training\n\ncheckpoint = MC(filepath='../working/pos_pe_model.h5', monitor='val_loss', save_best_only=True, verbose=1)\n#Train loop\nhistory,trained_model = train_pos_model('../working/pos_pe_model.h5', train_data, train_size, batch_size, max_train_time, debug)\ntrained_model.save('pos_pe_model_trained.h5')\n#history.save('history_pos_pe.pkl')\n\nwith open('history.pkl', 'wb') as f:\n    pickle.dump(history, f)\n    \nFileLink(r'is_pos_model_trained.h5')\nFileLink(r'history_pos.pkl')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_sev_model(model_path, train_data, train_size, batch_size, max_train_time, debug):\n    #Train loop\n    for n, (x, y) in enumerate(custom_dcom_image_generator(batch_size, train_data, 'pe', False, debug)):\n        \n        if len(x) < 10: #Tries to filter out empty or short data\n            break\n\n        clear_output(wait=True)\n        print(\"Training batch: %i - %i\" %(batch_size*n, batch_size*(n+1)))\n        model = load_model(model_path)\n        hist = model.fit(\n            x[:train_size], \n            #Y values are in a dict as there's more than one target for training output\n            {'chronic_pe':y[:train_size, 0],\n             'acute_and_chronic_pe':y[:train_size, 1]},\n\n            callbacks = checkpoint,\n\n            validation_split=0.2,\n            epochs=3,\n            batch_size=8,\n            verbose=debug\n        )\n\n        print(\"Metrics for batch validation:\")\n        model.evaluate(x[train_size:],\n                       {'chronic_pe':y[train_size:, 0],\n             'acute_and_chronic_pe':y[train_size:, 1]})\n\n        try:\n            for key in hist.history.keys():\n                history[key] = np.concatenate([history[key], hist.history[key]], axis=0)\n        except:\n            for key in hist.history.keys():\n                history[key] = hist.history[key]\n\n        #To make sure that our model don't train overtime\n        if time.time() - start >= max_train_time:\n            print(\"Time's up!\")\n            break\n\n        model.save('sev_pe_model.h5')\n        del model, x, y, hist\n        K.clear_session()\n        gc.collect()\n    \n    return history, model","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:26:58.839393Z","iopub.execute_input":"2022-01-31T11:26:58.839671Z","iopub.status.idle":"2022-01-31T11:26:58.850865Z","shell.execute_reply.started":"2022-01-31T11:26:58.83962Z","shell.execute_reply":"2022-01-31T11:26:58.849883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = {}\nstart = time.time()\n#train_data=train.sample(frac=1)\nbatch_size = 1000\ndebug = 0\n#90% for training, 10% for validation\ntrain_size = int(batch_size*0.9)\n\nmax_train_time = 3600 * 0.5 #hours to seconds of training\n\ncheckpoint = MC(filepath='../working/sev_pe_model.h5', monitor='val_loss', save_best_only=True, verbose=1)\n#Train loop\nhistory,trained_model = train_pos_model('../working/sev_pe_model.h5', train_data, train_size, batch_size, max_train_time, debug)\ntrained_model.save('sev_pe_model_trained.h5')\n#history.save('history_sev_pe.pkl')\n\nwith open('history.pkl', 'wb') as f:\n    pickle.dump(history, f)\n    \nFileLink(r'is_sev_model_trained.h5')\nFileLink(r'history_sev.pkl')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predictions","metadata":{}},{"cell_type":"code","source":"predictions = {}\nstopper = 3600 * 1 #8 hours limit for prediction\npred_start_time = time.time()\\\n\np, c = time.time(), time.time()\nbatch_size = 1000\n    \nl = 0\nn = test.shape[0]\n\nfor x in custom_dcom_image_generator(batch_size, test, 'pe', True, False):\n#custom_dcom_image_generator(batch_size, test, True, False):\n    clear_output(wait=True)\n    model = load_model(\"../input/models/pos_pe_model_trained.h5\")\n#   model = trained_model\n    preds = model.predict(x, batch_size=8, verbose=1)\n    \n    try:\n        for key in preds.keys():\n            predictions[key] += preds[key].flatten().tolist()\n            \n    except Exception as e:\n        print(e)\n        for key in preds.keys():\n            predictions[key] = preds[key].flatten().tolist()\n            \n    l = (l+batch_size)%n\n    #print('Total predicted:', len(predictions['indeterminate']),'/', n)\n    p, c = c, time.time()\n    print(\"One batch time: %.2f seconds\" %(c-p))\n    print(\"ETA: %.2f\" %((n-l)*(c-p)/batch_size))\n    \n    if c - pred_start_time >= stopper:\n        print(\"Time's up!\")\n        break\n    \n    del model\n    K.clear_session()\n    \n    del x, preds\n    gc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-01-31T11:27:33.921259Z","iopub.execute_input":"2022-01-31T11:27:33.921512Z","iopub.status.idle":"2022-01-31T12:27:41.636655Z","shell.execute_reply.started":"2022-01-31T11:27:33.921481Z","shell.execute_reply":"2022-01-31T12:27:41.634895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_file = \"pred_file.pkl\"\nwith open(pred_file,'wb') as f:\n    pickle.dump(predictions, f)\nFileLink(pred_file)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T12:31:22.831175Z","iopub.execute_input":"2022-01-31T12:31:22.835709Z","iopub.status.idle":"2022-01-31T12:31:22.865312Z","shell.execute_reply.started":"2022-01-31T12:31:22.835662Z","shell.execute_reply":"2022-01-31T12:31:22.864306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2022-01-31T12:31:30.03277Z","iopub.execute_input":"2022-01-31T12:31:30.033101Z","iopub.status.idle":"2022-01-31T12:31:30.090207Z","shell.execute_reply.started":"2022-01-31T12:31:30.033062Z","shell.execute_reply":"2022-01-31T12:31:30.089423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pickle \nwith open('../input/predictions/pred_is_pe.pkl','rb') as f:\n    predictions=pickle.load(f)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T12:33:27.327115Z","iopub.execute_input":"2022-01-31T12:33:27.327387Z","iopub.status.idle":"2022-01-31T12:33:27.347726Z","shell.execute_reply.started":"2022-01-31T12:33:27.327347Z","shell.execute_reply":"2022-01-31T12:33:27.347073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for key in predictions.keys():\n    print(key, np.array(predictions[key]).shape)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T12:33:31.759557Z","iopub.execute_input":"2022-01-31T12:33:31.760111Z","iopub.status.idle":"2022-01-31T12:33:31.784376Z","shell.execute_reply.started":"2022-01-31T12:33:31.760072Z","shell.execute_reply":"2022-01-31T12:33:31.78365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for key in y_test.keys():\n    print(key, np.array(y_test[key]).shape)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T12:33:35.014462Z","iopub.execute_input":"2022-01-31T12:33:35.015005Z","iopub.status.idle":"2022-01-31T12:33:35.032251Z","shell.execute_reply.started":"2022-01-31T12:33:35.014965Z","shell.execute_reply":"2022-01-31T12:33:35.031524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# image-level test performance\n\ndef GetFinalScores(pred, y_test):\n    test_scores={}\n    pred=round(pred)\n    for key in ['pe_present_on_image']:\n    #['pe_present_on_image', 'leftsided_pe','chronic_pe', 'rightsided_pe', 'acute_and_chronic_pe', 'central_pe', 'indeterminate']:\n        fpr, tpr, thresholds = roc_curve(y_test[key], pred[key])\n        roc_auc = auc(fpr, tpr)\n        conf_matrix=confusion_matrix(y_test[key], pred[key])\n\n#         precision = precision_score(y_test[key], pred[key])\n\n        recall = recall_score(y_test[key], pred[key])\n\n        accuracy = balanced_accuracy_score(y_test[key], pred[key])\n        \n        test_scores[key]=[roc_auc,conf_matrix,recall,accuracy]\n\n    return test_scores","metadata":{"execution":{"iopub.status.busy":"2022-01-31T12:35:18.323214Z","iopub.execute_input":"2022-01-31T12:35:18.3235Z","iopub.status.idle":"2022-01-31T12:35:18.330362Z","shell.execute_reply.started":"2022-01-31T12:35:18.323467Z","shell.execute_reply":"2022-01-31T12:35:18.329663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Prediction size may be smaller than y_test if 4 hours weren't enough to predict entire test set\npred_df=pd.DataFrame.from_dict(predictions)\n\nif len(y_test)!= len(pred_df):\n    y_test_cropped=y_test[:len(pred_df)]\n    test_scores=GetFinalScores(pred_df,y_test_cropped)\nelse:\n    test_scores=GetFinalScores(pred_df,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-01-31T12:35:22.178648Z","iopub.execute_input":"2022-01-31T12:35:22.179276Z","iopub.status.idle":"2022-01-31T12:35:22.28817Z","shell.execute_reply.started":"2022-01-31T12:35:22.179235Z","shell.execute_reply":"2022-01-31T12:35:22.287091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_scores","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# OLD CODE","metadata":{}}]}