{"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":"import numpy as np # linear algebra\nnp.random.seed(32)\n\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport seaborn as sns\nimport cv2\nimport matplotlib.pyplot as plt\nimport random\n\nfrom tqdm.notebook import tqdm\ntqdm.pandas()\nimport torch\nfrom PIL import Image\nimport ast\nimport albumentations as albu\nimport glob\nimport shutil\nfrom decimal import getcontext\nBASE_DIR = '/kaggle/input/tensorflow-great-barrier-reef'","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-08T06:51:34.754238Z","iopub.execute_input":"2023-10-08T06:51:34.754648Z","iopub.status.idle":"2023-10-08T06:51:34.762179Z","shell.execute_reply.started":"2023-10-08T06:51:34.754618Z","shell.execute_reply":"2023-10-08T06:51:34.760982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.set_printoptions(precision = 4, suppress = True)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T06:51:38.127547Z","iopub.execute_input":"2023-10-08T06:51:38.127939Z","iopub.status.idle":"2023-10-08T06:51:38.134070Z","shell.execute_reply.started":"2023-10-08T06:51:38.127909Z","shell.execute_reply":"2023-10-08T06:51:38.132577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analysing Sequences","metadata":{}},{"cell_type":"code","source":"df_tr = pd.read_csv(os.path.join(BASE_DIR ,'train.csv' ))\n\ndef get_full_path(x):\n    vid_id , vid_frame = x.split('-')\n    path = os.path.join(BASE_DIR ,'train_images', 'video_{id}'.format(id = vid_id) ,\n                        '{frame}.jpg'.format(frame = vid_frame))\n    return path\n\ndf_tr['path'] = df_tr.image_id.apply(lambda x: get_full_path(x))\nfig = plt.figure(figsize = (15,5))\nsns.histplot(data = df_tr , x = df_tr.sequence , hue = df_tr.video_id, multiple=\"dodge\")","metadata":{"execution":{"iopub.status.busy":"2023-10-08T06:51:43.622985Z","iopub.execute_input":"2023-10-08T06:51:43.623461Z","iopub.status.idle":"2023-10-08T06:51:44.299901Z","shell.execute_reply.started":"2023-10-08T06:51:43.623401Z","shell.execute_reply":"2023-10-08T06:51:44.298380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analysing BBox","metadata":{}},{"cell_type":"code","source":"def count_bbox(x):\n    if len(x)<=2:\n        return 0\n    return len(x[1:-1].split('}'))-1    # '[{},{},{}]'\n\ndf_tr['bbox_ct'] = df_tr['annotations'].apply(lambda x: count_bbox(x))\nfig, axs = plt.subplots(nrows = 3 ,ncols = 1 , figsize=(10,10))\n\nfor i in range(3):\n    axs[i].title.set_text('Video-{i}'.format(i =i))\n    df = df_tr[df_tr.video_id == i].bbox_ct\n    axs[i].hist(df , bins = 50)\n\n    ","metadata":{"execution":{"iopub.status.busy":"2023-10-08T06:52:03.914108Z","iopub.execute_input":"2023-10-08T06:52:03.914617Z","iopub.status.idle":"2023-10-08T06:52:04.807734Z","shell.execute_reply.started":"2023-10-08T06:52:03.914577Z","shell.execute_reply":"2023-10-08T06:52:04.806547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (15,8))\nsns.histplot(df_tr , x = df_tr.bbox_ct)\nx = len(df_tr[df_tr.bbox_ct==0])/len(df_tr)\nprint('Percentage frames with no bbox :{:.2f}%'.format(100*x) )","metadata":{"execution":{"iopub.status.busy":"2023-10-08T06:52:10.251695Z","iopub.execute_input":"2023-10-08T06:52:10.252126Z","iopub.status.idle":"2023-10-08T06:52:10.608508Z","shell.execute_reply.started":"2023-10-08T06:52:10.252095Z","shell.execute_reply":"2023-10-08T06:52:10.607287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Nearly 80% data has no starfish in it**\n\nBut we can't neglect 80% data and do training on just remaining 20% as the model needs to be robust in both ways -->\n\n**a) predicting right bbox**\n\n**b) not predicting bbox when not needed**\n\nThere are also pictures with near about 70 bboxes ,but they are very rare ","metadata":{}},{"cell_type":"code","source":"def no_bbox(x):\n    if x==0:\n        return 1\n    return 0\n\ndf_tr['no_bbox'] = df_tr.bbox_ct.apply(lambda x :no_bbox(x))\ndf_tr.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-08T06:52:14.769175Z","iopub.execute_input":"2023-10-08T06:52:14.769638Z","iopub.status.idle":"2023-10-08T06:52:14.796922Z","shell.execute_reply.started":"2023-10-08T06:52:14.769603Z","shell.execute_reply":"2023-10-08T06:52:14.795473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tr_v2 = df_tr.groupby(['video_id' , 'sequence']).agg({\n    'sequence_frame' :'count' , \n    'no_bbox' : 'sum'\n}).rename(columns = {'sequence_frame': 'total_frames' ,\n                    'no_bbox' : 'frames_without_annot'})\ndf_tr_v2","metadata":{"execution":{"iopub.status.busy":"2023-10-08T06:52:24.858451Z","iopub.execute_input":"2023-10-08T06:52:24.858930Z","iopub.status.idle":"2023-10-08T06:52:24.888369Z","shell.execute_reply.started":"2023-10-08T06:52:24.858887Z","shell.execute_reply":"2023-10-08T06:52:24.887302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tr_without_annot = df_tr[df_tr.bbox_ct==0].reset_index(drop = True)\ndf_tr_with_annot = df_tr[df_tr.bbox_ct>0]\n\nseq_set = set(df_tr.sequence)\n\ndf = pd.DataFrame()\n\n# I am deciding to choose 8% of total frames with no bbox from each sequence randomly\nfactor = 0.08\nfor seq in seq_set:\n    idxs = df_tr_without_annot[df_tr_without_annot.sequence==seq].index\n    if len(idxs)==0:\n        continue\n    \n    ct = (int)(len(idxs)*factor)+1\n    arr = np.random.choice(idxs , ct)\n    \n    df_ = df_tr_without_annot.iloc[arr]\n    df = pd.concat([df, df_], ignore_index=True)\n    \nsns.histplot(df , x =  df.sequence, bins = 80)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T06:52:51.465591Z","iopub.execute_input":"2023-10-08T06:52:51.465991Z","iopub.status.idle":"2023-10-08T06:52:51.882982Z","shell.execute_reply.started":"2023-10-08T06:52:51.465964Z","shell.execute_reply":"2023-10-08T06:52:51.881546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tr_v3 = pd.concat([df_tr_with_annot , df] , ignore_index = True).reset_index(drop = True)\n\ndf_tr_v3.drop(['video_id','video_frame'  ,'no_bbox' ], axis=1 ,inplace =True)\ndf_tr_v3.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-08T06:54:32.098793Z","iopub.execute_input":"2023-10-08T06:54:32.099235Z","iopub.status.idle":"2023-10-08T06:54:32.119049Z","shell.execute_reply.started":"2023-10-08T06:54:32.099203Z","shell.execute_reply":"2023-10-08T06:54:32.117434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (20,5))\nsns.histplot(df_tr_v3 , x = df_tr_v3.bbox_ct)\nx = 100*(len(df)/len(df_tr_v3))\nprint('{:.2f}% without annotations'.format(x))","metadata":{"execution":{"iopub.status.busy":"2023-10-08T06:54:37.510756Z","iopub.execute_input":"2023-10-08T06:54:37.512253Z","iopub.status.idle":"2023-10-08T06:54:38.086953Z","shell.execute_reply.started":"2023-10-08T06:54:37.512076Z","shell.execute_reply":"2023-10-08T06:54:38.086092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tr_v3.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-08T06:54:50.429267Z","iopub.execute_input":"2023-10-08T06:54:50.429624Z","iopub.status.idle":"2023-10-08T06:54:50.442093Z","shell.execute_reply.started":"2023-10-08T06:54:50.429596Z","shell.execute_reply":"2023-10-08T06:54:50.440910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Analysing BBOX","metadata":{}},{"cell_type":"code","source":"getcontext().prec = 3\n","metadata":{"execution":{"iopub.status.busy":"2023-10-08T06:55:10.825549Z","iopub.execute_input":"2023-10-08T06:55:10.825995Z","iopub.status.idle":"2023-10-08T06:55:10.832028Z","shell.execute_reply.started":"2023-10-08T06:55:10.825964Z","shell.execute_reply":"2023-10-08T06:55:10.830636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import json\nfrom PIL import Image\nimport torch\nimport torchvision\nfrom torchvision.io import read_image\nfrom torchvision.utils import draw_bounding_boxes\n\ndef str2annots(annots):\n    return json.loads(annots.replace(\"'\", '\"'))\n    \ndef annot2str(annots):\n    return str(annots)\n\ndef get_bbox(annots):\n    if type(annots)==str:\n        annots = str2annots(annots)\n    bboxes = [list(annot.values()) for annot in annots]\n    return bboxes\n\ndef get_imgsize(img_path):\n    img = Image.open(img_path)\n    w,h = img.size\n    return [w,h]\n\n\n\n# COCO       :  [x_min, y_min, width, height]\n# YOLO       :  [x_center, y_center, width, height] \n# Pascal_VOC :  [x_min, y_min, x_max, y_max]     # torch format\n\ndef coco2voc(x1, y1, w, h):\n    return [x1,y1, x1 + w, y1 + h]\n\ndef coco2yolo(x1, y1, w, h, image_w, image_h):\n    return [((2*x1 + w)/(2*image_w)) , ((2*y1 + h)/(2*image_h)), w/image_w, h/image_h]\n\ndef voc2coco(x1, y1, x2, y2):\n    return [x1,y1, x2 - x1, y2 - y1]\n\ndef voc2yolo(x1, y1, x2, y2, image_w, image_h):\n    return [((x2 + x1)/(2*image_w)), ((y2 + y1)/(2*image_h)), (x2 - x1)/image_w, (y2 - y1)/image_h]\n\ndef yolo2coco(x_center, y_center, w, h,  image_w, image_h):\n    w = w * image_w\n    h = h * image_h\n    x1 = ((2 * x_center * image_w) - w)/2\n    y1 = ((2 * y_center * image_h) - h)/2\n    return [x1, y1, w, h]\n\ndef yolo2voc(x_center, y_center, w, h,  image_w, image_h):\n    w = w * image_w\n    h = h * image_h\n    x1 = ((2 * x_center * image_w) - w)/2\n    y1 = ((2 * y_center * image_h) - h)/2\n    x2 = x1 + w\n    y2 = y1 + h\n    return [x1, y1, x2, y2]\n\ndef plot_bbox_image(path , labels, bboxes , colors):\n    # bounding box in (xmin, ymin, xmax, ymax) format\n    ct = len(bboxes)\n    bboxes = torch.tensor(bboxes, dtype=torch.int)\n#     img = path\n    if type(path)==str:\n        img = read_image(path)\n    else:\n        img = Image.fromarray(path, 'RGB')\n    if ct>0:\n        # draw bounding boxes on the input image\n        img=draw_bounding_boxes(image=img , boxes=bboxes, labels = labels ,width=3,\n        colors=colors)\n    img = torchvision.transforms.ToPILImage()(img)\n    return img\n\n'''\nReferences : https://www.tutorialspoint.com/how-to-draw-bounding-boxes-on-an-image-in-pytorch\n           : https://www.geeksforgeeks.org/python-convert-string-to-list-of-dictionaries/\n'''","metadata":{"execution":{"iopub.status.busy":"2023-10-08T06:55:21.417415Z","iopub.execute_input":"2023-10-08T06:55:21.417815Z","iopub.status.idle":"2023-10-08T06:55:21.751663Z","shell.execute_reply.started":"2023-10-08T06:55:21.417789Z","shell.execute_reply":"2023-10-08T06:55:21.750272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Splitting Randomly (tr, val, tst)","metadata":{}},{"cell_type":"code","source":"df_tr_v3['bboxes'] = df_tr_v3.annotations.apply(lambda x : get_bbox(x))\ndf_tr_v3['img_dim'] = df_tr_v3.path.apply(lambda x : get_imgsize(x))\n\ntot = df_tr_v3.shape[0]\nidxs = np.argsort(np.random.rand(tot))\ntr_idxs , val_idxs , tst_idxs = idxs[:(int)(0.75*len(idxs))] , idxs[(int)(0.75*len(idxs)):(int)(0.85*len(idxs))] , idxs[(int)(0.85*len(idxs)):]\n\ndf_tr = df_tr_v3.loc[tr_idxs , :].reset_index(drop = True)\ndf_val = df_tr_v3.loc[val_idxs , :].reset_index(drop = True)\ndf_tst = df_tr_v3.loc[tst_idxs , :].reset_index(drop = True)\n\ndf_tr.shape , df_val.shape , df_tst.shape","metadata":{"execution":{"iopub.status.busy":"2023-10-08T06:55:35.620280Z","iopub.execute_input":"2023-10-08T06:55:35.620763Z","iopub.status.idle":"2023-10-08T06:57:27.606893Z","shell.execute_reply.started":"2023-10-08T06:55:35.620728Z","shell.execute_reply":"2023-10-08T06:57:27.605387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Image Processing","metadata":{}},{"cell_type":"code","source":"import albumentations as A\n\n\ntransform = A.Compose([\n    A.CLAHE(clip_limit=5.0, tile_grid_size=(6,6), always_apply=True),\n#     A.Equalize(mode='cv', always_apply=True),\n])","metadata":{"execution":{"iopub.status.busy":"2023-10-08T07:17:57.436107Z","iopub.execute_input":"2023-10-08T07:17:57.436626Z","iopub.status.idle":"2023-10-08T07:17:57.443417Z","shell.execute_reply.started":"2023-10-08T07:17:57.436579Z","shell.execute_reply":"2023-10-08T07:17:57.441990Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transformed = transform(image=cv2.imread(df_tr.iloc[1201, 4]))\ntransformed_image = transformed['image']","metadata":{"execution":{"iopub.status.busy":"2023-10-08T07:17:59.499941Z","iopub.execute_input":"2023-10-08T07:17:59.500759Z","iopub.status.idle":"2023-10-08T07:17:59.546137Z","shell.execute_reply.started":"2023-10-08T07:17:59.500710Z","shell.execute_reply":"2023-10-08T07:17:59.544555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(transformed_image)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T07:18:01.111314Z","iopub.execute_input":"2023-10-08T07:18:01.112344Z","iopub.status.idle":"2023-10-08T07:18:01.572567Z","shell.execute_reply.started":"2023-10-08T07:18:01.112255Z","shell.execute_reply":"2023-10-08T07:18:01.571012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Saving Images","metadata":{}},{"cell_type":"code","source":"BASE_PATH = \"/kaggle/working/dataset\"\nPATH_IMAGES = os.path.join(BASE_PATH , \"images\")\nPATH_LABELS = os.path.join(BASE_PATH , \"labels\")\nif not os.path.exists(BASE_PATH):\n    os.mkdir(BASE_PATH)\n    os.mkdir(PATH_IMAGES)\n    os.mkdir(PATH_LABELS)\n\ndef create_dataset(df:pd.DataFrame, name:str):\n    p1 , p2 = os.path.join(PATH_LABELS , name), os.path.join(PATH_IMAGES , name)\n    os.mkdir(p1)\n    os.mkdir(p2)\n    \n    for i in range(600):\n        img = cv2.imread(df.iloc[i,4])\n        img_id = df.iloc[i,2]\n        transformed_image = transform(image=img)['image']\n        labels_coco = df.iloc[i, 6]\n        label_path = os.path.join(p1,img_id+'.txt')\n        img_path = os.path.join(p2,img_id+'.jpg')\n        cv2.imwrite(img_path, transformed_image)\n        \n        with open(label_path, \"w\", encoding='utf-8') as file:\n            for box in labels_coco:\n                box_ = [0]\n                x,y,w,h = box[0] ,box[1] , box[2] , box[3]\n                image_w, image_h = 1280, 720\n                box_.extend(coco2yolo( x,y,w,h ,image_w, image_h))\n                for x in box_:\n                    file.write(str(x)+'\\t')\n                file.write('\\n')\n        file.close()\n        \n        \ncreate_dataset(df_tr, 'train')\ncreate_dataset(df_tst, 'test')\ncreate_dataset(df_val, 'validation')","metadata":{"execution":{"iopub.status.busy":"2023-10-08T07:18:12.742188Z","iopub.execute_input":"2023-10-08T07:18:12.742677Z","iopub.status.idle":"2023-10-08T07:20:25.515857Z","shell.execute_reply.started":"2023-10-08T07:18:12.742641Z","shell.execute_reply":"2023-10-08T07:20:25.514213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Plotting Images ","metadata":{}},{"cell_type":"code","source":"fig , axs = plt.subplots(3,2 , figsize = (15,12))\n\nimges = np.random.choice(df_tr.index , 6)\nfor i in range(6):\n    r, c = i//2 , i%2\n    path = df_tr_v3.path.iloc[imges[r]]\n    \n    bboxes = df_tr_v3.bboxes.iloc[imges[r]]     # in coco format\n    voc_bbox = [coco2voc(bbox[0], bbox[1], bbox[2] ,bbox[3]) for bbox in bboxes]\n    colors=[(255,0,0) for i in range(len(bboxes))]\n    labels = ['cot']*len(voc_bbox)\n    img = plot_bbox_image(path, labels,voc_bbox, colors)\n    \n    img = plot_bbox_image(path, labels,voc_bbox, colors)\n    axs[r,c].imshow(img)\n    axs[r,c].set_xticks([])\n    axs[r,c].set_yticks([])\n\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2023-10-08T07:22:19.151232Z","iopub.execute_input":"2023-10-08T07:22:19.151818Z","iopub.status.idle":"2023-10-08T07:22:21.555507Z","shell.execute_reply.started":"2023-10-08T07:22:19.151783Z","shell.execute_reply":"2023-10-08T07:22:21.553755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# YOLOv5s IMPLEMENTATION","metadata":{}},{"cell_type":"code","source":"!git clone https://github.com/ultralytics/yolov5\n%cd yolov5    ","metadata":{"execution":{"iopub.status.busy":"2023-10-08T07:22:33.689914Z","iopub.execute_input":"2023-10-08T07:22:33.690469Z","iopub.status.idle":"2023-10-08T07:22:37.716932Z","shell.execute_reply.started":"2023-10-08T07:22:33.690430Z","shell.execute_reply":"2023-10-08T07:22:37.715498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Creating Config","metadata":{}},{"cell_type":"code","source":"import yaml\nfrom yaml.loader import SafeLoader\n\ndata = {\n    \"train\": '/kaggle/working/dataset/images/train',\n    \"val\": '/kaggle/working/dataset/images/validation',\n    \"test\": '/kaggle/working/dataset/images/test',\n    'nc': 1, \n    'names' : ['Cots']\n}\n\nwith open(\"/kaggle/working/yolov5/data/cots_data.yaml\", \"w\") as f:\n    yaml.dump(data, f)\n    \n!cat data/cots_data.yaml","metadata":{"execution":{"iopub.status.busy":"2023-10-08T07:23:08.272564Z","iopub.execute_input":"2023-10-08T07:23:08.274765Z","iopub.status.idle":"2023-10-08T07:23:09.427960Z","shell.execute_reply.started":"2023-10-08T07:23:08.274701Z","shell.execute_reply":"2023-10-08T07:23:09.426701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with open('models/yolov5s.yaml', 'r') as f:\n    data = yaml.load(f, Loader=SafeLoader)\n\ndata['nc'] = 1\nwith open(\"models/yolov5s.yaml\", \"w\") as f:\n    yaml.dump(data, f)","metadata":{"execution":{"iopub.status.busy":"2023-10-08T07:23:45.768463Z","iopub.execute_input":"2023-10-08T07:23:45.769248Z","iopub.status.idle":"2023-10-08T07:23:45.797193Z","shell.execute_reply.started":"2023-10-08T07:23:45.769208Z","shell.execute_reply":"2023-10-08T07:23:45.795556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training Model","metadata":{}},{"cell_type":"code","source":"#BATCH_SIZE = 32\n#EPOCHS = 50","metadata":{"execution":{"iopub.status.busy":"2023-10-01T21:52:02.514557Z","iopub.execute_input":"2023-10-01T21:52:02.517009Z","iopub.status.idle":"2023-10-01T21:52:02.525336Z","shell.execute_reply.started":"2023-10-01T21:52:02.516978Z","shell.execute_reply":"2023-10-01T21:52:02.524114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python train.py --batch 32 --epochs 1 --data 'data/cots_data.yaml' --weights 'models/yolov5s.pt' --project 'cots_detection' --name 'feature_extraction' --cache --freeze 12","metadata":{"execution":{"iopub.status.busy":"2023-10-08T07:24:34.764883Z","iopub.execute_input":"2023-10-08T07:24:34.765348Z","iopub.status.idle":"2023-10-08T07:36:07.517864Z","shell.execute_reply.started":"2023-10-08T07:24:34.765300Z","shell.execute_reply":"2023-10-08T07:36:07.516152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# INFERENCE","metadata":{}},{"cell_type":"code","source":"import os\nimport random\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\n\nimage_folder = '/kaggle/working/yolov5/cots_detection/feature_extraction'\n\n\nimage_files = [f for f in os.listdir(image_folder) if f.endswith('.jpg') or f.endswith('.png')]\n\n\n\n\nfor i, file in enumerate(image_files):\n    image_path = os.path.join(image_folder, file)\n    img = Image.open(image_path)\n    fig, ax = plt.subplots(1,1,figsize=(15, 12))\n    # Display the image using matplotlib\n    plt.imshow(img)\n    plt.axis('off')  # Turn off axis labels\n    plt.title(f\"Image {i + 1}\")\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-08T07:56:19.544546Z","iopub.execute_input":"2023-10-08T07:56:19.545092Z","iopub.status.idle":"2023-10-08T07:56:34.404019Z","shell.execute_reply.started":"2023-10-08T07:56:19.545056Z","shell.execute_reply":"2023-10-08T07:56:34.402543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python val.py --weights '/kaggle/working/yolov5/cots_detection/feature_extraction/weights/best.pt' --batch 32 --data 'data/cots_data.yaml' --task test --project 'val_cots' --name 'validation_on_test_data' --augment\n","metadata":{"execution":{"iopub.status.busy":"2023-10-08T07:58:26.337435Z","iopub.execute_input":"2023-10-08T07:58:26.337890Z","iopub.status.idle":"2023-10-08T07:59:23.274108Z","shell.execute_reply.started":"2023-10-08T07:58:26.337857Z","shell.execute_reply":"2023-10-08T07:59:23.272191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_result('/kaggle/working/yolov5/val_cots/validation_on_test_data/confusion_matrix.png')","metadata":{"execution":{"iopub.status.busy":"2023-10-01T22:07:56.507636Z","iopub.execute_input":"2023-10-01T22:07:56.508327Z","iopub.status.idle":"2023-10-01T22:07:57.666661Z","shell.execute_reply.started":"2023-10-01T22:07:56.508285Z","shell.execute_reply":"2023-10-01T22:07:57.665739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_result('/kaggle/working/yolov5/val_cots/validation_on_test_data/PR_curve.png')","metadata":{"execution":{"iopub.status.busy":"2023-10-01T22:07:57.668433Z","iopub.execute_input":"2023-10-01T22:07:57.669189Z","iopub.status.idle":"2023-10-01T22:07:58.318863Z","shell.execute_reply.started":"2023-10-01T22:07:57.669149Z","shell.execute_reply":"2023-10-01T22:07:58.317851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_result('/kaggle/working/yolov5/val_cots/validation_on_test_data/F1_curve.png')","metadata":{"execution":{"iopub.status.busy":"2023-10-01T22:07:58.320100Z","iopub.execute_input":"2023-10-01T22:07:58.321121Z","iopub.status.idle":"2023-10-01T22:07:58.964008Z","shell.execute_reply.started":"2023-10-01T22:07:58.321085Z","shell.execute_reply":"2023-10-01T22:07:58.963022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MODEL_PATH = '/kaggle/working/yolov5/runs_cots/feature_extraction/weights/best.pt'","metadata":{"execution":{"iopub.status.busy":"2023-10-01T22:07:58.965470Z","iopub.execute_input":"2023-10-01T22:07:58.966521Z","iopub.status.idle":"2023-10-01T22:07:58.971744Z","shell.execute_reply.started":"2023-10-01T22:07:58.966484Z","shell.execute_reply":"2023-10-01T22:07:58.970381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python detect.py --weights {MODEL_PATH}  --conf 0.6 --source '/kaggle/working/dataset/images/test' --project 'runs_test' --name 'detect_test' --augment\n","metadata":{"execution":{"iopub.status.busy":"2023-10-01T22:07:58.973397Z","iopub.execute_input":"2023-10-01T22:07:58.974070Z","iopub.status.idle":"2023-10-01T22:08:53.275662Z","shell.execute_reply.started":"2023-10-01T22:07:58.974034Z","shell.execute_reply":"2023-10-01T22:08:53.274550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport random\nfrom PIL import Image\nimport matplotlib.pyplot as plt\n\n\nimage_folder = '/kaggle/working/yolov5/cots_detection/detect_test'\n\n\nimage_files = [f for f in os.listdir(image_folder) if f.endswith('.jpg') or f.endswith('.png')]\n\n\nrandom_indices = random.sample(range(len(image_files)), 10)\n\n\nfor i, index in enumerate(random_indices):\n    image_path = os.path.join(image_folder, image_files[index])\n    img = Image.open(image_path)\n    fig, ax = plt.subplots(figsize=(15, 12))\n    # Display the image using matplotlib\n    plt.imshow(img)\n    plt.axis('off')  # Turn off axis labels\n    plt.title(f\"Image {i + 1}\")\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-01T22:08:53.277394Z","iopub.execute_input":"2023-10-01T22:08:53.277736Z","iopub.status.idle":"2023-10-01T22:09:00.852121Z","shell.execute_reply.started":"2023-10-01T22:08:53.277700Z","shell.execute_reply":"2023-10-01T22:09:00.850874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"import greatbarrierreef\n#","metadata":{"execution":{"iopub.status.busy":"2023-10-08T08:12:05.643963Z","iopub.execute_input":"2023-10-08T08:12:05.644858Z","iopub.status.idle":"2023-10-08T08:12:05.723120Z","shell.execute_reply.started":"2023-10-08T08:12:05.644786Z","shell.execute_reply":"2023-10-08T08:12:05.721112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"env = greatbarrierreef.make_env()\niter_test = env.iter_test()","metadata":{"execution":{"iopub.status.busy":"2023-10-01T22:15:41.880394Z","iopub.execute_input":"2023-10-01T22:15:41.880742Z","iopub.status.idle":"2023-10-01T22:15:41.919423Z","shell.execute_reply.started":"2023-10-01T22:15:41.880713Z","shell.execute_reply":"2023-10-01T22:15:41.918127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}