{"cells":[{"metadata":{"trusted":true,"_uuid":"3cbfb24c526c7646df10657291cd850f705f135e"},"cell_type":"code","source":"import pandas as pd \nimport numpy as np\nimport scipy.misc\nimport pydicom \nimport glob\nimport sys\nimport os\nimport pandas as pd \nimport base64\nfrom IPython.display import HTML","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"!git clone https://github.com/fizyr/keras-retinanet\nos.chdir(\"keras-retinanet\") \n!python setup.py build_ext --inplace","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"DATA_DIR = \"/kaggle/input/\"\nROOT_DIR = \"/kaggle/working/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b6507bf63555041be8b99fc06e71ad59ec52f55f"},"cell_type":"code","source":"train_pngs_dir = os.path.join(DATA_DIR, \"rsna-pneu-train-png/stage_1_train_pngs/orig/\")\ntest_dicoms_dir  = os.path.join(DATA_DIR, \"rsna-pneumonia-detection-challenge/stage_2_test_images/\") ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c11efca7a4483f071bd62bdc81b337702df71586"},"cell_type":"code","source":"bbox_info = pd.read_csv(os.path.join(DATA_DIR, \"rsna-stage1-archived-inputs/stage_1_train_labels.csv\"))\ndetailed_class_info = pd.read_csv(os.path.join(DATA_DIR, \"rsna-stage1-archived-inputs/stage_1_detailed_class_info.csv\"))\ndetailed_class_info = detailed_class_info.drop_duplicates()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"21ed332fc94fa014880a52a8c3d288874253570d"},"cell_type":"code","source":"positives = detailed_class_info\npositives.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ef5ff8ffedbee2cb96b46990dba21d808c39ca56"},"cell_type":"code","source":"cash_class = positives\ncash_class.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b5846286c6bf38a3943056791074f51c5f4946da"},"cell_type":"code","source":"# To get started, we'll train on positives only\npositives = detailed_class_info[detailed_class_info[\"class\"] == \"opacity\"]\n# Annotations file should have no header and columns in the following order:\n# filename, x1, y1, x2, y2, class \npositives = positives.merge(bbox_info, on=\"patientId\")\npositives = positives[[\"patientId\", \"x\", \"y\", \"width\", \"height\", \"Target\"]]\npositives[\"patientId\"] = [os.path.join(train_pngs_dir, \"{}.png\".format(_)) for _ in positives.patientId]\npositives[\"x1\"] = positives[\"x\"] \npositives[\"y1\"] = positives[\"y\"] \npositives[\"x2\"] = positives[\"x\"] + positives[\"width\"]\npositives[\"y2\"] = positives[\"y\"] + positives[\"height\"]\npositives[\"Target\"] = \"opacity\"\ndel positives[\"x\"], positives[\"y\"], positives[\"width\"], positives[\"height\"]\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b7f7379693445579fad252961f561ce8def64729"},"cell_type":"code","source":"annotations = positives","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a0aeeddc4ab1bf4a4a8bfbd2c5f69a3e0c5ce0d8"},"cell_type":"code","source":"annotations = annotations.fillna(88888)\nannotations[\"x1\"] = annotations.x1.astype(\"int32\").astype(\"str\") \nannotations[\"y1\"] = annotations.y1.astype(\"int32\").astype(\"str\") \nannotations[\"x2\"] = annotations.x2.astype(\"int32\").astype(\"str\") \nannotations[\"y2\"] = annotations.y2.astype(\"int32\").astype(\"str\")\nannotations = annotations.replace({\"88888\": \"\"}) \nannotations = annotations[[\"patientId\", \"x1\", \"y1\", \"x2\", \"y2\", \"Target\"]]\nannotations.to_csv(os.path.join(ROOT_DIR, \"annotations.csv\"), index=False, header=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ad86e704479171f5a7c35b09a2c2ac262d7dec0b"},"cell_type":"code","source":"classes_file = pd.DataFrame({\"class\": [\"opacity\"], \"id\": [0]}) \nclasses_file.to_csv(os.path.join(ROOT_DIR, \"classes.csv\"), index=False, header=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"25d776ee6cf1b6eb611a5ce78a19fda5fae4eb37"},"cell_type":"code","source":"!python /kaggle/working/keras-retinanet/keras_retinanet/bin/train.py --backbone \"resnet50\" --image-min-side 256 --image-max-side 256 --batch-size 1 --random-transform --epochs 1 --steps 8964 csv /kaggle/working/annotations.csv /kaggle/working/classes.csv","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"f9acfdf61cfc5ed464da6134722b21a9fd732038"},"cell_type":"code","source":"!python /kaggle/working/keras-retinanet/keras_retinanet/bin/convert_model.py /kaggle/working/keras-retinanet/snapshots/resnet50_csv_01.h5 /kaggle/working/converted_model.h5 ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f6d715953a5898aab248bce8d12560fbd971e97"},"cell_type":"code","source":"from keras_retinanet.models import load_model \nretinanet = load_model(os.path.join(ROOT_DIR, \"converted_model.h5\"), \n                       backbone_name=\"resnet50\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"24b140261750deebc8356bb94f995ba87bea29df"},"cell_type":"code","source":"import skimage.io","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"04d2326b19484278e2a8ac88a5888cd39642617d"},"cell_type":"code","source":"image = skimage.io.imread(os.path.join(train_pngs_dir, '05fb0374-ab2d-4cf4-bb36-ce56fb2c28a5.png'))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9076ed5564d16fdd40b187d3509500681e006033"},"cell_type":"code","source":"image = np.stack((image,) * 3, -1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"11987158564d3979d0f75b63f489b61cda2a6a61"},"cell_type":"code","source":"boxes, scores, labels = retinanet.predict_on_batch(np.expand_dims(image, axis=0))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"15bf8a66cf67dd0b0f6989f5cb7a558c3a2c12b0"},"cell_type":"code","source":"import cv2\ndraw = image.copy()\ndraw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"590b71e085d328308f62ef2a8f992884f2effc19"},"cell_type":"code","source":"from keras_retinanet.utils.colors import label_color\nfrom keras_retinanet.utils.visualization import draw_box, draw_caption","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"369865c9f9cc45b6c2a49447bda5841e86a28a85"},"cell_type":"code","source":"labels_to_names = {0:'opcity'}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d80faea5f5398d2aea9e2f77ff33134e89abe300"},"cell_type":"code","source":"import matplotlib.pyplot as plt\n# visualize detections\nfor box, score, label in zip(boxes[0], scores[0], labels[0]):\n    # scores are sorted so we can break\n    if score < 0.5:\n        break\n        \n    color = label_color(label)\n    \n    b = box.astype(int)\n    draw_box(draw, b, color=color)\n    \n    caption = \"{} {:.3f}\".format(labels_to_names[label], score)\n    draw_caption(draw, b, caption)\n    \nplt.figure(figsize=(15, 15))\nplt.axis('off')\nplt.imshow(draw)\nplt.show()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}