{"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":"# Importing Required packages\nimport os\nimport torch\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom IPython.display import Image, clear_output\nprint('Setup complete. Using torch %s %s' % (torch.__version__, torch.cuda.get_device_properties(0) if torch.cuda.is_available() else 'CPU'))\nclear_output()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T08:14:39.041562Z","iopub.execute_input":"2021-06-25T08:14:39.041919Z","iopub.status.idle":"2021-06-25T08:14:39.583898Z","shell.execute_reply.started":"2021-06-25T08:14:39.041881Z","shell.execute_reply":"2021-06-25T08:14:39.582899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install clearml","metadata":{"execution":{"iopub.status.busy":"2021-06-25T06:29:29.988233Z","iopub.execute_input":"2021-06-25T06:29:29.988563Z","iopub.status.idle":"2021-06-25T06:29:39.382885Z","shell.execute_reply.started":"2021-06-25T06:29:29.988513Z","shell.execute_reply":"2021-06-25T06:29:39.382007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from clearml import Task\ntask = Task.init(project_name='objectdetection', task_name='hello world')","metadata":{"execution":{"iopub.status.busy":"2021-06-25T08:14:52.706267Z","iopub.execute_input":"2021-06-25T08:14:52.706611Z","iopub.status.idle":"2021-06-25T08:14:57.05626Z","shell.execute_reply.started":"2021-06-25T08:14:52.70658Z","shell.execute_reply":"2021-06-25T08:14:57.054297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#customize iPython writefile so we can write variables\nfrom IPython.core.magic import register_line_cell_magic\n\n@register_line_cell_magic\ndef writetemplate(line, cell):\n    with open(line, 'w') as f:\n        f.write(cell.format(**globals()))","metadata":{"execution":{"iopub.status.busy":"2021-06-25T08:15:02.479912Z","iopub.execute_input":"2021-06-25T08:15:02.480232Z","iopub.status.idle":"2021-06-25T08:15:02.488898Z","shell.execute_reply.started":"2021-06-25T08:15:02.480199Z","shell.execute_reply":"2021-06-25T08:15:02.487934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Cloning the repo and installing requiements\n!git clone https://github.com/ultralytics/yolov5.git\n!mv ./yolov5/* ./\n!pip install -r requirements.txt\nclear_output()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T08:15:07.881711Z","iopub.execute_input":"2021-06-25T08:15:07.882047Z","iopub.status.idle":"2021-06-25T08:15:15.109801Z","shell.execute_reply.started":"2021-06-25T08:15:07.882016Z","shell.execute_reply":"2021-06-25T08:15:15.108927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Copying the dataset to working directory\n!mkdir Dataset\n!cp ../input/covid19-detection-for-yolov5-siimfisabiorsna/Covid19 -r ./Dataset\nclear_output()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T06:30:31.507908Z","iopub.execute_input":"2021-06-25T06:30:31.508242Z","iopub.status.idle":"2021-06-25T06:31:15.093169Z","shell.execute_reply.started":"2021-06-25T06:30:31.50821Z","shell.execute_reply":"2021-06-25T06:31:15.092162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir('./Dataset/Covid19/images'))","metadata":{"execution":{"iopub.status.busy":"2021-06-25T06:32:04.099611Z","iopub.execute_input":"2021-06-25T06:32:04.100062Z","iopub.status.idle":"2021-06-25T06:32:04.120144Z","shell.execute_reply.started":"2021-06-25T06:32:04.100018Z","shell.execute_reply":"2021-06-25T06:32:04.117201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writetemplate ./split_dataset.py\nfrom utils.datasets import * \nautosplit('./Dataset/Covid19', weights=(0.8, 0.2, 0.0))","metadata":{"execution":{"iopub.status.busy":"2021-06-25T06:32:11.222454Z","iopub.execute_input":"2021-06-25T06:32:11.222809Z","iopub.status.idle":"2021-06-25T06:32:11.228868Z","shell.execute_reply.started":"2021-06-25T06:32:11.222778Z","shell.execute_reply":"2021-06-25T06:32:11.227728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Sptting the dataset for training and validation using datasets from yolov5 repo","metadata":{}},{"cell_type":"code","source":"!python split_dataset.py","metadata":{"execution":{"iopub.status.busy":"2021-06-25T06:32:13.985853Z","iopub.execute_input":"2021-06-25T06:32:13.986182Z","iopub.status.idle":"2021-06-25T06:32:17.16133Z","shell.execute_reply.started":"2021-06-25T06:32:13.986154Z","shell.execute_reply":"2021-06-25T06:32:17.160359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('./Dataset/Covid19')","metadata":{"execution":{"iopub.status.busy":"2021-06-25T06:32:17.162981Z","iopub.execute_input":"2021-06-25T06:32:17.163341Z","iopub.status.idle":"2021-06-25T06:32:17.172839Z","shell.execute_reply.started":"2021-06-25T06:32:17.163311Z","shell.execute_reply":"2021-06-25T06:32:17.17176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir DataFile","metadata":{"execution":{"iopub.status.busy":"2021-06-25T06:32:19.856694Z","iopub.execute_input":"2021-06-25T06:32:19.85708Z","iopub.status.idle":"2021-06-25T06:32:20.498442Z","shell.execute_reply.started":"2021-06-25T06:32:19.857046Z","shell.execute_reply":"2021-06-25T06:32:20.497303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writetemplate ./DataFile/data.yaml\n\ntrain: ./Dataset/Covid19/autosplit_train.txt\nval: ./Dataset/Covid19/autosplit_val.txt\n\nnc: 4\nnames: ['Negative for Pneumonia', 'Typical Appearance',\n        'Indeterminate Appearance', 'Atypical Appearance']","metadata":{"execution":{"iopub.status.busy":"2021-06-25T06:32:23.015278Z","iopub.execute_input":"2021-06-25T06:32:23.015631Z","iopub.status.idle":"2021-06-25T06:32:23.020372Z","shell.execute_reply.started":"2021-06-25T06:32:23.015593Z","shell.execute_reply":"2021-06-25T06:32:23.019552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Declaration\n\nyou can find all the models [here](https://github.com/ultralytics/yolov5/tree/master/models) and cutomize the classes accordingly.","metadata":{}},{"cell_type":"code","source":"%%writetemplate ./models/custom_yolov5x.yaml\n\n\n# parameters\nnc: 4  # number of classes\ndepth_multiple: 1.33  # model depth multiple\nwidth_multiple: 1.25  # layer channel multiple\n\n# anchors\nanchors:\n  - [10,13, 16,30, 33,23]  # P3/8\n  - [30,61, 62,45, 59,119]  # P4/16\n  - [116,90, 156,198, 373,326]  # P5/32\n\n# YOLOv5 backbone\nbackbone:\n  # [from, number, module, args]\n  [[-1, 1, Focus, [64, 3]],  # 0-P1/2\n   [-1, 1, Conv, [128, 3, 2]],  # 1-P2/4\n   [-1, 3, C3, [128]],\n   [-1, 1, Conv, [256, 3, 2]],  # 3-P3/8\n   [-1, 9, C3, [256]],\n   [-1, 1, Conv, [512, 3, 2]],  # 5-P4/16\n   [-1, 9, C3, [512]],\n   [-1, 1, Conv, [1024, 3, 2]],  # 7-P5/32\n   [-1, 1, SPP, [1024, [5, 9, 13]]],\n   [-1, 3, C3, [1024, False]],  # 9\n  ]\n\n# YOLOv5 head\nhead:\n  [[-1, 1, Conv, [512, 1, 1]],\n   [-1, 1, nn.Upsample, [None, 2, 'nearest']],\n   [[-1, 6], 1, Concat, [1]],  # cat backbone P4\n   [-1, 3, C3, [512, False]],  # 13\n\n   [-1, 1, Conv, [256, 1, 1]],\n   [-1, 1, nn.Upsample, [None, 2, 'nearest']],\n   [[-1, 4], 1, Concat, [1]],  # cat backbone P3\n   [-1, 3, C3, [256, False]],  # 17 (P3/8-small)\n\n   [-1, 1, Conv, [256, 3, 2]],\n   [[-1, 14], 1, Concat, [1]],  # cat head P4\n   [-1, 3, C3, [512, False]],  # 20 (P4/16-medium)\n\n   [-1, 1, Conv, [512, 3, 2]],\n   [[-1, 10], 1, Concat, [1]],  # cat head P5\n   [-1, 3, C3, [1024, False]],  # 23 (P5/32-large)\n\n   [[17, 20, 23], 1, Detect, [nc, anchors]],  # Detect(P3, P4, P5)\n  ]","metadata":{"execution":{"iopub.status.busy":"2021-06-25T06:33:27.150524Z","iopub.execute_input":"2021-06-25T06:33:27.150966Z","iopub.status.idle":"2021-06-25T06:33:27.16073Z","shell.execute_reply.started":"2021-06-25T06:33:27.150924Z","shell.execute_reply":"2021-06-25T06:33:27.159746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import wandb\nwandb.login()","metadata":{"execution":{"iopub.status.busy":"2021-06-25T06:34:44.903417Z","iopub.execute_input":"2021-06-25T06:34:44.903791Z","iopub.status.idle":"2021-06-25T06:35:00.757812Z","shell.execute_reply.started":"2021-06-25T06:34:44.903755Z","shell.execute_reply":"2021-06-25T06:35:00.756952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training the model","metadata":{}},{"cell_type":"code","source":"### %%time\n\n!python train.py --img-size 602 --batch 10 --epochs 3 --data './DataFile/data.yaml' --cfg ./models/custom_yolov5x.yaml --weights yolov5x.pt --name demo1  --cache","metadata":{"execution":{"iopub.status.busy":"2021-06-25T08:15:31.513577Z","iopub.execute_input":"2021-06-25T08:15:31.513903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%load_ext tensorboard\n%tensorboard --logdir runs","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analyze the result","metadata":{}},{"cell_type":"code","source":"os.listdir('./runs/train/')","metadata":{"execution":{"iopub.status.busy":"2021-06-25T07:36:02.496118Z","iopub.execute_input":"2021-06-25T07:36:02.496435Z","iopub.status.idle":"2021-06-25T07:36:02.512385Z","shell.execute_reply.started":"2021-06-25T07:36:02.496403Z","shell.execute_reply":"2021-06-25T07:36:02.510645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image('./runs/train/experiment2/results.png')","metadata":{"execution":{"iopub.status.busy":"2021-06-24T16:42:27.945792Z","iopub.execute_input":"2021-06-24T16:42:27.946197Z","iopub.status.idle":"2021-06-24T16:42:27.967937Z","shell.execute_reply.started":"2021-06-24T16:42:27.946152Z","shell.execute_reply":"2021-06-24T16:42:27.967125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image('./runs/train/experiment2/confusion_matrix.png',width=400)","metadata":{"execution":{"iopub.status.busy":"2021-06-24T16:42:33.603763Z","iopub.execute_input":"2021-06-24T16:42:33.604087Z","iopub.status.idle":"2021-06-24T16:42:33.618588Z","shell.execute_reply.started":"2021-06-24T16:42:33.604058Z","shell.execute_reply":"2021-06-24T16:42:33.617544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image('./runs/train/experiment2/test_batch1_labels.jpg')","metadata":{"execution":{"iopub.status.busy":"2021-06-24T16:42:45.581734Z","iopub.execute_input":"2021-06-24T16:42:45.582048Z","iopub.status.idle":"2021-06-24T16:42:45.594678Z","shell.execute_reply.started":"2021-06-24T16:42:45.582015Z","shell.execute_reply":"2021-06-24T16:42:45.593686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('./Dataset/Covid19')","metadata":{"execution":{"iopub.status.busy":"2021-06-24T16:42:58.335575Z","iopub.execute_input":"2021-06-24T16:42:58.335888Z","iopub.status.idle":"2021-06-24T16:42:58.343787Z","shell.execute_reply.started":"2021-06-24T16:42:58.335858Z","shell.execute_reply":"2021-06-24T16:42:58.342762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Detecting the classes ","metadata":{}},{"cell_type":"code","source":"!python detect.py --img-size 602  --conf 0.05 --source ../input/covid19-detection-for-yolov5-siimfisabiorsna/Covid19/images/1014_0.jpg --weights ./runs/train/experiment2/weights/best.pt","metadata":{"execution":{"iopub.status.busy":"2021-06-24T16:45:20.152052Z","iopub.execute_input":"2021-06-24T16:45:20.152486Z","iopub.status.idle":"2021-06-24T16:45:26.918715Z","shell.execute_reply.started":"2021-06-24T16:45:20.152445Z","shell.execute_reply":"2021-06-24T16:45:26.91771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image('./runs/detect/exp4/1014_0.jpg')","metadata":{"execution":{"iopub.status.busy":"2021-06-24T16:45:40.95906Z","iopub.execute_input":"2021-06-24T16:45:40.959423Z","iopub.status.idle":"2021-06-24T16:45:40.967594Z","shell.execute_reply.started":"2021-06-24T16:45:40.95938Z","shell.execute_reply":"2021-06-24T16:45:40.966622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}