{"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":"<div class='alert alert-info' style='text-align: center'><h1>YOLOv5 Brain Tumor Object Detection - Train</h1>\n- yet another MR processing notebook -</div>\n\n#### This notebook is a train script for object detecting brain tumors in all three planes.\n#### It trains both Object Detection and Classification (pos/neg for MGMT status).\n#### The dataset contains ~400 images, classification labels and bounding box coordinates for each plane of the T1wCE series.\n#### The datasets are split into train/test sets.\n#### I exported the JPGs from the RSNA-MICCAI brain MR dataset and hand labeled tumors the using https://makesense.ai\n\n- Dataset -> https://www.kaggle.com/davidbroberts/brain-tumor-object-detection-datasets\n- Inference Notebook -> https://www.kaggle.com/davidbroberts/brain-tumor-object-detection\n\n- * I set the epochs to 5 for quick saving, you'll probably want to do many more.\n- * Classification is not very accurate when compared to the patient's actual MGMT status. \n- * You might increase the number of images to make it more accurate (but I doubt it will help using the RSNA dataset).","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-04T02:41:06.342738Z","iopub.execute_input":"2021-10-04T02:41:06.343074Z","iopub.status.idle":"2021-10-04T02:41:19.656073Z","shell.execute_reply.started":"2021-10-04T02:41:06.342990Z","shell.execute_reply":"2021-10-04T02:41:19.655114Z"}}},{"cell_type":"code","source":"# Clone and install YOLOv5\n!git clone https://github.com/ultralytics/yolov5\n%cd yolov5\n%pip install -qr requirements.txt\n%cd ../","metadata":{"execution":{"iopub.status.busy":"2021-10-04T03:30:49.834095Z","iopub.execute_input":"2021-10-04T03:30:49.834839Z","iopub.status.idle":"2021-10-04T03:31:00.623186Z","shell.execute_reply.started":"2021-10-04T03:30:49.834755Z","shell.execute_reply":"2021-10-04T03:31:00.622368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ['WANDB_MODE']=\"disabled\"\nfrom IPython.display import FileLink\nimport shutil\nfrom shutil import copyfile","metadata":{"execution":{"iopub.status.busy":"2021-10-04T03:31:00.625250Z","iopub.execute_input":"2021-10-04T03:31:00.625776Z","iopub.status.idle":"2021-10-04T03:31:00.632726Z","shell.execute_reply.started":"2021-10-04T03:31:00.625735Z","shell.execute_reply":"2021-10-04T03:31:00.632025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make a directory to copy the best weights file to\nif not os.path.exists('/kaggle/working/output'):\n    os.mkdir('/kaggle/working/output')","metadata":{"execution":{"iopub.status.busy":"2021-10-04T03:31:00.634175Z","iopub.execute_input":"2021-10-04T03:31:00.634526Z","iopub.status.idle":"2021-10-04T03:31:00.640402Z","shell.execute_reply.started":"2021-10-04T03:31:00.634490Z","shell.execute_reply":"2021-10-04T03:31:00.639550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Specify the series and plane (only the T1wCE series is currently available in the dataset)\nSERIES = 't1wce'\nPLANE = 'axial'","metadata":{"execution":{"iopub.status.busy":"2021-10-04T03:31:00.642543Z","iopub.execute_input":"2021-10-04T03:31:00.642953Z","iopub.status.idle":"2021-10-04T03:31:00.648949Z","shell.execute_reply.started":"2021-10-04T03:31:00.642920Z","shell.execute_reply":"2021-10-04T03:31:00.648119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Call the YOLO train script\n!python yolov5/train.py --img 512 --batch 32 --epochs 5 --data '../input/brain-tumor-object-detection-datasets/{PLANE}_{SERIES}_2_class/{PLANE}_{SERIES}_2_class.yaml' --weights yolov5/yolov5s.pt --cache","metadata":{"execution":{"iopub.status.busy":"2021-10-04T03:31:00.650419Z","iopub.execute_input":"2021-10-04T03:31:00.651008Z","iopub.status.idle":"2021-10-04T03:32:19.960910Z","shell.execute_reply.started":"2021-10-04T03:31:00.650966Z","shell.execute_reply":"2021-10-04T03:32:19.960086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Copy the pt file and zip it up\n# Example usage of the exported model: https://www.kaggle.com/davidbroberts/brain-tumor-object-detection\n\ncopyfile('yolov5/runs/train/exp/weights/best.pt', f'/kaggle/working/output/{PLANE}_{SERIES}_2_class.pt')\nshutil.make_archive(f'{PLANE}_{SERIES}', 'zip', '/kaggle/working/output')\n\nFileLink(f'{PLANE}_{SERIES}.zip')","metadata":{"execution":{"iopub.status.busy":"2021-10-04T03:32:19.964392Z","iopub.execute_input":"2021-10-04T03:32:19.964642Z","iopub.status.idle":"2021-10-04T03:32:20.630840Z","shell.execute_reply.started":"2021-10-04T03:32:19.964611Z","shell.execute_reply":"2021-10-04T03:32:20.629993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Some of my other MR processing notebooks:\n\n- Brain Tumor Object Detection -> https://www.kaggle.com/davidbroberts/brain-tumor-object-detection\n- Determining MR image planes -> https://www.kaggle.com/davidbroberts/determining-mr-image-planes\n- Determining MR Slice Orientation -> https://www.kaggle.com/davidbroberts/determining-mr-slice-orientation\n- Determining DICOM image order -> https://www.kaggle.com/davidbroberts/determining-dicom-image-order\n- Reference Lines on MR images -> https://www.kaggle.com/davidbroberts/mr-reference-lines\n- Manual VOI LUT on MR images -> https://www.kaggle.com/davidbroberts/manual-voi-lut-on-mr-images\n- Standardizing MR Images -> https://www.kaggle.com/davidbroberts/standardizing-mr-images\n- Export DICOM Images by Plane -> https://www.kaggle.com/davidbroberts/export-dicom-series-by-plane/","metadata":{}}]}