{"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":"markdown","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 matplotlib.pyplot as plt\nimport cv2\nimport json\nfrom PIL import Image\nimport shutil\nfrom tqdm import tqdm\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\nimage_paths = []\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        if filenames == \"train_images\":\n            image_paths.append(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":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-01T19:34:37.311643Z","iopub.execute_input":"2022-01-01T19:34:37.312017Z","iopub.status.idle":"2022-01-01T19:34:42.174069Z","shell.execute_reply.started":"2022-01-01T19:34:37.311914Z","shell.execute_reply":"2022-01-01T19:34:42.173338Z"}}},{"cell_type":"markdown","source":"def drawAnnotation(annotaions, img):\n    annotaions = json.loads(annotaions.replace(\"\\'\", \"\\\"\"))\n    for i in annotaions:\n        width, height = i['width'], i['height']\n        x, y = i['x'], i['y']\n        cv2.rectangle(img, (x,y), (x+width, y+height), (0,255,255), 4)\n    \n    return img","metadata":{"execution":{"iopub.status.busy":"2022-01-01T19:34:46.15189Z","iopub.execute_input":"2022-01-01T19:34:46.152421Z","iopub.status.idle":"2022-01-01T19:34:46.157287Z","shell.execute_reply.started":"2022-01-01T19:34:46.152377Z","shell.execute_reply":"2022-01-01T19:34:46.156632Z"}}},{"cell_type":"markdown","source":"def show_annotations_and_images(k):\n    annotations = k[\"annotations\"]\n    print(annotations)\n    v_n_f = k['image_id']\n    video_number = v_n_f.split(\"-\")[0]\n    videoFrame = v_n_f.split(\"-\")[1]\n    image_path = \"../input/tensorflow-great-barrier-reef/train_images/video_{}/{}.jpg\".format(video_number, videoFrame)\n    img = cv2.imread(image_path)\n    img = drawAnnotation(annotations, img)\n    plt.figure(figsize=(20,10))\n#     img[:,:,0] = np.zeros([img.shape[0], img.shape[1]])+100\n#     plt.imshow(img[...,::-1])\n#     plt.imshow(img[:,:,::-1])\n    plt.imshow(img)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T18:49:46.518301Z","iopub.execute_input":"2022-01-01T18:49:46.518838Z","iopub.status.idle":"2022-01-01T18:49:46.524151Z","shell.execute_reply.started":"2022-01-01T18:49:46.518804Z","shell.execute_reply":"2022-01-01T18:49:46.523414Z"}}},{"cell_type":"markdown","source":"def ret_full_image_path(d):\n    image_path_list = []\n    class_name = []\n    for i, k in d.iterrows():\n        v_n_f = k['image_id']\n        video_number = v_n_f.split(\"-\")[0]\n        videoFrame = v_n_f.split(\"-\")[1]\n        ll = video_number \n        image_path = \"../input/tensorflow-great-barrier-reef/train_images/video_{}/{}.jpg\".format(video_number, videoFrame)\n        image_path_list.append(image_path)\n        class_name.append(0)\n    return image_path_list, class_name","metadata":{"execution":{"iopub.status.busy":"2022-01-01T18:49:46.898023Z","iopub.execute_input":"2022-01-01T18:49:46.898385Z","iopub.status.idle":"2022-01-01T18:49:46.903581Z","shell.execute_reply.started":"2022-01-01T18:49:46.898338Z","shell.execute_reply":"2022-01-01T18:49:46.902901Z"}}},{"cell_type":"markdown","source":"!pwd","metadata":{"execution":{"iopub.status.busy":"2022-01-01T18:49:48.002271Z","iopub.execute_input":"2022-01-01T18:49:48.002831Z","iopub.status.idle":"2022-01-01T18:49:48.780755Z","shell.execute_reply.started":"2022-01-01T18:49:48.002797Z","shell.execute_reply":"2022-01-01T18:49:48.780016Z"}}},{"cell_type":"markdown","source":"data = pd.read_csv(\"../input/tensorflow-great-barrier-reef/train.csv\")\nprint(\"Total images = \",data.shape)\nimage_dir = \"../input/tensorflow-great-barrier-reef/train_images/video_0\"\nimage_names = []\nfor img in os.listdir(image_dir):\n    image_names.append(img)\n    \ndata = data[data['annotations'] != \"[]\"]\nprint(\"Total images with annotations = \",data.shape)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T18:49:50.878438Z","iopub.execute_input":"2022-01-01T18:49:50.87916Z","iopub.status.idle":"2022-01-01T18:49:50.95606Z","shell.execute_reply.started":"2022-01-01T18:49:50.879121Z","shell.execute_reply":"2022-01-01T18:49:50.955325Z"}}},{"cell_type":"markdown","source":"img_path_list, class_name_list = ret_full_image_path(data)\ndata = data.assign(image_path = img_path_list)\ndata = data.assign(Class = class_name_list)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T18:49:53.723937Z","iopub.execute_input":"2022-01-01T18:49:53.724493Z","iopub.status.idle":"2022-01-01T18:49:53.9491Z","shell.execute_reply.started":"2022-01-01T18:49:53.724454Z","shell.execute_reply":"2022-01-01T18:49:53.948387Z"}}},{"cell_type":"markdown","source":"show_annotations_and_images(data.loc[4438])","metadata":{"execution":{"iopub.status.busy":"2022-01-01T18:49:55.23156Z","iopub.execute_input":"2022-01-01T18:49:55.231998Z","iopub.status.idle":"2022-01-01T18:49:56.091897Z","shell.execute_reply.started":"2022-01-01T18:49:55.231963Z","shell.execute_reply":"2022-01-01T18:49:56.091282Z"}}},{"cell_type":"markdown","source":"data","metadata":{"execution":{"iopub.status.busy":"2022-01-01T17:10:49.50994Z","iopub.execute_input":"2022-01-01T17:10:49.510215Z","iopub.status.idle":"2022-01-01T17:10:49.528143Z","shell.execute_reply.started":"2022-01-01T17:10:49.510181Z","shell.execute_reply":"2022-01-01T17:10:49.527475Z"}}},{"cell_type":"markdown","source":"train = data[[\"image_path\",\"image_id\", \"annotations\", \"Class\"]]","metadata":{"execution":{"iopub.status.busy":"2022-01-01T17:10:52.565157Z","iopub.execute_input":"2022-01-01T17:10:52.565727Z","iopub.status.idle":"2022-01-01T17:10:52.571602Z","shell.execute_reply.started":"2022-01-01T17:10:52.565684Z","shell.execute_reply":"2022-01-01T17:10:52.57077Z"}}},{"cell_type":"markdown","source":"train.head(50)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T17:11:52.680177Z","iopub.execute_input":"2022-01-01T17:11:52.680877Z","iopub.status.idle":"2022-01-01T17:11:52.698231Z","shell.execute_reply.started":"2022-01-01T17:11:52.680838Z","shell.execute_reply":"2022-01-01T17:11:52.697439Z"}}},{"cell_type":"markdown","source":"for idx, r in train.iterrows():\n    annotaitons = r[\"annotations\"]\n    annotations = json.loads(annotaitons.replace(\"\\'\", \"\\\"\"))\n    print(r[\"image_path\"])\n    break","metadata":{"execution":{"iopub.status.busy":"2022-01-01T13:29:22.596741Z","iopub.execute_input":"2022-01-01T13:29:22.59753Z","iopub.status.idle":"2022-01-01T13:29:22.604471Z","shell.execute_reply.started":"2022-01-01T13:29:22.597481Z","shell.execute_reply":"2022-01-01T13:29:22.603671Z"}}},{"cell_type":"markdown","source":"p = \"./\"\nfor i in os.listdir(p):\n    os.remove(p+i)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T13:29:22.95488Z","iopub.execute_input":"2022-01-01T13:29:22.955174Z","iopub.status.idle":"2022-01-01T13:29:22.959204Z","shell.execute_reply.started":"2022-01-01T13:29:22.955144Z","shell.execute_reply":"2022-01-01T13:29:22.958555Z"}}},{"cell_type":"markdown","source":"def convert(size, box):\n    dw = 1./size[0]\n    dh = 1./size[1]\n    x = (box[0] + box[1])/2.0\n    y = (box[2] + box[3])/2.0\n    w = box[1] - box[0]\n    h = box[3] - box[2]\n    x = x*dw\n    w = w*dw\n    y = y*dh\n    h = h*dh\n    return (x,y,w,h)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T13:29:24.011036Z","iopub.execute_input":"2022-01-01T13:29:24.01173Z","iopub.status.idle":"2022-01-01T13:29:24.017772Z","shell.execute_reply.started":"2022-01-01T13:29:24.011691Z","shell.execute_reply":"2022-01-01T13:29:24.017113Z"}}},{"cell_type":"markdown","source":"","metadata":{"execution":{"iopub.status.busy":"2022-01-01T13:04:35.125035Z","iopub.execute_input":"2022-01-01T13:04:35.125313Z","iopub.status.idle":"2022-01-01T13:04:35.145232Z","shell.execute_reply.started":"2022-01-01T13:04:35.125284Z","shell.execute_reply":"2022-01-01T13:04:35.144325Z"}}},{"cell_type":"markdown","source":"!mkdir train","metadata":{"execution":{"iopub.status.busy":"2022-01-01T13:29:25.91336Z","iopub.execute_input":"2022-01-01T13:29:25.91409Z","iopub.status.idle":"2022-01-01T13:29:26.568485Z","shell.execute_reply.started":"2022-01-01T13:29:25.91405Z","shell.execute_reply":"2022-01-01T13:29:26.567643Z"}}},{"cell_type":"markdown","source":"tain_folder_list = []\nex = []\nfor idx, r in tqdm(train.iterrows()):\n    image_id = r[\"image_id\"]\n    annotaitons = r[\"annotations\"]\n    image_name = image_id.split(\"-\")[1]\n    vn = image_id.split(\"-\")[0]\n    annotations = json.loads(annotaitons.replace(\"\\'\", \"\\\"\"))\n    image_path = r[\"image_path\"]\n    destination = \"./train/\"\n    shutil.copyfile(image_path, destination+\"{}_{}.jpg\".format(vn, image_name))\n    \n    txt_name = \"{}_{}.txt\".format(vn, image_name)\n    if txt_name in tain_folder_list:\n        ex.append(image_path)\n    \n    with open(\"./train/{}_{}.txt\".format(vn, image_name), \"w\") as f:\n        image_path = r[\"image_path\"]\n        img = Image.open(image_path)\n        ww= int(img.size[0])\n        hh= int(img.size[1])\n        for i in annotations:\n            x_min = i[\"x\"]\n            y_min = i[\"y\"]\n            x_max = i[\"width\"] + x_min\n            y_max = i[\"height\"] + y_min\n            b = (x_min, x_max, y_min, y_max)\n            x, y, w, h = convert((ww,hh), b)\n            writee = \"0 {} {} {} {}\".format(x, y, w, h)\n            f.write(writee + \"\\n\")\n            \n        tain_folder_list.append(\"{}_{}.txt\".format(vn, image_name))\n        \n        ","metadata":{"execution":{"iopub.status.busy":"2022-01-01T13:29:29.202281Z","iopub.execute_input":"2022-01-01T13:29:29.203061Z","iopub.status.idle":"2022-01-01T13:30:32.545479Z","shell.execute_reply.started":"2022-01-01T13:29:29.203011Z","shell.execute_reply":"2022-01-01T13:30:32.544668Z"}}},{"cell_type":"markdown","source":"len(os.listdir(\"./train\"))","metadata":{"execution":{"iopub.status.busy":"2022-01-01T13:30:32.547381Z","iopub.execute_input":"2022-01-01T13:30:32.547843Z","iopub.status.idle":"2022-01-01T13:30:32.562433Z","shell.execute_reply.started":"2022-01-01T13:30:32.547799Z","shell.execute_reply":"2022-01-01T13:30:32.561564Z"}}},{"cell_type":"markdown","source":"shutil.rmtree(\"./fishyolov5\")","metadata":{"execution":{"iopub.status.busy":"2022-01-01T13:10:21.349734Z","iopub.execute_input":"2022-01-01T13:10:21.350506Z","iopub.status.idle":"2022-01-01T13:10:21.366323Z","shell.execute_reply.started":"2022-01-01T13:10:21.350466Z","shell.execute_reply":"2022-01-01T13:10:21.365677Z"}}},{"cell_type":"markdown","source":"%cd ..","metadata":{"execution":{"iopub.status.busy":"2022-01-01T13:10:04.726033Z","iopub.execute_input":"2022-01-01T13:10:04.726417Z","iopub.status.idle":"2022-01-01T13:10:04.739309Z","shell.execute_reply.started":"2022-01-01T13:10:04.726374Z","shell.execute_reply":"2022-01-01T13:10:04.738555Z"}}},{"cell_type":"markdown","source":"!git clone https://github.com/rukon-uddin/fishyolov5.git\n%cd fishyolov5\n%pip install -qr requirements.txt\nimport torch\nfrom fishyolov5 import utils\ndisplay = utils.notebook_init()\n!cp best.pt /kaggle/working/best.pt","metadata":{"execution":{"iopub.status.busy":"2022-01-01T18:50:09.355877Z","iopub.execute_input":"2022-01-01T18:50:09.356402Z","iopub.status.idle":"2022-01-01T18:50:11.958641Z","shell.execute_reply.started":"2022-01-01T18:50:09.356356Z","shell.execute_reply":"2022-01-01T18:50:11.957871Z"}}},{"cell_type":"markdown","source":"%cd fishyolov5\n%pip install -qr requirements.txt","metadata":{"execution":{"iopub.status.busy":"2022-01-01T18:50:19.996374Z","iopub.execute_input":"2022-01-01T18:50:19.996647Z","iopub.status.idle":"2022-01-01T18:50:29.083095Z","shell.execute_reply.started":"2022-01-01T18:50:19.996619Z","shell.execute_reply":"2022-01-01T18:50:29.082262Z"}}},{"cell_type":"markdown","source":"import torch\nfrom fishyolov5 import utils\ndisplay = utils.notebook_init()","metadata":{"execution":{"iopub.status.busy":"2022-01-01T18:50:29.085476Z","iopub.execute_input":"2022-01-01T18:50:29.085741Z","iopub.status.idle":"2022-01-01T18:50:31.095782Z","shell.execute_reply.started":"2022-01-01T18:50:29.085705Z","shell.execute_reply":"2022-01-01T18:50:31.094921Z"}}},{"cell_type":"markdown","source":"!cp best.pt /kaggle/working/best.pt","metadata":{"execution":{"iopub.status.busy":"2022-01-01T18:51:10.510499Z","iopub.execute_input":"2022-01-01T18:51:10.511145Z","iopub.status.idle":"2022-01-01T18:51:11.19165Z","shell.execute_reply.started":"2022-01-01T18:51:10.511102Z","shell.execute_reply":"2022-01-01T18:51:11.190707Z"}}},{"cell_type":"markdown","source":"!tensorboard --logdir runs/train","metadata":{}},{"cell_type":"markdown","source":"!python train.py --img 640 --batch 16 --epochs 30 --data data/fish.yaml --weights yolov5s.pt","metadata":{"execution":{"iopub.status.busy":"2022-01-01T13:31:06.249566Z","iopub.execute_input":"2022-01-01T13:31:06.249859Z","iopub.status.idle":"2022-01-01T16:54:54.280713Z","shell.execute_reply.started":"2022-01-01T13:31:06.249824Z","shell.execute_reply":"2022-01-01T16:54:54.279854Z"}}},{"cell_type":"markdown","source":"%cd ..","metadata":{"execution":{"iopub.status.busy":"2022-01-01T17:00:34.839999Z","iopub.execute_input":"2022-01-01T17:00:34.840452Z","iopub.status.idle":"2022-01-01T17:00:34.846452Z","shell.execute_reply.started":"2022-01-01T17:00:34.840398Z","shell.execute_reply":"2022-01-01T17:00:34.845471Z"}}},{"cell_type":"markdown","source":"!tensorboard --logdir runs/train --load_fast=false --bind_all","metadata":{"execution":{"iopub.status.busy":"2022-01-01T17:01:41.431038Z","iopub.execute_input":"2022-01-01T17:01:41.43134Z","iopub.status.idle":"2022-01-01T17:02:01.405682Z","shell.execute_reply.started":"2022-01-01T17:01:41.431303Z","shell.execute_reply":"2022-01-01T17:02:01.404798Z"}}},{"cell_type":"markdown","source":"!cp /kaggle/input/tensorflow-great-barrier-reef/train_images/video_0/60.jpg /kaggle/working/60.jpg","metadata":{"execution":{"iopub.status.busy":"2022-01-01T17:13:02.010632Z","iopub.execute_input":"2022-01-01T17:13:02.010923Z","iopub.status.idle":"2022-01-01T17:13:02.722117Z","shell.execute_reply.started":"2022-01-01T17:13:02.010886Z","shell.execute_reply":"2022-01-01T17:13:02.721128Z"}}},{"cell_type":"markdown","source":"!cp best.pt /kaggle/working/best.pt","metadata":{"execution":{"iopub.status.busy":"2022-01-01T17:13:02.724168Z","iopub.execute_input":"2022-01-01T17:13:02.724484Z","iopub.status.idle":"2022-01-01T17:13:03.443732Z","shell.execute_reply.started":"2022-01-01T17:13:02.724441Z","shell.execute_reply":"2022-01-01T17:13:03.442867Z"}}},{"cell_type":"markdown","source":"%cd weights\n%ls","metadata":{"execution":{"iopub.status.busy":"2022-01-01T17:04:05.338652Z","iopub.execute_input":"2022-01-01T17:04:05.338933Z","iopub.status.idle":"2022-01-01T17:04:06.008277Z","shell.execute_reply.started":"2022-01-01T17:04:05.338898Z","shell.execute_reply":"2022-01-01T17:04:06.007249Z"}}},{"cell_type":"markdown","source":"%cd /kaggle/working/fishyolov5","metadata":{"execution":{"iopub.status.busy":"2022-01-01T17:14:04.563496Z","iopub.execute_input":"2022-01-01T17:14:04.563759Z","iopub.status.idle":"2022-01-01T17:14:04.572841Z","shell.execute_reply.started":"2022-01-01T17:14:04.563731Z","shell.execute_reply":"2022-01-01T17:14:04.572113Z"}}},{"cell_type":"markdown","source":"!python detect.py --weights /kaggle/working/best.pt --img 640 --conf 0.1 --source /kaggle/input/tensorflow-great-barrier-reef/train_images/video_0/64.jpg\ndisplay.Image(filename='/kaggle/input/tensorflow-great-barrier-reef/train_images/video_0/64.jpg', width=600)","metadata":{"execution":{"iopub.status.busy":"2022-01-01T17:18:21.413782Z","iopub.execute_input":"2022-01-01T17:18:21.414059Z","iopub.status.idle":"2022-01-01T17:18:28.940625Z","shell.execute_reply.started":"2022-01-01T17:18:21.414025Z","shell.execute_reply":"2022-01-01T17:18:28.939908Z"}}},{"cell_type":"markdown","source":"from tqdm import tqdm\nimport os\nimport time\n\nstart = time.time() \nimage_files = []\nfor filename in tqdm(os.listdir(r\"/kaggle/working/train\")):   # prive the appropriate path\n    if filename.endswith(\".jpg\") or filename.endswith(\".JPG\") or filename.endswith(\".png\") or filename.endswith(\".PNG\"):\n        image_files.append(\"/kaggle/working/train/\" + filename) # prive the appropriate path (Note: dont forget to put he backslash '/' at the end)\nos.chdir(\"./\")\nwith open(\"train.txt\", \"w\") as outfile:\n    for image in tqdm(image_files):\n        outfile.write(image)\n        outfile.write(\"\\n\")\n    outfile.close()\nos.chdir(\"..\")\n\nprint(f'\\nTime: {time.time() - start}')","metadata":{"execution":{"iopub.status.busy":"2022-01-01T12:49:45.068495Z","iopub.execute_input":"2022-01-01T12:49:45.069164Z","iopub.status.idle":"2022-01-01T12:49:45.108485Z","shell.execute_reply.started":"2022-01-01T12:49:45.069123Z","shell.execute_reply":"2022-01-01T12:49:45.107816Z"}}},{"cell_type":"markdown","source":"import torch\n\n# Model\nmodel = torch.hub.load('ultralytics/yolov5', 'custom', path='/kaggle/working/best.pt')  # or yolov5m, yolov5l, yolov5x, custom\n\n# Images\n\n# Results","metadata":{"execution":{"iopub.status.busy":"2022-01-01T18:08:36.532766Z","iopub.execute_input":"2022-01-01T18:08:36.53306Z","iopub.status.idle":"2022-01-01T18:08:36.893957Z","shell.execute_reply.started":"2022-01-01T18:08:36.533022Z","shell.execute_reply":"2022-01-01T18:08:36.893104Z"}}},{"cell_type":"markdown","source":"img = '/kaggle/input/tensorflow-great-barrier-reef/train_images/video_0/64.jpg'\nresults = model(img)\nprint('\\n', results.xyxy[0])","metadata":{"execution":{"iopub.status.busy":"2022-01-01T18:13:31.774678Z","iopub.execute_input":"2022-01-01T18:13:31.774934Z","iopub.status.idle":"2022-01-01T18:13:31.818626Z","shell.execute_reply.started":"2022-01-01T18:13:31.774904Z","shell.execute_reply":"2022-01-01T18:13:31.817537Z"}}},{"cell_type":"markdown","source":"img = plt.imread(\"/kaggle/input/tensorflow-great-barrier-reef/train_images/video_0/64.jpg\")\nfor i in results.xyxy[0].tolist():\n    print(map(int, i))\n    x1, y1, x2, y2, c, clas = map(int, i)   \n    print(x1)\n    \n    cv2.rectangle(img, (x1,y1), (x2, y2), (0,255,255), 4)\n\nplt.figure(figsize=(20,10))\nplt.imshow(img)\n    ","metadata":{"execution":{"iopub.status.busy":"2022-01-01T18:15:34.416613Z","iopub.execute_input":"2022-01-01T18:15:34.417388Z","iopub.status.idle":"2022-01-01T18:15:35.13066Z","shell.execute_reply.started":"2022-01-01T18:15:34.41735Z","shell.execute_reply":"2022-01-01T18:15:35.129749Z"}}},{"cell_type":"markdown","source":"x1, y1, x2, y2, c, clas = map(int, results.xyxy[0].tolist()[0])","metadata":{"execution":{"iopub.status.busy":"2022-01-01T18:06:08.803389Z","iopub.execute_input":"2022-01-01T18:06:08.803632Z","iopub.status.idle":"2022-01-01T18:06:08.808492Z","shell.execute_reply.started":"2022-01-01T18:06:08.803606Z","shell.execute_reply":"2022-01-01T18:06:08.807805Z"}}},{"cell_type":"code","source":"# if x1 > x2:\n#     t=x1\n#     x1=x2\n#     x2=t\n    \n# if y1 > y2:\n#     t=y1\n#     y1=y2\n#     y2=t\n","metadata":{"execution":{"iopub.status.busy":"2022-01-01T18:06:14.209729Z","iopub.execute_input":"2022-01-01T18:06:14.209994Z","iopub.status.idle":"2022-01-01T18:06:14.214851Z","shell.execute_reply.started":"2022-01-01T18:06:14.209964Z","shell.execute_reply":"2022-01-01T18:06:14.21405Z"},"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)\nimport matplotlib.pyplot as plt\nimport cv2\nimport json\nfrom PIL import Image\nimport shutil\nfrom tqdm import tqdm\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","metadata":{"execution":{"iopub.status.busy":"2022-01-02T08:06:43.974985Z","iopub.execute_input":"2022-01-02T08:06:43.975979Z","iopub.status.idle":"2022-01-02T08:06:44.157534Z","shell.execute_reply.started":"2022-01-02T08:06:43.975859Z","shell.execute_reply":"2022-01-02T08:06:44.156786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"!git clone https://github.com/rukon-uddin/fishyolov5.git","metadata":{"execution":{"iopub.status.busy":"2022-01-02T05:51:52.507485Z","iopub.execute_input":"2022-01-02T05:51:52.508126Z","iopub.status.idle":"2022-01-02T05:51:55.466219Z","shell.execute_reply.started":"2022-01-02T05:51:52.50809Z","shell.execute_reply":"2022-01-02T05:51:55.465227Z"}}},{"cell_type":"markdown","source":"%cd fishyolov5","metadata":{}},{"cell_type":"markdown","source":"%cd fishyolov5\n%pip install -qr requirements.txt\nimport torch\nfrom fishyolov5 import utils\ndisplay = utils.notebook_init()","metadata":{"execution":{"iopub.status.busy":"2022-01-02T05:52:00.413967Z","iopub.execute_input":"2022-01-02T05:52:00.414821Z","iopub.status.idle":"2022-01-02T05:52:11.382863Z","shell.execute_reply.started":"2022-01-02T05:52:00.414778Z","shell.execute_reply":"2022-01-02T05:52:11.382114Z"}}},{"cell_type":"markdown","source":"!cp best.pt /kaggle/working/best.pt","metadata":{"execution":{"iopub.status.busy":"2022-01-02T05:52:13.730709Z","iopub.execute_input":"2022-01-02T05:52:13.731016Z","iopub.status.idle":"2022-01-02T05:52:14.411664Z","shell.execute_reply.started":"2022-01-02T05:52:13.730981Z","shell.execute_reply":"2022-01-02T05:52:14.410695Z"}}},{"cell_type":"code","source":"!mkdir -p /root/.config/Ultralytics\n!cp /kaggle/input/yolov5-font/Arial.ttf /root/.config/Ultralytics/","metadata":{"execution":{"iopub.status.busy":"2022-01-02T08:06:51.404326Z","iopub.execute_input":"2022-01-02T08:06:51.404573Z","iopub.status.idle":"2022-01-02T08:06:52.765142Z","shell.execute_reply.started":"2022-01-02T08:06:51.404546Z","shell.execute_reply":"2022-01-02T08:06:52.764200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\n\n# Model\nmodel = torch.hub.load('/kaggle/input/yolov5-lib-ds', 'custom', path='/kaggle/input/weights/best.pt', source='local', force_reload=True)  # or yolov5m, yolov5l, yolov5x, custom\nmodel.conf = 0.1","metadata":{"execution":{"iopub.status.busy":"2022-01-02T08:06:54.973739Z","iopub.execute_input":"2022-01-02T08:06:54.974032Z","iopub.status.idle":"2022-01-02T08:07:01.828646Z","shell.execute_reply.started":"2022-01-02T08:06:54.973984Z","shell.execute_reply":"2022-01-02T08:07:01.827730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import greatbarrierreef\n\nenv = greatbarrierreef.make_env()   # initialize the environment\niter_test = env.iter_test()","metadata":{"execution":{"iopub.status.busy":"2022-01-02T08:07:06.478289Z","iopub.execute_input":"2022-01-02T08:07:06.479133Z","iopub.status.idle":"2022-01-02T08:07:06.502336Z","shell.execute_reply.started":"2022-01-02T08:07:06.479081Z","shell.execute_reply":"2022-01-02T08:07:06.501540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idc = 0\nsubmission_dict = {\n    'index': [],\n    'annotations': [],\n}\n\n# env.predict(sample_prediction_df)\nfor (image, sample_prediction_df) in iter_test:\n    results = model(image)\n    predictions = []\n#     print('\\n', results.xyxy[0])\n    submission_dict['index'].append(idc)\n    for i in results.xyxy[0].tolist():\n        x1, y1, x2, y2, c, clas = map(int, i)\n        print(x1, y1, x2, y2)\n        predictions.append('{:.2f} {} {} {} {}'.format(c, x1, y1, x2, y2))\n    \n    prediction_str = ' '.join(predictions)\n    if len(prediction_str)==0:\n        prediction_str = \"NaN\"\n    submission_dict['annotations'].append(prediction_str)\n    sample_prediction_df['annotations'] = prediction_str\n    env.predict(sample_prediction_df)\n    idc+=1\n    \n    \ndf= pd.DataFrame(submission_dict)\nprint(\"Done!!\")","metadata":{"execution":{"iopub.status.busy":"2022-01-02T08:07:08.278636Z","iopub.execute_input":"2022-01-02T08:07:08.278895Z","iopub.status.idle":"2022-01-02T08:07:13.383062Z","shell.execute_reply.started":"2022-01-02T08:07:08.278866Z","shell.execute_reply":"2022-01-02T08:07:13.381589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-01-02T08:07:20.836514Z","iopub.execute_input":"2022-01-02T08:07:20.837372Z","iopub.status.idle":"2022-01-02T08:07:20.853910Z","shell.execute_reply.started":"2022-01-02T08:07:20.837322Z","shell.execute_reply":"2022-01-02T08:07:20.853225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"%cd ..","metadata":{"execution":{"iopub.status.busy":"2022-01-02T05:52:48.813643Z","iopub.execute_input":"2022-01-02T05:52:48.814332Z","iopub.status.idle":"2022-01-02T05:52:48.820005Z","shell.execute_reply.started":"2022-01-02T05:52:48.814287Z","shell.execute_reply":"2022-01-02T05:52:48.819212Z"}}},{"cell_type":"code","source":"df.to_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-01-02T08:07:24.337854Z","iopub.execute_input":"2022-01-02T08:07:24.338548Z","iopub.status.idle":"2022-01-02T08:07:24.344134Z","shell.execute_reply.started":"2022-01-02T08:07:24.338508Z","shell.execute_reply":"2022-01-02T08:07:24.343224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}