{"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":"thanks to https://www.kaggle.com/h053473666/siim-cov19-efnb7-infer-study/data","metadata":{}},{"cell_type":"code","source":"!conda install '/kaggle/input/pydicom-conda-helper/libjpeg-turbo-2.1.0-h7f98852_0.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/libgcc-ng-9.3.0-h2828fa1_19.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/gdcm-2.8.9-py37h500ead1_1.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/conda-4.10.1-py37h89c1867_0.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/certifi-2020.12.5-py37h89c1867_1.tar.bz2' -c conda-forge -y\n!conda install '/kaggle/input/pydicom-conda-helper/openssl-1.1.1k-h7f98852_0.tar.bz2' -c conda-forge -y\n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-08T09:48:45.805265Z","iopub.execute_input":"2021-07-08T09:48:45.805792Z","iopub.status.idle":"2021-07-08T09:49:53.488816Z","shell.execute_reply.started":"2021-07-08T09:48:45.805657Z","shell.execute_reply":"2021-07-08T09:49:53.487886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom PIL import Image\nimport pandas as pd\nfrom tqdm.auto import tqdm","metadata":{"execution":{"iopub.status.busy":"2021-07-08T09:49:53.492484Z","iopub.execute_input":"2021-07-08T09:49:53.492744Z","iopub.status.idle":"2021-07-08T09:49:53.65712Z","shell.execute_reply.started":"2021-07-08T09:49:53.492715Z","shell.execute_reply":"2021-07-08T09:49:53.656295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\ndef read_xray(path, voi_lut=True, fix_monochrome = True):\n    dicom = pydicom.read_file(path)\n    if voi_lut:\n        data = apply_voi_lut(dicom.pixel_array, dicom)\n    else:\n        data = dicom.pixel_array\n    \n    if fix_monochrome and dicom.PhotometricInterpretation ==\"MONOCHROME1\" : \n        data = np.amax(data) - data\n    \n    data = data - np.min(data)\n    data = data / np.max(data)\n    data = (data*255).astype(np.uint8)\n    \n    return data","metadata":{"execution":{"iopub.status.busy":"2021-07-08T09:49:53.658905Z","iopub.execute_input":"2021-07-08T09:49:53.659272Z","iopub.status.idle":"2021-07-08T09:49:53.930503Z","shell.execute_reply.started":"2021-07-08T09:49:53.659235Z","shell.execute_reply":"2021-07-08T09:49:53.929658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize(array, size, keep_ratio = False, resample = Image.LANCZOS):\n    im = Image.fromarray(array)\n    if keep_ratio:\n        im.thumbnail((size,size), resample)\n    else:\n        im = im.resize((size,size), resample)\n    return im","metadata":{"execution":{"iopub.status.busy":"2021-07-08T09:49:53.932235Z","iopub.execute_input":"2021-07-08T09:49:53.93257Z","iopub.status.idle":"2021-07-08T09:49:53.939911Z","shell.execute_reply.started":"2021-07-08T09:49:53.932535Z","shell.execute_reply":"2021-07-08T09:49:53.939007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# split = 'test'\n# save_dir = f'/kaggle/tmp/{split}/'\n\n# os.makedirs(save_dir, exist_ok=True)\n# save_dir = f'/kaggle/tmp/{split}/study/'\n# os.makedirs(save_dir, exist_ok = True)\n\n# for dirname, _, filenames in tqdm(os.walk(f'../input/siim-covid19-detection/{split}')):\n#     for file in filenames:\n#         xray = read_xray(os.path.join(dirname, file))\n#         im = resize(xray, size=600)\n#         study = dirname.split('/')[-2] + '_study.png'\n#         im.save(os.path.join(save_dir, study))\n\nsplit = 'test'\nsave_dir = f'/kaggle/tmp/{split}/'\n\nos.makedirs(save_dir, exist_ok=True)\n\nsave_dir = f'/kaggle/tmp/{split}/study/'\nos.makedirs(save_dir, exist_ok=True)\n\nfor dirname, _, filenames in tqdm(os.walk(f'../input/siim-covid19-detection/{split}')):\n    for file in filenames:\n        xray = read_xray(os.path.join(dirname, file))\n        im = resize(xray, size=600)  \n        study = dirname.split('/')[-2] + '_study.png'\n        im.save(os.path.join(save_dir, study))","metadata":{"execution":{"iopub.status.busy":"2021-07-08T09:49:53.941107Z","iopub.execute_input":"2021-07-08T09:49:53.941443Z","iopub.status.idle":"2021-07-08T09:59:07.106663Z","shell.execute_reply.started":"2021-07-08T09:49:53.941408Z","shell.execute_reply":"2021-07-08T09:59:07.105873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\n\ndf = pd.read_csv('../input/siim-covid19-detection/sample_submission.csv')\nid_laststr_list  = []\nfor i in range(df.shape[0]):\n    id_laststr_list.append(df.loc[i,'id'][-1])\ndf['id_last_str'] = id_laststr_list\n\nstudy_len = df[df['id_last_str'] == 'y'].shape[0]","metadata":{"execution":{"iopub.status.busy":"2021-07-08T09:59:07.108079Z","iopub.execute_input":"2021-07-08T09:59:07.108416Z","iopub.status.idle":"2021-07-08T09:59:07.183162Z","shell.execute_reply.started":"2021-07-08T09:59:07.108378Z","shell.execute_reply":"2021-07-08T09:59:07.182358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install /kaggle/input/kerasapplications -q\n!pip install /kaggle/input/efficientnet-keras-source-code/ -q --no-deps\n\nimport os\n\nimport efficientnet.tfkeras as efn\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\n\ndef auto_select_accelerator():\n    try:\n        tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n        tf.config.experimental_connect_to_cluster(tpu)\n        tf.tpu.experimental.initialize_tpu_system(tpu)\n        strategy = tf.distribute.experimental.TPUStrategy(tpu)\n        print(\"Running on TPU:\", tpu.master())\n    except ValueError:\n        strategy = tf.distribute.get_strategy()\n    print(f\"Running on {strategy.num_replicas_in_sync} replicas\")\n\n    return strategy\n\n\ndef build_decoder(with_labels=True, target_size=(300, 300), ext='jpg'):\n    def decode(path):\n        file_bytes = tf.io.read_file(path)\n        if ext == 'png':\n            img = tf.image.decode_png(file_bytes, channels=3)\n        elif ext in ['jpg', 'jpeg']:\n            img = tf.image.decode_jpeg(file_bytes, channels=3)\n        else:\n            raise ValueError(\"Image extension not supported\")\n\n        img = tf.cast(img, tf.float32) / 255.0\n        img = tf.image.resize(img, target_size)\n\n        return img\n\n    def decode_with_labels(path, label):\n        return decode(path), label\n\n    return decode_with_labels if with_labels else decode\n\n\ndef build_augmenter(with_labels=True):\n    def augment(img):\n        img = tf.image.random_flip_left_right(img)\n        img = tf.image.random_flip_up_down(img)\n        return img\n\n    def augment_with_labels(img, label):\n        return augment(img), label\n\n    return augment_with_labels if with_labels else augment\n\n\ndef build_dataset(paths, labels=None, bsize=32, cache=True,\n                  decode_fn=None, augment_fn=None,\n                  augment=True, repeat=True, shuffle=1024, \n                  cache_dir=\"\"):\n    if cache_dir != \"\" and cache is True:\n        os.makedirs(cache_dir, exist_ok=True)\n\n    if decode_fn is None:\n        decode_fn = build_decoder(labels is not None)\n\n    if augment_fn is None:\n        augment_fn = build_augmenter(labels is not None)\n\n    AUTO = tf.data.experimental.AUTOTUNE\n    slices = paths if labels is None else (paths, labels)\n\n    dset = tf.data.Dataset.from_tensor_slices(slices)\n    dset = dset.map(decode_fn, num_parallel_calls=AUTO)\n    dset = dset.cache(cache_dir) if cache else dset\n    dset = dset.map(augment_fn, num_parallel_calls=AUTO) if augment else dset\n    dset = dset.repeat() if repeat else dset\n    dset = dset.shuffle(shuffle) if shuffle else dset\n    dset = dset.batch(bsize).prefetch(AUTO)\n\n    return dset\n\n#COMPETITION_NAME = \"siim-cov19-test-img512-study-600\"\nstrategy = auto_select_accelerator()\nBATCH_SIZE = strategy.num_replicas_in_sync * 16\n\nIMSIZE = (224, 240, 260, 300, 380, 456, 528, 600)\n\n#load_dir = f\"/kaggle/input/{COMPETITION_NAME}/\"\nsub_df = pd.read_csv('../input/siim-covid19-detection/sample_submission.csv')\nsub_df = sub_df[:study_len]\ntest_paths = f'/kaggle/tmp/{split}/study/' + sub_df['id'] +'.png'\n\nsub_df['negative'] = 0\nsub_df['typical'] = 0\nsub_df['indeterminate'] = 0\nsub_df['atypical'] = 0\n\n\nlabel_cols = sub_df.columns[2:]\n\ntest_decoder = build_decoder(with_labels=False, target_size=(IMSIZE[7], IMSIZE[7]), ext='png')\ndtest = build_dataset(\n    test_paths, bsize=BATCH_SIZE, repeat=False, \n    shuffle=False, augment=False, cache=False,\n    decode_fn=test_decoder\n)\n\nwith strategy.scope():\n    \n    models = []\n    \n    models0 = tf.keras.models.load_model(\n        '../input/fifthefb-model/model0.h5'\n    )\n    models1 = tf.keras.models.load_model(\n        '../input/fifthefb-model/model1.h5'\n    )\n    models2 = tf.keras.models.load_model(\n        '../input/fifthefb-model/model2.h5'\n    )\n    models3 = tf.keras.models.load_model(\n        '../input/fifthefb-model/model3.h5'\n    )\n    models4 = tf.keras.models.load_model(\n        '../input/fifthefb-model/model4.h5'\n    )\n    \n    models.append(models0)\n    models.append(models1)\n    models.append(models2)\n    models.append(models3)\n    models.append(models4)\n\n    \n    \n    \nsub_df[label_cols] = sum([model.predict(dtest, verbose=1) for model in models]) / len(models)","metadata":{"execution":{"iopub.status.busy":"2021-07-08T09:59:07.186024Z","iopub.execute_input":"2021-07-08T09:59:07.186291Z","iopub.status.idle":"2021-07-08T10:06:16.813582Z","shell.execute_reply.started":"2021-07-08T09:59:07.186265Z","shell.execute_reply":"2021-07-08T10:06:16.812733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.columns = ['id', 'PredictionString1', 'negative', 'typical', 'indeterminate', 'atypical']\ndf = pd.merge(df, sub_df, on = 'id', how = 'left')","metadata":{"execution":{"iopub.status.busy":"2021-07-08T10:06:16.815645Z","iopub.execute_input":"2021-07-08T10:06:16.815899Z","iopub.status.idle":"2021-07-08T10:06:16.834188Z","shell.execute_reply.started":"2021-07-08T10:06:16.815871Z","shell.execute_reply":"2021-07-08T10:06:16.833427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(study_len):\n    negative = df.loc[i,'negative']\n    typical = df.loc[i,'typical']\n    indeterminate = df.loc[i,'indeterminate']\n    atypical = df.loc[i,'atypical']\n    df.loc[i, 'PredictionString'] = f'negative {negative} 0 0 1 1 typical {typical} 0 0 1 1 indeterminate {indeterminate} 0 0 1 1 atypical {atypical} 0 0 1 1'","metadata":{"execution":{"iopub.status.busy":"2021-07-08T10:06:16.835528Z","iopub.execute_input":"2021-07-08T10:06:16.835867Z","iopub.status.idle":"2021-07-08T10:06:17.478289Z","shell.execute_reply.started":"2021-07-08T10:06:16.835831Z","shell.execute_reply":"2021-07-08T10:06:17.477458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df[['id', 'PredictionString']]\n\n\ndf","metadata":{"execution":{"iopub.status.busy":"2021-07-08T10:06:17.479585Z","iopub.execute_input":"2021-07-08T10:06:17.479952Z","iopub.status.idle":"2021-07-08T10:06:17.499662Z","shell.execute_reply.started":"2021-07-08T10:06:17.479914Z","shell.execute_reply":"2021-07-08T10:06:17.498592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-07-08T10:06:17.500929Z","iopub.execute_input":"2021-07-08T10:06:17.50129Z","iopub.status.idle":"2021-07-08T10:06:17.521797Z","shell.execute_reply.started":"2021-07-08T10:06:17.501254Z","shell.execute_reply":"2021-07-08T10:06:17.521114Z"},"trusted":true},"execution_count":null,"outputs":[]}]}