{"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)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"dcd9a36f-7d4b-4d98-ab0b-426f1e64d724","_cell_guid":"bb94a464-4b1d-44b6-a3df-2aeedaf77d0c","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov5  # clone\n%cd yolov5\n%pip install -qr requirements.txt  # install\n\nimport torch\nfrom yolov5 import utils\ndisplay = utils.notebook_init()  # checks","metadata":{"execution":{"iopub.status.busy":"2022-01-25T05:22:02.362357Z","iopub.execute_input":"2022-01-25T05:22:02.362838Z","iopub.status.idle":"2022-01-25T05:22:19.858725Z","shell.execute_reply.started":"2022-01-25T05:22:02.362731Z","shell.execute_reply":"2022-01-25T05:22:19.857424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file=open('/kaggle/working/yolov5/my.yaml',\"w\")\nfile.write('train: /kaggle/working/images/train\\n')\nfile.write('val: /kaggle/working/images/val\\n')\nfile.write('nc: 1\\n')\nfile.write('names: [\\'COTS\\']')\nfile.close()","metadata":{"execution":{"iopub.status.busy":"2022-01-25T05:22:24.597034Z","iopub.execute_input":"2022-01-25T05:22:24.598067Z","iopub.status.idle":"2022-01-25T05:22:24.606736Z","shell.execute_reply.started":"2022-01-25T05:22:24.598025Z","shell.execute_reply":"2022-01-25T05:22:24.605704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file=open('/kaggle/working/yolov5/models/yolov5m.yaml','r')\nstring=file.readlines()\nfile.close()\nstring[3]='nc: 1  # number of classes\\n'\nfile=open('/kaggle/working/yolov5/models/yolov5m.yaml','w')\nfor j in range(int(len(string))):\n    file.write(string[j])\nfile.close()\n    \n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-25T05:26:32.114408Z","iopub.execute_input":"2022-01-25T05:26:32.114858Z","iopub.status.idle":"2022-01-25T05:26:32.123916Z","shell.execute_reply.started":"2022-01-25T05:26:32.114805Z","shell.execute_reply":"2022-01-25T05:26:32.122889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    os.mkdir('/kaggle/working/images')\n    os.mkdir('/kaggle/working/images/val')\n    os.mkdir('/kaggle/working/images/train')\n    os.mkdir('/kaggle/working/labels')\n    os.mkdir('/kaggle/working/labels/train')\n    os.mkdir('/kaggle/working/labels/val')","metadata":{"_uuid":"71d0fc77-02a7-46c9-bc87-3493cb15baa3","_cell_guid":"02e893b9-8fc0-4399-8dfb-a8bbd9023520","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-01-25T05:23:39.231569Z","iopub.execute_input":"2022-01-25T05:23:39.231869Z","iopub.status.idle":"2022-01-25T05:23:39.238347Z","shell.execute_reply.started":"2022-01-25T05:23:39.231836Z","shell.execute_reply":"2022-01-25T05:23:39.237013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    import shutil    \n","metadata":{"_uuid":"f04bd508-d1da-440c-82b8-504647934cc4","_cell_guid":"159973ca-96b7-49d2-9f8a-0c1aed80357e","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-01-25T05:23:44.479082Z","iopub.execute_input":"2022-01-25T05:23:44.481809Z","iopub.status.idle":"2022-01-25T05:23:44.488066Z","shell.execute_reply.started":"2022-01-25T05:23:44.481773Z","shell.execute_reply":"2022-01-25T05:23:44.4869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def yeniden(a):\n    data=pd.read_csv('/kaggle/input/tensorflow-great-barrier-reef/train.csv')\n    DIR_test='/kaggle/working/images/val/'\n    DIR_train='/kaggle/working/images/train/'\n   \n         \n    i=0\n    while i in data.index:\n            if data.annotations[i]=='[]':\n                data= data.drop(i)\n            i+=1  \n    k=[]\n    \n    for i in data.index:\n        k.append('/kaggle/input/tensorflow-great-barrier-reef/train_images/video_'+ data.video_id[i].astype(str)+'/'+data.video_frame[i].astype(str)+'.jpg')\n    \n    data.insert(6,'old_image_path',k)\n    \n    k=[]\n    \n    for i in data.index:\n        k.append('/kaggle/working/images/train/'+data.video_frame[i].astype(str)+'.jpg')\n    \n    data.insert(7,'new_image_path',k)\n    \n    from sklearn.model_selection import train_test_split\n    X_train, X_test = train_test_split(data.old_image_path,test_size=0.30,random_state=69)\n    j=0\n    if a==True:\n        for j in X_train.index:\n            shutil.copyfile(X_train[j] ,DIR_train+data.video_frame[j].astype(str)+'.jpg' )\n        for j in X_test.index:\n            shutil.copyfile(X_test[j] ,DIR_test+data.video_frame[j].astype(str)+'.jpg' )\n    print('finished process')        \n    return data,X_train,X_test","metadata":{"_uuid":"4b33b9cd-60f6-4fb9-bc4d-ff42791be529","_cell_guid":"d426e5f6-d50c-44a6-8eb2-9d53feb5e311","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-01-25T05:23:48.276057Z","iopub.execute_input":"2022-01-25T05:23:48.27679Z","iopub.status.idle":"2022-01-25T05:23:48.28844Z","shell.execute_reply.started":"2022-01-25T05:23:48.276756Z","shell.execute_reply":"2022-01-25T05:23:48.287098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def yenidenv2(data,control):\n    k=['[',']','x',':','y','\\'','w','i','d','t','h','e','g','t','{','}']\n    t=[]\n    if control:\n        data['width']=1289\n        data['height']=720\n        for j in data.index:\n            o=''.join(filter(lambda x: x  not in k,data.annotations[j]))\n            o.replace(\" \",\"\")\n            p=list(o.split(','))\n            h=map(float,p)\n            l=list(h)\n            t.append(l)\n        data['bbox']=t    \n    p=[]\n    for j in data.index:\n        m=[]\n        for n in range(int(len(data.bbox[j])/4)):\n            a=n*4\n            b=n*4+1\n            c=n*4+2\n            d=n*4+3\n            m.append((data.bbox[j][a]+data.bbox[j][c])/data.width[j])\n            m.append((data.bbox[j][b]+data.bbox[j][d])/data.width[j])\n        else:\n            p.append(m)\n    data[\"yolo_bbox\"]=p\n    print(\"finished process\")\n    return data","metadata":{"_uuid":"1d4de2dc-4c09-4bba-bb81-f027ef373e3c","_cell_guid":"f66eb503-558d-4d2d-8363-0f84261f56a5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-01-25T05:23:56.200971Z","iopub.execute_input":"2022-01-25T05:23:56.201826Z","iopub.status.idle":"2022-01-25T05:23:56.213389Z","shell.execute_reply.started":"2022-01-25T05:23:56.201786Z","shell.execute_reply":"2022-01-25T05:23:56.212165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data,X_train,X_test=yeniden(a=True)\ndata=yenidenv2(data,control=True)","metadata":{"_uuid":"c17796bf-62ff-4d6e-91d6-906c4c50b154","_cell_guid":"ce34913d-c123-4ff1-b139-0e5c47eb8267","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-01-25T05:24:01.428108Z","iopub.execute_input":"2022-01-25T05:24:01.429121Z","iopub.status.idle":"2022-01-25T05:25:35.28249Z","shell.execute_reply.started":"2022-01-25T05:24:01.429062Z","shell.execute_reply":"2022-01-25T05:25:35.281297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def labels(X_train,X_test):\n    for u in X_train.index:\n        n=0\n        while n < int(len(data.yolo_bbox[u])):\n            if n==0:\n                file=open('/kaggle/working/labels/train/'+data.video_frame[u].astype(str)+'.txt','w')\n            else:\n                file=open('/kaggle/working/labels/train/'+data.video_frame[u].astype(str)+'.txt','a')\n            d=n+1\n            file.write(\"0\"+\" \"+data.yolo_bbox[u][n].astype(str)+\" \"+data.yolo_bbox[u][d].astype(str)+\" \"+\"1\"+\" \"+\"1\"+\"\\n\")\n            n+=2\n            file.close()\n\n\n    for u in X_test.index:\n        n=0\n        while n < int(len(data.yolo_bbox[u])):\n            if n==0:\n                file=open('/kaggle/working/labels/val/'+data.video_frame[u].astype(str)+'.txt','w')\n            else:\n                file=open('/kaggle/working/labels/val/'+data.video_frame[u].astype(str)+'.txt','a')\n            d=n+1\n            file.write(\"0\"+\" \"+data.yolo_bbox[u][n].astype(str)+\" \"+data.yolo_bbox[u][d].astype(str)+\" \"+\"1\"+\" \"+\"1\"+\"\\n\")\n            n+=2\n            file.close()\n    print(\"finished\")    ","metadata":{"_uuid":"0d122483-b68f-46c0-926b-fec4d94d0665","_cell_guid":"0580997c-e662-4f79-a829-d4eddc586f03","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2022-01-25T05:26:18.374819Z","iopub.execute_input":"2022-01-25T05:26:18.375177Z","iopub.status.idle":"2022-01-25T05:26:18.388732Z","shell.execute_reply.started":"2022-01-25T05:26:18.375143Z","shell.execute_reply":"2022-01-25T05:26:18.387639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels(X_train,X_test)","metadata":{"execution":{"iopub.status.busy":"2022-01-25T05:26:22.176104Z","iopub.execute_input":"2022-01-25T05:26:22.176748Z","iopub.status.idle":"2022-01-25T05:26:24.875994Z","shell.execute_reply.started":"2022-01-25T05:26:22.17671Z","shell.execute_reply":"2022-01-25T05:26:24.874906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\ntorch.cuda.empty_cache()\n","metadata":{"execution":{"iopub.status.busy":"2022-01-25T05:34:15.757978Z","iopub.execute_input":"2022-01-25T05:34:15.758665Z","iopub.status.idle":"2022-01-25T05:34:16.269186Z","shell.execute_reply.started":"2022-01-25T05:34:15.758617Z","shell.execute_reply":"2022-01-25T05:34:16.268079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python /kaggle/working/yolov5/train.py --img 1312 --batch 10 --epochs 3000 --data /kaggle/working/yolov5/my.yaml --cfg /kaggle/working/yolov5/models/yolov5m.yaml --name takıl","metadata":{"execution":{"iopub.status.busy":"2022-01-25T05:34:39.546008Z","iopub.execute_input":"2022-01-25T05:34:39.546883Z"},"trusted":true},"execution_count":null,"outputs":[]}]}