{"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":"## My Other Works\n### [Create IDwise TF Records](https://www.kaggle.com/code/itsuki9180/create-idwise-tf-records)\n### [RSNA 2022 TF-Keras Traing Phase With TPU](https://www.kaggle.com/code/itsuki9180/rsna-2022-tf-keras-traing-phase-with-tpu)\n### [RSNA 2022 TF-Keras Inference Phase With GPU](https://www.kaggle.com/itsuki9180/rsna-2022-tf-keras-inference-phase-with-gpu) <- Here","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-08-21T01:19:02.882858Z","iopub.execute_input":"2022-08-21T01:19:02.883445Z","iopub.status.idle":"2022-08-21T01:19:09.229409Z","shell.execute_reply.started":"2022-08-21T01:19:02.883354Z","shell.execute_reply":"2022-08-21T01:19:09.228261Z"},"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-08-21T01:19:09.231452Z","iopub.execute_input":"2022-08-21T01:19:09.23235Z","iopub.status.idle":"2022-08-21T01:20:09.451433Z","shell.execute_reply.started":"2022-08-21T01:19:09.232311Z","shell.execute_reply":"2022-08-21T01:20:09.450527Z"},"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-08-21T01:20:09.452826Z","iopub.execute_input":"2022-08-21T01:20:09.453196Z","iopub.status.idle":"2022-08-21T01:21:03.788356Z","shell.execute_reply.started":"2022-08-21T01:20:09.453168Z","shell.execute_reply":"2022-08-21T01:21:03.78717Z"},"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-08-21T01:21:03.792288Z","iopub.execute_input":"2022-08-21T01:21:03.792636Z","iopub.status.idle":"2022-08-21T01:21:04.019146Z","shell.execute_reply.started":"2022-08-21T01:21:03.792592Z","shell.execute_reply":"2022-08-21T01:21:04.018162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BASE = '../input/rsna-2022-cervical-spine-fracture-detection'","metadata":{"execution":{"iopub.status.busy":"2022-08-21T01:21:04.020611Z","iopub.execute_input":"2022-08-21T01:21:04.021058Z","iopub.status.idle":"2022-08-21T01:21:04.026485Z","shell.execute_reply.started":"2022-08-21T01:21:04.021016Z","shell.execute_reply":"2022-08-21T01:21:04.025464Z"},"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-08-21T01:21:04.028171Z","iopub.execute_input":"2022-08-21T01:21:04.028934Z","iopub.status.idle":"2022-08-21T01:21:04.036969Z","shell.execute_reply.started":"2022-08-21T01:21:04.028884Z","shell.execute_reply":"2022-08-21T01:21:04.035918Z"},"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-08-21T01:21:04.038497Z","iopub.execute_input":"2022-08-21T01:21:04.039209Z","iopub.status.idle":"2022-08-21T01:21:04.064393Z","shell.execute_reply.started":"2022-08-21T01:21:04.039175Z","shell.execute_reply":"2022-08-21T01:21:04.06339Z"},"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-08-21T01:21:04.065731Z","iopub.execute_input":"2022-08-21T01:21:04.066141Z","iopub.status.idle":"2022-08-21T01:21:04.100872Z","shell.execute_reply.started":"2022-08-21T01:21:04.066106Z","shell.execute_reply":"2022-08-21T01:21:04.099922Z"},"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-08-21T01:21:04.102185Z","iopub.execute_input":"2022-08-21T01:21:04.103124Z","iopub.status.idle":"2022-08-21T01:21:04.111319Z","shell.execute_reply.started":"2022-08-21T01:21:04.10309Z","shell.execute_reply":"2022-08-21T01:21:04.110347Z"},"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-08-21T01:21:04.116148Z","iopub.execute_input":"2022-08-21T01:21:04.117809Z","iopub.status.idle":"2022-08-21T01:21:04.132471Z","shell.execute_reply.started":"2022-08-21T01:21:04.117773Z","shell.execute_reply":"2022-08-21T01:21:04.131443Z"},"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-08-21T01:21:04.135938Z","iopub.execute_input":"2022-08-21T01:21:04.137785Z","iopub.status.idle":"2022-08-21T01:21:04.145827Z","shell.execute_reply.started":"2022-08-21T01:21:04.137758Z","shell.execute_reply":"2022-08-21T01:21:04.144532Z"},"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-08-21T01:21:04.147303Z","iopub.execute_input":"2022-08-21T01:21:04.148351Z","iopub.status.idle":"2022-08-21T01:21:04.158536Z","shell.execute_reply.started":"2022-08-21T01:21:04.148316Z","shell.execute_reply":"2022-08-21T01:21:04.157559Z"},"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(8, activation='sigmoid')\n    ])\n    model.compile(\n        optimizer=tf.keras.optimizers.Adam(),\n        loss='binary_crossentropy',\n        metrics=[tf.keras.metrics.AUC(multi_label=True)])\n    model.summary()\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-08-21T01:21:04.160138Z","iopub.execute_input":"2022-08-21T01:21:04.16081Z","iopub.status.idle":"2022-08-21T01:21:04.168733Z","shell.execute_reply.started":"2022-08-21T01:21:04.160777Z","shell.execute_reply":"2022-08-21T01:21:04.167754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = get_model()","metadata":{"execution":{"iopub.status.busy":"2022-08-21T01:21:04.171173Z","iopub.execute_input":"2022-08-21T01:21:04.172456Z","iopub.status.idle":"2022-08-21T01:21:15.397615Z","shell.execute_reply.started":"2022-08-21T01:21:04.172421Z","shell.execute_reply":"2022-08-21T01:21:15.396549Z"},"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-08-21T01:21:15.398917Z","iopub.execute_input":"2022-08-21T01:21:15.399496Z","iopub.status.idle":"2022-08-21T01:21:15.404585Z","shell.execute_reply.started":"2022-08-21T01:21:15.399447Z","shell.execute_reply":"2022-08-21T01:21:15.403237Z"},"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    model.load_weights('../input/rsna2022-split-fold-id-wise-efnet-b7/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-08-21T01:21:15.406257Z","iopub.execute_input":"2022-08-21T01:21:15.406946Z","iopub.status.idle":"2022-08-21T01:22:02.890232Z","shell.execute_reply.started":"2022-08-21T01:21:15.406888Z","shell.execute_reply":"2022-08-21T01:22:02.889298Z"},"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-08-21T01:22:02.891854Z","iopub.execute_input":"2022-08-21T01:22:02.892201Z","iopub.status.idle":"2022-08-21T01:22:02.906295Z","shell.execute_reply.started":"2022-08-21T01:22:02.892163Z","shell.execute_reply":"2022-08-21T01:22:02.905154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}