{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import math\nimport os\nimport shutil\nimport sys\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport glob\nimport pydicom\nimport cv2\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"random_stat = 123\nnp.random.seed(random_stat)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ff3cb9af44731fe1615165e8874a52466e6379be"},"cell_type":"code","source":"!git clone https://github.com/pjreddie/darknet.git\n\n# Build gpu version darknet\n!cd darknet && sed '1 s/^.*$/GPU=1/; 2 s/^.*$/CUDNN=1/' -i Makefile\n\n# -j <The # of cpu cores to use>. Chang 999 to fit your environment. Actually i used '-j 50'.\n!cd darknet && make -j 999 -s\n!cp darknet/darknet darknet_gpu","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4c38747412b48855bdf60d75b68a2113df9a2a9f"},"cell_type":"code","source":"DATA_DIR = \"../input\"\n\ntrain_dcm_dir = os.path.join(DATA_DIR, \"stage_2_train_images\")\ntest_dcm_dir = os.path.join(DATA_DIR, \"stage_2_test_images\")\n\nimg_dir = os.path.join(os.getcwd(), \"images\")  # .jpg\nlabel_dir = os.path.join(os.getcwd(), \"labels\")  # .txt\nmetadata_dir = os.path.join(os.getcwd(), \"metadata\") # .txt\n\n# YOLOv3 config file directory\ncfg_dir = os.path.join(os.getcwd(), \"cfg\")\n# YOLOv3 training checkpoints will be saved here\nbackup_dir = os.path.join(os.getcwd(), \"backup\")\n\nfor directory in [img_dir, label_dir, metadata_dir, cfg_dir, backup_dir]:\n    if os.path.isdir(directory):\n        continue\n    os.mkdir(directory)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b311e32d6c1e5b1121503c9e4626591b23538e43"},"cell_type":"code","source":"!wget --no-check-certificate -q \"https://docs.google.com/uc?export=download&id=1-KTV7K9G1bl3SmnLnzmpkDyNt6tDmH7j\" -O darknet.py","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"db26d1321d395fe53f94f6f139982eb1201f8e13"},"cell_type":"code","source":"from darknet import *","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bce7c1290d0d4347d6344b956b79b7f081b874f3"},"cell_type":"code","source":"threshold = 0.2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fa81c485e1df63a1da77c29d999dac3c6efc4abb"},"cell_type":"code","source":"submit_file_path = \"submission.csv\"\ncfg_path = os.path.join(cfg_dir, \"rsna_yolov3.cfg_test\")\n# weight_path = os.path.join(backup_dir, \"rsna_yolov3_15300.weights\")\nweight_path = os.path.join(backup_dir, \"rsna_yolov3_final.weights\")\n\ntest_img_list_path = os.path.join(metadata_dir, \"te_list.txt\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5907dd83818026e6ce610d33b466f93cd7685774"},"cell_type":"code","source":"gpu_index = 0\nnet = load_net(cfg_path.encode(),\n               weight_path.encode(), \n               gpu_index)\nmeta = load_meta(data_extention_file_path.encode())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"03c86c494c64238f928c09798f5f5a34859ac292"},"cell_type":"code","source":"submit_dict = {\"patientId\": [], \"PredictionString\": []}\n\nwith open(test_img_list_path, \"r\") as test_img_list_f:\n    # tqdm run up to 1000(The # of test set)\n    for line in tqdm(test_img_list_f):\n        patient_id = line.strip().split('/')[-1].strip().split('.')[0]\n\n        infer_result = detect(net, meta, line.strip().encode(), thresh=threshold)\n\n        submit_line = \"\"\n        for e in infer_result:\n            confi = e[1]\n            w = e[2][2]\n            h = e[2][3]\n            x = e[2][0]-w/2\n            y = e[2][1]-h/2\n            submit_line += \"{} {} {} {} {} \".format(confi, x, y, w, h)\n\n        submit_dict[\"patientId\"].append(patient_id)\n        submit_dict[\"PredictionString\"].append(submit_line)\n\npd.DataFrame(submit_dict).to_csv(submit_file_path, index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4f2f41b118dc081cd64ea9b3c639bc3cf4158fb4"},"cell_type":"code","source":"# !ls -lsht\n# !rm -rf darknet images labels metadata backup cfg\n# !rm -rf train_log.txt darknet53.conv.74 darknet.py darknet_gpu\n# !rm -rf test.jpg\n# !rm -rf __pycache__ .ipynb_checkpoints\n\n!rm -rf darknet images labels metadata\n!rm -rf darknet53.conv.74 darknet.py darknet_gpu\n!rm -rf test.jpg\n!rm -rf __pycache__ .ipynb_checkpoints","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"38e01b84b332870b939a6b846e789b6d2f2ad1fd"},"cell_type":"code","source":"# !ls -alsht\n!ls ","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}