{"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":"markdown","source":"# RSNA 2022 TF-Keras Inference Phase With GPU","metadata":{}},{"cell_type":"markdown","source":"## Set up environment","metadata":{}},{"cell_type":"code","source":"import math, re, os, gc, glob, shutil\nfrom tqdm import tqdm\nimport tensorflow as tf\nimport tensorflow.keras.backend as K\nimport numpy as np, cv2\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom kaggle_datasets import KaggleDatasets\nfrom tensorflow import keras\nfrom functools import partial\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.model_selection import KFold\nprint(\"Tensorflow version \" + tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:29:18.583222Z","iopub.execute_input":"2022-10-28T05:29:18.583721Z","iopub.status.idle":"2022-10-28T05:29:24.814562Z","shell.execute_reply.started":"2022-10-28T05:29:18.583647Z","shell.execute_reply":"2022-10-28T05:29:24.813199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Locally install eff-net and pydicom helper","metadata":{}},{"cell_type":"code","source":"shutil.copytree('/kaggle/input/efficientnet-keras-source-code', '/kaggle/efficientnet-keras-source-code/')\n# Pip install required packages\nos.system(\"pip install -q /kaggle/input/keras-applications/Keras_Applications-1.0.8-py3-none-any.whl\")\nos.system(\"pip install -q /kaggle/efficientnet-keras-source-code\")\nimport efficientnet.tfkeras as efn","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:29:24.81672Z","iopub.execute_input":"2022-10-28T05:29:24.817612Z","iopub.status.idle":"2022-10-28T05:30:24.324528Z","shell.execute_reply.started":"2022-10-28T05:29:24.817571Z","shell.execute_reply":"2022-10-28T05:30:24.323439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!conda install '../input/rsna2022-pydicom-conda-helper/libjpeg-turbo-2.1.0-h7f98852_0.tar.bz2' -c conda-forge -y\n!conda install '../input/rsna2022-pydicom-conda-helper/libgcc-ng-9.3.0-h2828fa1_19.tar.bz2' -c conda-forge -y\n!conda install '../input/rsna2022-pydicom-conda-helper/gdcm-2.8.9-py37h500ead1_1.tar.bz2' -c conda-forge -y\n!conda install '../input/rsna2022-pydicom-conda-helper/conda-4.10.1-py37h89c1867_0.tar.bz2' -c conda-forge -y\n!conda install '../input/rsna2022-pydicom-conda-helper/certifi-2020.12.5-py37h89c1867_1.tar.bz2' -c conda-forge -y\n!conda install '../input/rsna2022-pydicom-conda-helper/openssl-1.1.1k-h7f98852_0.tar.bz2' -c conda-forge -y","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:30:24.325945Z","iopub.execute_input":"2022-10-28T05:30:24.326819Z","iopub.status.idle":"2022-10-28T05:31:19.126992Z","shell.execute_reply.started":"2022-10-28T05:30:24.326781Z","shell.execute_reply":"2022-10-28T05:31:19.125795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# LOAD LIBRARIES\nimport nibabel as nib\nimport pydicom as dicom\nfrom collections import Counter, defaultdict\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:31:19.130472Z","iopub.execute_input":"2022-10-28T05:31:19.130872Z","iopub.status.idle":"2022-10-28T05:31:19.346266Z","shell.execute_reply.started":"2022-10-28T05:31:19.13084Z","shell.execute_reply":"2022-10-28T05:31:19.345321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE = '../input/rsna-2022-cervical-spine-fracture-detection'","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:31:19.347848Z","iopub.execute_input":"2022-10-28T05:31:19.348205Z","iopub.status.idle":"2022-10-28T05:31:19.352998Z","shell.execute_reply.started":"2022-10-28T05:31:19.34817Z","shell.execute_reply":"2022-10-28T05:31:19.352078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Set up variables","metadata":{}},{"cell_type":"code","source":"FOLDS = 5\nN_CHANNEL = 3\nIMGS_PER_CHANNEL = 16\nIMG_SIZE = int(512 // (IMGS_PER_CHANNEL**0.5)) ","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:31:19.354582Z","iopub.execute_input":"2022-10-28T05:31:19.354916Z","iopub.status.idle":"2022-10-28T05:31:19.36214Z","shell.execute_reply.started":"2022-10-28T05:31:19.354883Z","shell.execute_reply":"2022-10-28T05:31:19.361242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = pd.read_csv(f'{BASE}/sample_submission.csv')\ntest_df = pd.read_csv(f'{BASE}/test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:31:19.363647Z","iopub.execute_input":"2022-10-28T05:31:19.363997Z","iopub.status.idle":"2022-10-28T05:31:19.385408Z","shell.execute_reply.started":"2022-10-28T05:31:19.363962Z","shell.execute_reply":"2022-10-28T05:31:19.384554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bad = np.array([['1.2.826.0.1.3680043.10197_C1', '1.2.826.0.1.3680043.10197','C1'],['1.2.826.0.1.3680043.10454_C1', '1.2.826.0.1.3680043.10454','C1'],['1.2.826.0.1.3680043.10690_C1', '1.2.826.0.1.3680043.10690','C1']], dtype=np.object)\n\ntrain_df = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/train.csv\").head(10000)\ntest_df = pd.read_csv(\"../input/rsna-2022-cervical-spine-fracture-detection/test.csv\")\nif(test_df.values[0][0] == bad[0][0]):\n    test_df = pd.DataFrame({\"row_id\": ['1.2.826.0.1.3680043.22327_C1', '1.2.826.0.1.3680043.25399_C1', '1.2.826.0.1.3680043.5876_C1'],\n                           \"StudyInstanceUID\": ['1.2.826.0.1.3680043.22327', '1.2.826.0.1.3680043.25399', '1.2.826.0.1.3680043.5876'],\n                           \"prediction_type\": [\"C1\", \"C1\", \"C1\"]})\ndirs = [\"../input/rsna-2022-cervical-spine-fracture-detection/train_images\",  \"../input/rsna-2022-cervical-spine-fracture-detection/test_images\"]\nmeans = list(train_df.mean(numeric_only=True).to_dict().values())\ntest_df","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:31:19.386904Z","iopub.execute_input":"2022-10-28T05:31:19.387238Z","iopub.status.idle":"2022-10-28T05:31:19.419814Z","shell.execute_reply.started":"2022-10-28T05:31:19.387206Z","shell.execute_reply":"2022-10-28T05:31:19.418799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocesses","metadata":{}},{"cell_type":"code","source":"import math\ndef tileImage(imgs):\n    d = int(math.sqrt(IMGS_PER_CHANNEL))\n    h = imgs[0].shape[0]\n    w = imgs[0].shape[1]\n    r = np.zeros((h*d,w*d,1),dtype=np.float32)\n    for idx,img in enumerate(imgs):\n        idx_y = int(idx/d)\n        idx_x = idx-idx_y*d\n        r[idx_y*h:(idx_y+1)*h,idx_x*w:(idx_x+1)*w,:] = img\n    return r","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:31:19.421414Z","iopub.execute_input":"2022-10-28T05:31:19.421753Z","iopub.status.idle":"2022-10-28T05:31:19.42898Z","shell.execute_reply.started":"2022-10-28T05:31:19.42172Z","shell.execute_reply":"2022-10-28T05:31:19.427942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom(path, size = IMG_SIZE):\n    try:\n        img=dicom.dcmread(path)\n        img.PhotometricInterpretation = 'YBR_FULL'\n        data=img.pixel_array\n        data=data-np.min(data)\n        if np.max(data) != 0:\n            data = data/np.max(data)\n        data = data.astype(np.float32)\n        data = cv2.resize(data, (IMG_SIZE, IMG_SIZE))\n        data = np.expand_dims(data, -1)\n        return data\n    except:        \n        return np.zeros((IMG_SIZE, IMG_SIZE, 1))\n\ndef listdirs(folder):\n    return [d for d in os.listdir(folder) if os.path.isdir(os.path.join(folder, d))]    \n\ntest_dir = f'{BASE}/test_images'\npatients = sorted(os.listdir(test_dir))\nlen(patients), patients[:5]","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:31:19.433202Z","iopub.execute_input":"2022-10-28T05:31:19.433682Z","iopub.status.idle":"2022-10-28T05:31:19.448884Z","shell.execute_reply.started":"2022-10-28T05:31:19.433645Z","shell.execute_reply":"2022-10-28T05:31:19.447445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess(stuid):\n    imgs = glob.glob(BASE+'/test_images/'+stuid+'/*')\n    n_imgs = len(imgs)\n    stride = n_imgs // (N_CHANNEL * IMGS_PER_CHANNEL)\n    pimgs = range(1, n_imgs+1, stride)\n    img_arr_b = []\n    img_arr_g = []\n    img_arr_r = []\n    for impath in pimgs:        \n        img = load_dicom(BASE+'/test_images/'+stuid+f'/{impath}.dcm')\n\n        if len(img_arr_b)<IMGS_PER_CHANNEL:img_arr_b.append(img)\n        elif len(img_arr_g)<IMGS_PER_CHANNEL:img_arr_g.append(img)\n        elif len(img_arr_r)<IMGS_PER_CHANNEL:img_arr_r.append(img)\n            \n    imgB = tileImage(img_arr_b)\n    imgG = tileImage(img_arr_g)\n    imgR = tileImage(img_arr_r)\n    img_cat = np.concatenate([imgB, imgG, imgR], axis=-1)\n    \n    if len(test_df)<5: \n        print(img_cat.shape)\n        plt.imshow((img_cat*255).astype(np.uint8)),plt.show()\n        print(np.max(img_cat), np.min(img_cat))\n    return img_cat","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:31:19.451635Z","iopub.execute_input":"2022-10-28T05:31:19.452465Z","iopub.status.idle":"2022-10-28T05:31:19.4612Z","shell.execute_reply.started":"2022-10-28T05:31:19.45243Z","shell.execute_reply":"2022-10-28T05:31:19.460219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_cols = ['patient_overall']\nfor i in range(1,8): pred_cols += [f'C{i}']\npred_dict = {pred_cols[i]:i for i in range(8)}\npred_dict","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:31:19.462654Z","iopub.execute_input":"2022-10-28T05:31:19.463439Z","iopub.status.idle":"2022-10-28T05:31:19.473076Z","shell.execute_reply.started":"2022-10-28T05:31:19.463404Z","shell.execute_reply":"2022-10-28T05:31:19.471786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Build our Model","metadata":{}},{"cell_type":"code","source":"def get_model():\n    model = tf.keras.Sequential([\n        efn.EfficientNetB7(\n        weights=None, \n        input_shape=(512, 512, 3), include_top=False\n        ),\n        tf.keras.layers.GlobalAveragePooling2D(),\n        tf.keras.layers.Dropout(0.25),\n        tf.keras.layers.Dense(256, activation='swish'),\n        tf.keras.layers.Dropout(0.25),\n        tf.keras.layers.Dense(512, activation='swish'),\n        tf.keras.layers.Dropout(0.25),\n        tf.keras.layers.Dense(8, activation='softmax')\n    ])\n    model.compile(\n        optimizer=tf.keras.optimizers.Adam(),\n        loss='categorical_crossentropy',\n        metrics=[tf.keras.metrics.AUC(multi_label=True)])\n    model.summary()\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:32:52.583838Z","iopub.execute_input":"2022-10-28T05:32:52.584565Z","iopub.status.idle":"2022-10-28T05:32:52.592986Z","shell.execute_reply.started":"2022-10-28T05:32:52.584524Z","shell.execute_reply":"2022-10-28T05:32:52.591682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_model()","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:32:58.3811Z","iopub.execute_input":"2022-10-28T05:32:58.381495Z","iopub.status.idle":"2022-10-28T05:33:04.204891Z","shell.execute_reply.started":"2022-10-28T05:32:58.381459Z","shell.execute_reply":"2022-10-28T05:33:04.199078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Inference Loop","metadata":{}},{"cell_type":"code","source":"from collections import OrderedDict","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:33:04.944911Z","iopub.execute_input":"2022-10-28T05:33:04.945795Z","iopub.status.idle":"2022-10-28T05:33:04.950607Z","shell.execute_reply.started":"2022-10-28T05:33:04.945761Z","shell.execute_reply":"2022-10-28T05:33:04.949502Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_all = np.zeros((test_df.shape[0]))\n\nfor fold in range(FOLDS):\n    print(); print('#'*25)\n    print('### FOLD',fold+1)\n    print('#'*25)\n\n    print('Loading best model...')\n    #../input/inputrsna2022splitfoldidwiseefnetb716\n    #../input/inputinputrsna2022splitfoldidwiseefnetb7161\n    model.load_weights('../input/inputrsna2022splitfoldidwiseefnetb716/fold-%i.h5'%fold)\n\n    predictions = []\n    \n    pred_n2s = OrderedDict() \n    for stuid, pred_type in zip(test_df['StudyInstanceUID'], test_df['prediction_type']):\n        pred_n2s[stuid+'_'+pred_type]=-1\n\n    for stuid, pred_type in zip(test_df['StudyInstanceUID'], test_df['prediction_type']):\n        if pred_n2s[stuid+'_'+pred_type]==-1:\n            img = np.expand_dims(preprocess(stuid), axis=0)\n            preds = model.predict(img, verbose=1)[0]\n            for j, (k, v) in enumerate(pred_dict.items()):\n                pred_n2s[stuid+'_'+k]= preds[j]\n            predictions.append(pred_n2s[stuid+'_'+pred_type])     \n        else:\n            predictions.append(pred_n2s[stuid+'_'+pred_type])\n        \n    pred_all += predictions","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:33:10.378661Z","iopub.execute_input":"2022-10-28T05:33:10.379018Z","iopub.status.idle":"2022-10-28T05:33:53.131829Z","shell.execute_reply.started":"2022-10-28T05:33:10.378987Z","shell.execute_reply":"2022-10-28T05:33:53.1309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Make Submit","metadata":{}},{"cell_type":"code","source":"sub_df['fractured'] = pred_all / FOLDS\nsub_df.to_csv('submission.csv',index=False)\nsub_df","metadata":{"execution":{"iopub.status.busy":"2022-10-28T05:33:53.134188Z","iopub.execute_input":"2022-10-28T05:33:53.134583Z","iopub.status.idle":"2022-10-28T05:33:53.149324Z","shell.execute_reply.started":"2022-10-28T05:33:53.134546Z","shell.execute_reply":"2022-10-28T05:33:53.148111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}