{"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-20T02:43:29.631729Z","iopub.execute_input":"2022-01-20T02:43:29.632036Z","iopub.status.idle":"2022-01-20T02:43:31.128694Z","shell.execute_reply.started":"2022-01-20T02:43:29.631999Z","shell.execute_reply":"2022-01-20T02:43:31.127957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-20T02:43:31.130607Z","iopub.execute_input":"2022-01-20T02:43:31.131112Z","iopub.status.idle":"2022-01-20T02:43:31.135094Z","shell.execute_reply.started":"2022-01-20T02:43:31.131073Z","shell.execute_reply":"2022-01-20T02:43:31.134437Z"},"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-20T02:43:31.136704Z","iopub.execute_input":"2022-01-20T02:43:31.137354Z","iopub.status.idle":"2022-01-20T02:43:31.200767Z","shell.execute_reply.started":"2022-01-20T02:43:31.137285Z","shell.execute_reply":"2022-01-20T02:43:31.200103Z"},"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-20T02:43:31.201762Z","iopub.execute_input":"2022-01-20T02:43:31.201989Z","iopub.status.idle":"2022-01-20T02:43:33.817068Z","shell.execute_reply.started":"2022-01-20T02:43:31.201959Z","shell.execute_reply":"2022-01-20T02:43:33.816159Z"},"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-20T02:43:33.820428Z","iopub.execute_input":"2022-01-20T02:43:33.821064Z"},"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/cv516Buaa/tph-yolov5.git","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%pip install timm","metadata":{"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":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ./tph-yolov5","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python train.py --img 1920 --batch 1 --epochs 5 --data reef_f1_naive.yaml --weights yolov5l.pt --name l6_3600_uflip_vm5_f1 --hyp data/hyps/hyp.scratch-high.yaml","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}