{"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":"%load_ext autoreload\n%autoreload 2\n\n!conda install '/kaggle/input/pydicom-conda-helper/libjpeg-turbo-2.1.0-h7f98852_0.tar.bz2' -y --offline\n!conda install '/kaggle/input/pydicom-conda-helper/libgcc-ng-9.3.0-h2828fa1_19.tar.bz2' -y --offline\n!conda install '/kaggle/input/pydicom-conda-helper/gdcm-2.8.9-py37h500ead1_1.tar.bz2' -y --offline\n!conda install '/kaggle/input/pydicom-conda-helper/conda-4.10.1-py37h89c1867_0.tar.bz2' -y --offline\n!conda install '/kaggle/input/pydicom-conda-helper/certifi-2020.12.5-py37h89c1867_1.tar.bz2' -y --offline\n!conda install '/kaggle/input/pydicom-conda-helper/openssl-1.1.1k-h7f98852_0.tar.bz2' -y --offline\n\n## MMDetection compatible torch installation\n!pip install '/kaggle/input/pytorch-170-cuda-toolkit-110221/torch-1.7.0+cu110-cp37-cp37m-linux_x86_64.whl' --no-deps\n!pip install '/kaggle/input/pytorch-170-cuda-toolkit-110221/torchvision-0.8.1+cu110-cp37-cp37m-linux_x86_64.whl' --no-deps\n!pip install '/kaggle/input/pytorch-170-cuda-toolkit-110221/torchaudio-0.7.0-cp37-cp37m-linux_x86_64.whl' --no-deps\n\n## Compatible Cuda Toolkit installation\n!mkdir -p /kaggle/tmp && cp /kaggle/input/pytorch-170-cuda-toolkit-110221/cudatoolkit-11.0.221-h6bb024c_0 /kaggle/tmp/cudatoolkit-11.0.221-h6bb024c_0.tar.bz2 && conda install /kaggle/tmp/cudatoolkit-11.0.221-h6bb024c_0.tar.bz2 -y --offline\n\n## MMDetection Offline Installation\n!pip install '/kaggle/input/mmdetectionv2140/addict-2.4.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/yapf-0.31.0-py2.py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/terminal-0.4.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/terminaltables-3.1.0-py3-none-any.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/mmcv_full-1_3_8-cu110-torch1_7_0/mmcv_full-1.3.8-cp37-cp37m-manylinux1_x86_64.whl' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/pycocotools-2.0.2/pycocotools-2.0.2' --no-deps\n!pip install '/kaggle/input/mmdetectionv2140/mmpycocotools-12.0.3/mmpycocotools-12.0.3' --no-deps\n\n!cp -r /kaggle/input/mmdetectionv2140/mmdetection-2.14.0 /kaggle/working/\n!mv /kaggle/working/mmdetection-2.14.0 /kaggle/working/mmdetection\n%cd /kaggle/working/mmdetection\n!pip install -e . --no-deps\n%cd /kaggle/working/","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ! cp -f ../input/bug-fix/cascade_roi_head.py ./mmdetection/mmdet/models/roi_heads/cascade_roi_head.py","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport glob\nfrom tqdm import tqdm\nimport pydicom\nimport skimage.io\nfrom skimage.transform import resize\nimport tensorflow as tf\nfrom tensorflow.keras.layers.experimental import preprocessing\nfrom tensorflow.keras.applications import EfficientNetB0","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sys\nsys.path.insert(0, \"./mmdetection\")\nfrom mmcv import Config\nfrom mmdet.apis import init_detector, inference_detector\n\ncfg_path = '../input/siim-config/cascade_rcnn.py'\ncheckpoint = '../input/siimcovid19detection/cascade_rcnn.pth'\ncfg = Config.fromfile(cfg_path)\ncascade_net = init_detector(cfg, checkpoint, device='cuda:0')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 214\n\ndef build_efficient_net():\n    model = tf.keras.Sequential([\n        tf.keras.layers.InputLayer(input_shape=(IMG_SIZE, IMG_SIZE, 3)),\n        preprocessing.RandomRotation(factor=0.15),\n        preprocessing.RandomTranslation(height_factor=0.1, width_factor=0.1),\n        preprocessing.RandomFlip(),\n        preprocessing.RandomContrast(factor=0.1),\n        EfficientNetB0(include_top=True, weights=None, classes=4)\n    ])\n    return model\n\nefficient_net = build_efficient_net()\nefficient_net.load_weights('../input/efficientnet/efficient.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.read_csv('../input/siim-covid19-detection/sample_submission.csv')\ntest_paths = []\nfiles = glob.glob(f\"../input/siim-covid19-detection/test/*/*/*\")\nfor file in files:\n    test_paths.append(file)\n# test_paths = test_paths[:20]\ndicts = []\nfor path in tqdm(test_paths):\n    file = pydicom.dcmread(path)\n    image_id = path[-16:-4] + '_image'\n    study_id = path.split('/')[4] + '_study'\n    width = file.Columns\n    height = file.Rows\n    dicts.append({'image_id': image_id, 'study_id': study_id, 'path': path, 'width': width, 'height': height})\ntest_df = pd.DataFrame(dicts)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! rm -rf test_convert\n! mkdir test_convert","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TEST_DIR = \"./test_convert/\"\nto_grayscale = lambda x: np.uint8(x / x.max() * 255)\nSIZE = 512\n\ndef save_dicom(image, name, path=TEST_DIR):\n    image = to_grayscale(image)\n    image = resize(image, (SIZE, SIZE))\n    image = to_grayscale(image)\n    path = path + name\n    skimage.io.imsave(path, image)\n    \ndef save_test_image(dicom_path):\n    name = dicom_path[-16:-4] + '_512.png'\n    image = pydicom.dcmread(dicom_path).pixel_array\n    save_dicom(image, name, path=TEST_DIR)\n    \nfor path in tqdm(test_paths):\n    save_test_image(path)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = ['negative', 'typical', 'indeterminate', 'atypical']\n\ndef predict_type(path):\n    image = skimage.io.imread(path)\n    image = resize(image, (224, 224))\n    image = np.stack((image, image, image), axis=2)\n    image = image.reshape(1, 224, 224, 3)\n    index = efficient_net(image).numpy().argmax()\n    label = classes[index]\n    return label\n\ndef predict_row(row):\n    name = row['image_id'][:-6] + '_512.png'\n    path = TEST_DIR + name\n    result = inference_detector(cascade_net, path)\n    label = predict_type(path)\n    boxes = result[0][:, :4]\n    scores = result[0][:, 4]\n    output = ''\n    cnt = 0\n    for index in range(len(result[0])):\n        if scores[index] < 0.5:\n            continue\n        cnt += 1\n        prediction = 'opacity ' + \"{:.2f}\".format(scores[index]) + ' '\n        box = boxes[index]\n        h = row['height']\n        w = row['width']\n        x_min = box[0] / 512 * w\n        y_min = box[1] / 512 * h\n        x_max = box[2] / 512 * w\n        y_max = box[3] / 512 * h\n        for n in [x_min, y_min, x_max, y_max]:\n            prediction += \"{:.2f}\".format(n) + ' '\n        output += prediction\n    submission_df.loc[submission_df['id']==row['study_id'], ['PredictionString',]] = label + ' 1 0 0 1 1'\n    if cnt > 0:\n        submission_df.loc[submission_df['id']==row['image_id'], ['PredictionString',]] = output","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for _, row in tqdm(test_df.iterrows(), total=test_df.shape[0]):\n    predict_row(row)\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf /kaggle/working/mmdetection\n!rm -rf /kaggle/working/test_convert","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}