{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport torch\nfrom tqdm import tqdm\nimport sys\nimport numpy as np\nfrom tqdm.notebook import tqdm\ntqdm.pandas()\nimport pandas as pd\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport glob\nimport shutil\nimport sys\nsys.path.append('../input/tensorflow-great-barrier-reef')\nimport torch\nfrom PIL import Image\nimport ast\nsys.path.append('../input/tensorflow-great-barrier-reef')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-23T16:59:25.895187Z","iopub.execute_input":"2022-01-23T16:59:25.895588Z","iopub.status.idle":"2022-01-23T16:59:27.556337Z","shell.execute_reply.started":"2022-01-23T16:59:25.895478Z","shell.execute_reply":"2022-01-23T16:59:27.555602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom shutil import copyfile","metadata":{"execution":{"iopub.status.busy":"2022-01-23T16:59:27.558202Z","iopub.execute_input":"2022-01-23T16:59:27.558469Z","iopub.status.idle":"2022-01-23T16:59:27.562598Z","shell.execute_reply.started":"2022-01-23T16:59:27.558434Z","shell.execute_reply":"2022-01-23T16:59:27.561804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/tensorflow-great-barrier-reef/train.csv')\ntrain['pos'] = train.annotations != '[]'","metadata":{"execution":{"iopub.status.busy":"2022-01-23T16:59:27.563785Z","iopub.execute_input":"2022-01-23T16:59:27.564151Z","iopub.status.idle":"2022-01-23T16:59:27.631024Z","shell.execute_reply.started":"2022-01-23T16:59:27.564118Z","shell.execute_reply":"2022-01-23T16:59:27.630339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir -p ./yolo_data/fold1/images/val\n!mkdir -p ./yolo_data/fold1/images/train\n!mkdir -p ./yolo_data/fold1/labels/val\n!mkdir -p ./yolo_data/fold1/labels/train","metadata":{"execution":{"iopub.status.busy":"2022-01-23T16:59:27.633228Z","iopub.execute_input":"2022-01-23T16:59:27.633615Z","iopub.status.idle":"2022-01-23T16:59:30.353594Z","shell.execute_reply.started":"2022-01-23T16:59:27.633577Z","shell.execute_reply":"2022-01-23T16:59:30.352672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fold = 1\n\nannos = []\nfor i, x in train.iterrows():\n    if x.video_id == fold:\n        mode = 'val'\n    else:\n        # train\n        mode = 'train'\n        if not x.pos: continue\n        # val\n    copyfile(f'../input/tensorflow-great-barrier-reef/train_images/video_{x.video_id}/{x.video_frame}.jpg',\n                f'./yolo_data/fold{fold}/images/{mode}/{x.image_id}.jpg')\n    if not x.pos:\n        continue\n    r = ''\n    anno = eval(x.annotations)\n    for an in anno:\n#            annos.append(an)\n        r += '0 {} {} {} {}\\n'.format((an['x'] + an['width'] / 2) / 1280,\n                                        (an['y'] + an['height'] / 2) / 720,\n                                        an['width'] / 1280, an['height'] / 720)\n    with open(f'./yolo_data/fold{fold}/labels/{mode}/{x.image_id}.txt', 'w') as fp:\n        fp.write(r)","metadata":{"execution":{"iopub.status.busy":"2022-01-23T16:59:30.357809Z","iopub.execute_input":"2022-01-23T16:59:30.358070Z","iopub.status.idle":"2022-01-23T17:01:48.724137Z","shell.execute_reply.started":"2022-01-23T16:59:30.358041Z","shell.execute_reply":"2022-01-23T17:01:48.723345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = '''\n# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]\npath: ../yolo_data/fold1/  # dataset root dir\ntrain: images/train  # train images (relative to 'path') 128 images\nval: images/val  # val images (relative to 'path') 128 images\ntest:  # test images (optional)\n\n# Classes\nnc: 1  # number of classes\nnames: ['starfish']  # class names\n\n\n# Download script/URL (optional)\n# download: https://ultralytics.com/assets/coco128.zip\n'''","metadata":{"execution":{"iopub.status.busy":"2022-01-23T17:01:48.727141Z","iopub.execute_input":"2022-01-23T17:01:48.728477Z","iopub.status.idle":"2022-01-23T17:01:48.732383Z","shell.execute_reply.started":"2022-01-23T17:01:48.728435Z","shell.execute_reply":"2022-01-23T17:01:48.731723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/cv516Buaa/tph-yolov5.git","metadata":{"execution":{"iopub.status.busy":"2022-01-23T17:01:48.733453Z","iopub.execute_input":"2022-01-23T17:01:48.733904Z","iopub.status.idle":"2022-01-23T17:01:52.068641Z","shell.execute_reply.started":"2022-01-23T17:01:48.733865Z","shell.execute_reply":"2022-01-23T17:01:52.067779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pip install timm","metadata":{"execution":{"iopub.status.busy":"2022-01-23T17:01:52.070145Z","iopub.execute_input":"2022-01-23T17:01:52.070514Z","iopub.status.idle":"2022-01-23T17:02:03.503300Z","shell.execute_reply.started":"2022-01-23T17:01:52.070476Z","shell.execute_reply":"2022-01-23T17:02:03.502398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('./tph-yolov5/data/reef_f1_naive.yaml', 'w') as fp:\n    fp.write(data)","metadata":{"execution":{"iopub.status.busy":"2022-01-23T17:02:03.505711Z","iopub.execute_input":"2022-01-23T17:02:03.505985Z","iopub.status.idle":"2022-01-23T17:02:03.509849Z","shell.execute_reply.started":"2022-01-23T17:02:03.505945Z","shell.execute_reply":"2022-01-23T17:02:03.509173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ./tph-yolov5","metadata":{"execution":{"iopub.status.busy":"2022-01-23T17:02:03.511441Z","iopub.execute_input":"2022-01-23T17:02:03.511998Z","iopub.status.idle":"2022-01-23T17:02:03.521048Z","shell.execute_reply.started":"2022-01-23T17:02:03.511962Z","shell.execute_reply":"2022-01-23T17:02:03.520259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python train.py --img 1920 --batch 1 --epochs 6 --data reef_f1_naive.yaml --weights yolov5l.pt --name l6_3600_uflip_vm5_f1 --hyp data/hyps/hyp.scratch-high.yaml","metadata":{"execution":{"iopub.status.busy":"2022-01-23T17:12:54.789065Z","iopub.execute_input":"2022-01-23T17:12:54.789404Z","iopub.status.idle":"2022-01-24T00:24:11.644644Z","shell.execute_reply.started":"2022-01-23T17:12:54.789365Z","shell.execute_reply":"2022-01-24T00:24:11.642839Z"},"trusted":true},"execution_count":null,"outputs":[]}]}