{"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/working/datasets/coco/images/'):\n    for filename in filenames:\n        #print(str(dirname[-1])+\"-\"+str(filename)[:-4])\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-04T09:54:17.952123Z","iopub.execute_input":"2022-05-04T09:54:17.952566Z","iopub.status.idle":"2022-05-04T09:54:17.986105Z","shell.execute_reply.started":"2022-05-04T09:54:17.952487Z","shell.execute_reply":"2022-05-04T09:54:17.985202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2022-05-04T09:54:17.98791Z","iopub.execute_input":"2022-05-04T09:54:17.988309Z","iopub.status.idle":"2022-05-04T09:54:23.034464Z","shell.execute_reply.started":"2022-05-04T09:54:17.988268Z","shell.execute_reply":"2022-05-04T09:54:23.033582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov5","metadata":{"execution":{"iopub.status.busy":"2022-05-04T09:54:23.036094Z","iopub.execute_input":"2022-05-04T09:54:23.037598Z","iopub.status.idle":"2022-05-04T09:54:26.268099Z","shell.execute_reply.started":"2022-05-04T09:54:23.037556Z","shell.execute_reply":"2022-05-04T09:54:26.266915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir /kaggle/working/datasets/\n!mkdir /kaggle/working/datasets/coco\n!mkdir /kaggle/working/datasets/coco/images\n!mkdir /kaggle/working/datasets/coco/labels\n\nimport pandas as pd\n\nannotations_csv=pd.read_csv(\"../input/tensorflow-great-barrier-reef/train.csv\")\nfinal_anno_csv=pd.DataFrame({\"image_id\":[],\"x\":[],\"y\":[],\"width\":[],\"height\":[]})\nfor i in range(len(annotations_csv.index)):\n    s=list(eval(annotations_csv[\"annotations\"][i]))\n    f=open(f'/kaggle/working/datasets/coco/labels/{annotations_csv[\"image_id\"][i]}.txt',\"a\")\n    for k in s:\n        final_anno_csv.loc[len(final_anno_csv.index)]=[annotations_csv[\"image_id\"][i],k[\"x\"]/1280.0,k[\"y\"]/720.0,k[\"width\"]/1280.0,k[\"height\"]/720.0]\n        f.write(f'{0} {k[\"x\"]/1280.0+k[\"width\"]/2560.0} {k[\"y\"]/720.0+k[\"height\"]/1440.0} {k[\"width\"]/1280.0} {k[\"height\"]/720.0}'+\"\\n\")\n\nfinal_anno_csv[\"class\"]=[\"star\"]*len(final_anno_csv.index)\nfinal_anno_csv[\"x_centre\"]=final_anno_csv[\"x\"]+final_anno_csv[\"width\"]/2.0\nfinal_anno_csv[\"y_centre\"]=final_anno_csv[\"y\"]+final_anno_csv[\"height\"]/2.0\nimage_path=[]\nfor i in range(len(final_anno_csv.index)):\n    image_path.append(f\"/kaggle/input/tensorflow-great-barrier-reef/train_images/video_{final_anno_csv['image_id'][i][0]}/{final_anno_csv['image_id'][i][2:]}.jpg\")\n\nfinal_anno_csv[\"image_path\"]=image_path\n\n\n\nwith open(\"/kaggle/working/yolov5/data/coco.yaml\",\"r\") as jkl:\n    print(jkl.read())\n\n\n\ndata={\"path\":\"/kaggle/working/datasets/coco\",\"train\":\"images\",\"val\":\"images\",\"test\":\"images\",\"nc\":1,\"names\":[\"star\"]}\n\nimport yaml\n\nwith open(\"/kaggle/working/yolov5/data/coco.yaml\",\"w\") as f:\n    yaml.dump(data,f)\n    \nwith open(\"/kaggle/working/yolov5/data/coco.yaml\",\"r\") as jkl:\n    print(jkl.read())","metadata":{"execution":{"iopub.status.busy":"2022-05-04T09:54:26.271583Z","iopub.execute_input":"2022-05-04T09:54:26.271922Z","iopub.status.idle":"2022-05-04T09:55:11.959858Z","shell.execute_reply.started":"2022-05-04T09:54:26.271876Z","shell.execute_reply":"2022-05-04T09:55:11.958782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(\"./yolov5/data/hyps/hyp.scratch-low.yaml\") as f:\n    lista=yaml.safe_load(f)\n    lista[\"lr0\"]=0.001\nprint(lista)\nwith open(\"./yolov5/data/hyps/hyp.scratch-low.yaml\",\"w\") as g:\n    yaml.dump(lista,g)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open(\"./yolov5/data/hyps/hyp.scratch-low.yaml\",\"r\") as f:\n    print(f.read())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a=[]\nfor i in list(final_anno_csv[\"image_path\"]):\n    if i not in a:\n        a.append(i)\n\nimport cv2\n\nfor i in a:\n    image=cv2.imread(i,-1)\n    image=cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n    image = cv2.resize(image, (640,640),interpolation=cv2.INTER_NEAREST)\n    image=cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\n    cv2.imwrite(\"/kaggle/working/datasets/coco/images/\"+str(i[63])+\"-\"+str(i[65:-4])+\".jpg\",image)","metadata":{"execution":{"iopub.status.busy":"2022-05-04T09:55:11.961753Z","iopub.execute_input":"2022-05-04T09:55:11.96232Z","iopub.status.idle":"2022-05-04T09:59:19.684005Z","shell.execute_reply.started":"2022-05-04T09:55:11.962269Z","shell.execute_reply":"2022-05-04T09:59:19.682846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cd yolov5\n!pip install -qr /kaggle/working/yolov5/requirements.txt # install dependencies\n!pip install -q roboflow\n\nimport torch\nimport os\nfrom IPython.display import Image, clear_output  # to display images","metadata":{"execution":{"iopub.status.busy":"2022-05-04T09:59:19.685818Z","iopub.execute_input":"2022-05-04T09:59:19.686137Z","iopub.status.idle":"2022-05-04T10:00:06.207866Z","shell.execute_reply.started":"2022-05-04T09:59:19.686095Z","shell.execute_reply":"2022-05-04T10:00:06.2067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!python /kaggle/working/yolov5/train.py --img 640 --batch 64 --epochs 50 --data coco.yaml --weights yolov5s.pt","metadata":{"execution":{"iopub.status.busy":"2022-05-04T10:00:06.210322Z","iopub.execute_input":"2022-05-04T10:00:06.211059Z","iopub.status.idle":"2022-05-04T10:00:06.21684Z","shell.execute_reply.started":"2022-05-04T10:00:06.211015Z","shell.execute_reply":"2022-05-04T10:00:06.215301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python /kaggle/working/yolov5/train.py --img 640 --batch 64 --epochs 50 --data coco.yaml --weights ../input/checkpoints/best.pt","metadata":{"execution":{"iopub.status.busy":"2022-05-04T12:13:48.621205Z","iopub.execute_input":"2022-05-04T12:13:48.621545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python /kaggle/working/yolov5/detect.py --weights /kaggle/working/yolov5/runs/train/exp/weights/best.pt --img 640 --conf 0.04 --source /kaggle/input/tensorflow-great-barrier-reef/train_images/video_0/38.jpg","metadata":{"execution":{"iopub.status.busy":"2022-05-04T12:02:03.751866Z","iopub.execute_input":"2022-05-04T12:02:03.753891Z","iopub.status.idle":"2022-05-04T12:02:03.761757Z","shell.execute_reply.started":"2022-05-04T12:02:03.753831Z","shell.execute_reply":"2022-05-04T12:02:03.760304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp /kaggle/working/yolov5/runs/train/exp/weights/best.pt /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2022-05-04T12:02:03.767305Z","iopub.execute_input":"2022-05-04T12:02:03.770755Z","iopub.status.idle":"2022-05-04T12:02:04.690272Z","shell.execute_reply.started":"2022-05-04T12:02:03.770704Z","shell.execute_reply":"2022-05-04T12:02:04.688954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!python /kaggle/working/yolov5/detect.py --weights ../input/checkpoints/best.pt --img 640 --conf 0.02 --source /kaggle/input/tensorflow-great-barrier-reef/train_images/video_0/38.jpg","metadata":{"execution":{"iopub.status.busy":"2022-05-04T12:02:04.692708Z","iopub.execute_input":"2022-05-04T12:02:04.693101Z","iopub.status.idle":"2022-05-04T12:02:04.700976Z","shell.execute_reply.started":"2022-05-04T12:02:04.693029Z","shell.execute_reply":"2022-05-04T12:02:04.698875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!cp /kaggle/working/yolov5/runs/detect/exp/38.jpg /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2022-05-04T12:02:04.702872Z","iopub.execute_input":"2022-05-04T12:02:04.703637Z","iopub.status.idle":"2022-05-04T12:02:04.710086Z","shell.execute_reply.started":"2022-05-04T12:02:04.703562Z","shell.execute_reply":"2022-05-04T12:02:04.709012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''with open('/kaggle/working/imgpath.txt', 'w') as the_file:\n    the_file.write('')\n    the_file=open('/kaggle/working/imgpath.txt', 'r')\nprint(the_file.read())\nthe_file=open('/kaggle/working/imgvalpath.txt', 'r')\nthe_file.read()\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/working/datasets/coco/images'):\n    for filename in filenames:\n        os.remove(os.path.join(dirname, filename))\nthe_file=open('/kaggle/working/imgpath.txt', 'a')\nfor i in final_anno_csv[\"image_path\"]:\n    the_file.write(str(i)+\"\\n\")\n\nthe_file=open('/kaggle/working/imgvalpath.txt', 'a')\nfor i in final_anno_csv[\"image_path\"][:100]:\n    the_file.write(str(i)+\"\\n\")\n\"path: /kaggle/working/datasets/coco\",f)\n    yaml.dump(\"train: /kaggle/working/datasets/coco/images\",f)\n    yaml.dump(\"val: /kaggle/working/datasets/coco/images\",f)\n    yaml.dump(\"test: \",f)\n    yaml.dump(\"nc: 1\",f)\n    yaml.dump(\"names: ['star']\",f)\nimport yaml\n\nwith open(\"/kaggle/working/yolov5/data/yolomp.yaml\",\"r\") as f:\n     list_doc = yaml.safe_load(f)\n\nprint(list_doc[\"names\"])\nwith open(\"/kaggle/working/yolov5/data/yolomp.yaml\",\"r\") as f:\n    print(f.read())\nwith open(\"/kaggle/working/yolov5/data/coco.yaml\") as f:\n     list_doc = yaml.safe_load(f)\nprint(list_doc[\"path\"])\nimport shutil, os\nfiles = final_anno_csv[\"image_path\"]\nfor i in range(len(files)):\n    shutil.copy(files[i], f'/kaggle/working/datasets/coco/images/{final_anno_csv[\"image_id\"][i]}.jpg')\n\nimport PIL\nfrom PIL import Image\nfor dirname, _, filenames in os.walk('/kaggle/input/tensorflow-great-barrier-reef/train_images'):\n    for filename in filenames:\n        if str(dirname[-1])+\"-\"+str(filename)[:-4] in list(annotations_csv[\"image_id\"]):\n            image=Image.open(os.path.join(dirname, filename))\n            image=tf.image.resize(np.array(image),(640,640))\n            image=Image.fromarray(np.array(image))\n            image.save(\"/kaggle/working/\"+str(dirname[-1])+\"_\"+str(filename),image)\ni=a[0]\nimage=cv2.imread(i,-1)\nimage=cv2.cvtColor(image, cv2.COLOR_RGB2BGR)\nimage = cv2.resize(image, (640,640),interpolation=cv2.INTER_NEAREST)\ncv2.imwrite(\"/kaggle/working/exp1.jpg\",image)\n'''","metadata":{"execution":{"iopub.status.busy":"2022-05-04T12:02:04.713168Z","iopub.execute_input":"2022-05-04T12:02:04.713605Z","iopub.status.idle":"2022-05-04T12:02:04.730976Z","shell.execute_reply.started":"2022-05-04T12:02:04.713559Z","shell.execute_reply":"2022-05-04T12:02:04.729882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n    \nmodel = torch.hub.load('ultralytics/yolov5', 'custom', '../input/checkpoints/best.pt') \npredictions = model(\"../input/tensorflow-great-barrier-reef/train_images/video_0/38.jpg\")\npredictions.print()\npredictions.xyxy[0]\n#labels, cord_thres = results.xyxyn[0][:, -1].numpy(), results.xyxyn[0][:, :-1].numpy()","metadata":{"execution":{"iopub.status.busy":"2022-05-04T12:02:04.733258Z","iopub.execute_input":"2022-05-04T12:02:04.734175Z","iopub.status.idle":"2022-05-04T12:02:40.097838Z","shell.execute_reply.started":"2022-05-04T12:02:04.73412Z","shell.execute_reply":"2022-05-04T12:02:40.096611Z"},"trusted":true},"execution_count":null,"outputs":[]}]}