{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":31703,"databundleVersionId":2871752,"sourceType":"competition"}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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":"2024-03-29T06:27:40.408635Z","iopub.execute_input":"2024-03-29T06:27:40.409058Z","iopub.status.idle":"2024-03-29T06:27:40.418483Z","shell.execute_reply.started":"2024-03-29T06:27:40.409027Z","shell.execute_reply":"2024-03-29T06:27:40.417185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.set_printoptions(precision = 4, suppress = True)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T06:27:40.420065Z","iopub.execute_input":"2024-03-29T06:27:40.420409Z","iopub.status.idle":"2024-03-29T06:27:40.429528Z","shell.execute_reply.started":"2024-03-29T06:27:40.420381Z","shell.execute_reply":"2024-03-29T06:27:40.428231Z"},"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":"2024-03-29T06:27:40.432265Z","iopub.execute_input":"2024-03-29T06:27:40.43286Z","iopub.status.idle":"2024-03-29T06:27:41.364097Z","shell.execute_reply.started":"2024-03-29T06:27:40.432817Z","shell.execute_reply":"2024-03-29T06:27:41.362938Z"},"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":"2024-03-29T06:27:41.365713Z","iopub.execute_input":"2024-03-29T06:27:41.366054Z","iopub.status.idle":"2024-03-29T06:27:42.581408Z","shell.execute_reply.started":"2024-03-29T06:27:41.366027Z","shell.execute_reply":"2024-03-29T06:27:42.580139Z"},"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":"2024-03-29T06:27:42.582892Z","iopub.execute_input":"2024-03-29T06:27:42.583255Z","iopub.status.idle":"2024-03-29T06:27:42.964992Z","shell.execute_reply.started":"2024-03-29T06:27:42.583224Z","shell.execute_reply":"2024-03-29T06:27:42.963854Z"},"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":"2024-03-29T06:27:42.968368Z","iopub.execute_input":"2024-03-29T06:27:42.969129Z","iopub.status.idle":"2024-03-29T06:27:43.013666Z","shell.execute_reply.started":"2024-03-29T06:27:42.969086Z","shell.execute_reply":"2024-03-29T06:27:43.012289Z"},"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":"2024-03-29T06:27:43.015368Z","iopub.execute_input":"2024-03-29T06:27:43.016214Z","iopub.status.idle":"2024-03-29T06:27:43.042012Z","shell.execute_reply.started":"2024-03-29T06:27:43.016176Z","shell.execute_reply":"2024-03-29T06:27:43.040898Z"},"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":"2024-03-29T06:27:43.043693Z","iopub.execute_input":"2024-03-29T06:27:43.04403Z","iopub.status.idle":"2024-03-29T06:27:43.524407Z","shell.execute_reply.started":"2024-03-29T06:27:43.044002Z","shell.execute_reply":"2024-03-29T06:27:43.522725Z"},"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":"2024-03-29T06:27:43.526285Z","iopub.execute_input":"2024-03-29T06:27:43.526849Z","iopub.status.idle":"2024-03-29T06:27:43.548372Z","shell.execute_reply.started":"2024-03-29T06:27:43.526808Z","shell.execute_reply":"2024-03-29T06:27:43.547299Z"},"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":"2024-03-29T06:27:43.549929Z","iopub.execute_input":"2024-03-29T06:27:43.550274Z","iopub.status.idle":"2024-03-29T06:27:44.220973Z","shell.execute_reply.started":"2024-03-29T06:27:43.550238Z","shell.execute_reply":"2024-03-29T06:27:44.219734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_tr_v3.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-29T06:27:44.222386Z","iopub.execute_input":"2024-03-29T06:27:44.222753Z","iopub.status.idle":"2024-03-29T06:27:44.238332Z","shell.execute_reply.started":"2024-03-29T06:27:44.222713Z","shell.execute_reply":"2024-03-29T06:27:44.237075Z"},"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":"2024-03-29T06:27:44.243267Z","iopub.execute_input":"2024-03-29T06:27:44.243718Z","iopub.status.idle":"2024-03-29T06:27:44.248114Z","shell.execute_reply.started":"2024-03-29T06:27:44.243687Z","shell.execute_reply":"2024-03-29T06:27:44.2472Z"},"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":"2024-03-29T06:27:44.249531Z","iopub.execute_input":"2024-03-29T06:27:44.249886Z","iopub.status.idle":"2024-03-29T06:27:44.613113Z","shell.execute_reply.started":"2024-03-29T06:27:44.249843Z","shell.execute_reply":"2024-03-29T06:27:44.611955Z"},"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":"2024-03-29T06:27:44.614652Z","iopub.execute_input":"2024-03-29T06:27:44.615134Z","iopub.status.idle":"2024-03-29T06:29:29.762825Z","shell.execute_reply.started":"2024-03-29T06:27:44.6151Z","shell.execute_reply":"2024-03-29T06:29:29.761481Z"},"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":"2024-03-29T06:29:29.764521Z","iopub.execute_input":"2024-03-29T06:29:29.765646Z","iopub.status.idle":"2024-03-29T06:29:29.772929Z","shell.execute_reply.started":"2024-03-29T06:29:29.765601Z","shell.execute_reply":"2024-03-29T06:29:29.771433Z"},"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":"2024-03-29T06:29:29.774627Z","iopub.execute_input":"2024-03-29T06:29:29.776707Z","iopub.status.idle":"2024-03-29T06:29:30.025975Z","shell.execute_reply.started":"2024-03-29T06:29:29.776661Z","shell.execute_reply":"2024-03-29T06:29:30.025043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(transformed_image)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T06:29:30.02711Z","iopub.execute_input":"2024-03-29T06:29:30.028057Z","iopub.status.idle":"2024-03-29T06:29:30.629182Z","shell.execute_reply.started":"2024-03-29T06:29:30.028025Z","shell.execute_reply":"2024-03-29T06:29:30.627971Z"},"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":"2024-03-29T06:29:30.631063Z","iopub.execute_input":"2024-03-29T06:29:30.632072Z","iopub.status.idle":"2024-03-29T06:31:35.233097Z","shell.execute_reply.started":"2024-03-29T06:29:30.632031Z","shell.execute_reply":"2024-03-29T06:31:35.231659Z"},"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":"2024-03-29T06:31:35.234702Z","iopub.execute_input":"2024-03-29T06:31:35.235093Z","iopub.status.idle":"2024-03-29T06:31:38.043811Z","shell.execute_reply.started":"2024-03-29T06:31:35.23506Z","shell.execute_reply":"2024-03-29T06:31:38.042129Z"},"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":"2024-03-29T06:31:38.045565Z","iopub.execute_input":"2024-03-29T06:31:38.046658Z","iopub.status.idle":"2024-03-29T06:31:41.149898Z","shell.execute_reply.started":"2024-03-29T06:31:38.046621Z","shell.execute_reply":"2024-03-29T06:31:41.148462Z"},"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":"2024-03-29T06:31:41.151906Z","iopub.execute_input":"2024-03-29T06:31:41.152399Z","iopub.status.idle":"2024-03-29T06:31:42.270315Z","shell.execute_reply.started":"2024-03-29T06:31:41.152354Z","shell.execute_reply":"2024-03-29T06:31:42.269108Z"},"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":"2024-03-29T06:31:42.272594Z","iopub.execute_input":"2024-03-29T06:31:42.273023Z","iopub.status.idle":"2024-03-29T06:31:42.311525Z","shell.execute_reply.started":"2024-03-29T06:31:42.272982Z","shell.execute_reply":"2024-03-29T06:31:42.310302Z"},"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":"2024-03-29T06:31:42.313255Z","iopub.execute_input":"2024-03-29T06:31:42.313602Z","iopub.status.idle":"2024-03-29T06:31:42.317535Z","shell.execute_reply.started":"2024-03-29T06:31:42.313574Z","shell.execute_reply":"2024-03-29T06:31:42.316466Z"},"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":"2024-03-29T06:31:42.31855Z","iopub.execute_input":"2024-03-29T06:31:42.318849Z","iopub.status.idle":"2024-03-29T06:42:27.091487Z","shell.execute_reply.started":"2024-03-29T06:31:42.318822Z","shell.execute_reply":"2024-03-29T06:42:27.085206Z"},"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":"2024-03-29T06:42:27.106258Z","iopub.execute_input":"2024-03-29T06:42:27.108133Z","iopub.status.idle":"2024-03-29T06:42:50.868844Z","shell.execute_reply.started":"2024-03-29T06:42:27.107951Z","shell.execute_reply":"2024-03-29T06:42:50.867074Z"},"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":"2024-03-29T06:42:50.870388Z","iopub.execute_input":"2024-03-29T06:42:50.87085Z","iopub.status.idle":"2024-03-29T06:48:02.638045Z","shell.execute_reply.started":"2024-03-29T06:42:50.870816Z","shell.execute_reply":"2024-03-29T06:48:02.636515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\ndef plot_result(image_path):\n    # Display the image\n    plot_result('/kaggle/working/yolov5/val_cots/validation_on_test_data/confusion_matrix.png')","metadata":{"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":"2024-03-29T08:08:59.177462Z","iopub.execute_input":"2024-03-29T08:08:59.177923Z","iopub.status.idle":"2024-03-29T08:08:59.276565Z","shell.execute_reply.started":"2024-03-29T08:08:59.177889Z","shell.execute_reply":"2024-03-29T08:08:59.274916Z"},"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":"2024-03-29T08:08:51.370668Z","iopub.status.idle":"2024-03-29T08:08:51.37116Z","shell.execute_reply.started":"2024-03-29T08:08:51.370913Z","shell.execute_reply":"2024-03-29T08:08:51.370958Z"},"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":"2024-03-29T07:11:48.870911Z","iopub.execute_input":"2024-03-29T07:11:48.871387Z","iopub.status.idle":"2024-03-29T07:11:48.876653Z","shell.execute_reply.started":"2024-03-29T07:11:48.871348Z","shell.execute_reply":"2024-03-29T07:11:48.875366Z"},"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":"2024-03-29T08:08:22.480601Z","iopub.execute_input":"2024-03-29T08:08:22.482398Z","iopub.status.idle":"2024-03-29T08:08:23.676603Z","shell.execute_reply.started":"2024-03-29T08:08:22.482345Z","shell.execute_reply":"2024-03-29T08:08:23.674183Z"},"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":"2024-03-29T06:48:03.375751Z","iopub.status.idle":"2024-03-29T06:48:03.37622Z","shell.execute_reply.started":"2024-03-29T06:48:03.376018Z","shell.execute_reply":"2024-03-29T06:48:03.376038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"import greatbarrierreef\n#","metadata":{"execution":{"iopub.status.busy":"2024-03-29T06:48:03.37767Z","iopub.status.idle":"2024-03-29T06:48:03.378096Z","shell.execute_reply.started":"2024-03-29T06:48:03.377901Z","shell.execute_reply":"2024-03-29T06:48:03.37792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"env = greatbarrierreef.make_env()\niter_test = env.iter_test()","metadata":{"execution":{"iopub.status.busy":"2024-03-29T06:48:03.379777Z","iopub.status.idle":"2024-03-29T06:48:03.381556Z","shell.execute_reply.started":"2024-03-29T06:48:03.381242Z","shell.execute_reply":"2024-03-29T06:48:03.381271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}