{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":67836,"databundleVersionId":7527339,"sourceType":"competition"},{"sourceId":8521750,"sourceType":"datasetVersion","datasetId":5088299}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"%pip install -Uqqq pycocotools","metadata":{"execution":{"iopub.status.busy":"2024-05-31T00:54:50.604286Z","iopub.execute_input":"2024-05-31T00:54:50.604818Z","iopub.status.idle":"2024-05-31T00:55:05.541989Z","shell.execute_reply.started":"2024-05-31T00:54:50.604782Z","shell.execute_reply":"2024-05-31T00:55:05.540656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Import","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.model_selection import GroupKFold\nfrom pathlib import Path\nfrom pycocotools.coco import COCO\nfrom PIL import Image\nimport os\nimport glob\nimport cv2\nfrom skimage import io\nimport pylab\nfrom tqdm import tqdm\nimport json\nimport matplotlib\nimport pdb","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-31T00:55:05.543952Z","iopub.execute_input":"2024-05-31T00:55:05.544329Z","iopub.status.idle":"2024-05-31T00:55:08.554523Z","shell.execute_reply.started":"2024-05-31T00:55:05.544295Z","shell.execute_reply":"2024-05-31T00:55:08.553429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load Data","metadata":{}},{"cell_type":"code","source":"!mkdir newcoco/data -p\n!cp /kaggle/input/dl-enigma-cocodataset/train2017/* newcoco/data/\n!cp /kaggle/input/dl-enigma-cocodataset/valid2017/* newcoco/data/","metadata":{"execution":{"iopub.status.busy":"2024-05-31T00:55:08.556265Z","iopub.execute_input":"2024-05-31T00:55:08.557702Z","iopub.status.idle":"2024-05-31T00:56:00.240992Z","shell.execute_reply.started":"2024-05-31T00:55:08.557648Z","shell.execute_reply":"2024-05-31T00:56:00.238741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# src_root = '../../../../datasets/voc2012/'\n# src_subset = 'images/'\n# src_file = src_root+'annotations/instances_train2012.json'\n# src_desc = 'train2017_voc12' \n\nsrc_root = '/kaggle/input/dl-enigma-cocodataset/'\nsrc_file_train = os.path.join(src_root, 'annotations_train.json')\nsrc_file_valid = os.path.join(src_root, 'annotations_valid.json')\nsrc_file = '/kaggle/working/newcoco/annotations.json'\nfile_path = '/kaggle/working/newcoco/data/'\n\n","metadata":{"execution":{"iopub.status.busy":"2024-05-31T00:56:00.246055Z","iopub.execute_input":"2024-05-31T00:56:00.246757Z","iopub.status.idle":"2024-05-31T00:56:00.254224Z","shell.execute_reply.started":"2024-05-31T00:56:00.246695Z","shell.execute_reply":"2024-05-31T00:56:00.252544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reading the json file\nwith open(src_file_train, 'r') as f:\n    root_train = json.load(f)\n\n# Reading the json file\nwith open(src_file_valid, 'r') as f:\n    root_valid = json.load(f)\n\nroot = root_train\nroot['images'] += root_valid['images']\nroot['annotations'] += root_valid['annotations']\n\nwith open(src_file, \"w\") as f:\n    json.dump(root, f)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-31T01:12:02.758154Z","iopub.execute_input":"2024-05-31T01:12:02.758686Z","iopub.status.idle":"2024-05-31T01:12:04.149682Z","shell.execute_reply.started":"2024-05-31T01:12:02.758649Z","shell.execute_reply":"2024-05-31T01:12:04.148373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coco_obj = COCO(src_file)","metadata":{"execution":{"iopub.status.busy":"2024-05-31T00:56:01.694731Z","iopub.execute_input":"2024-05-31T00:56:01.695169Z","iopub.status.idle":"2024-05-31T00:56:02.156554Z","shell.execute_reply.started":"2024-05-31T00:56:01.695134Z","shell.execute_reply":"2024-05-31T00:56:02.155350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Basic High Level Information","metadata":{}},{"cell_type":"code","source":"# Basic High Level Information\n\nn_images = len(root['images'])\nn_boxes = len(root['annotations'])\nn_categ = len(root['categories'])\n\n# height, width\nheights = [x['height'] for x in root['images']]\nwidths = [x['width'] for x in root['images']]\n\n\n\n\nprint('Number of images: ',n_images)\nprint('Number of bounding boxes: ', n_boxes)\nprint('Number of classes: ', n_categ)\nprint('Max min avg height: ', max(heights), min(heights), int(sum(heights)/len(heights)))\nprint('Max min avg width: ', max(widths), min(widths), int(sum(widths)/len(widths)))","metadata":{"execution":{"iopub.status.busy":"2024-05-31T00:56:02.157781Z","iopub.execute_input":"2024-05-31T00:56:02.158104Z","iopub.status.idle":"2024-05-31T00:56:02.170462Z","shell.execute_reply.started":"2024-05-31T00:56:02.158079Z","shell.execute_reply":"2024-05-31T00:56:02.169509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Mean, Std Over Images","metadata":{}},{"cell_type":"code","source":"def calc_avg_mean_std(img_names, img_root):\n    mean_sum = np.array([0., 0., 0.])\n    std_sum = np.array([0., 0., 0.])\n    n_images = len(img_names)\n    for img_name in tqdm(img_names):\n        img = cv2.imread(img_root + img_name + '.jpg')\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        mean, std = cv2.meanStdDev(img)\n        mean_sum += np.squeeze(mean)\n        std_sum += np.squeeze(std)\n    return (mean_sum / n_images, std_sum / n_images)","metadata":{"execution":{"iopub.status.busy":"2024-05-31T01:12:19.507415Z","iopub.execute_input":"2024-05-31T01:12:19.507965Z","iopub.status.idle":"2024-05-31T01:12:19.515765Z","shell.execute_reply.started":"2024-05-31T01:12:19.507912Z","shell.execute_reply":"2024-05-31T01:12:19.514658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_root = '/kaggle/working/newcoco/data/'\nimage_ids = [image['id'] for image in root['images']]\nmean_avg, std_avg = calc_avg_mean_std(image_ids, img_root)","metadata":{"execution":{"iopub.status.busy":"2024-05-31T01:12:21.172654Z","iopub.execute_input":"2024-05-31T01:12:21.173666Z","iopub.status.idle":"2024-05-31T01:15:45.023238Z","shell.execute_reply.started":"2024-05-31T01:12:21.173616Z","shell.execute_reply":"2024-05-31T01:15:45.021764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(mean_avg)\nprint(std_avg)","metadata":{"execution":{"iopub.status.busy":"2024-05-31T01:21:16.733565Z","iopub.execute_input":"2024-05-31T01:21:16.734201Z","iopub.status.idle":"2024-05-31T01:21:16.743045Z","shell.execute_reply.started":"2024-05-31T01:21:16.734160Z","shell.execute_reply":"2024-05-31T01:21:16.741238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Distribution of objects across images","metadata":{}},{"cell_type":"code","source":"# Objects per image distribution\n\nimg2nboxes = {} # mapping \"image id\" to \"number of boxes\"\n\nfor ann in root['annotations']:\n    img_id = ann['image_id']\n    \n    if img_id in img2nboxes.keys():\n        img2nboxes[img_id] += 1\n    else:\n        img2nboxes[img_id] = 1\n\nnboxes_list = list(img2nboxes.values())\n\nmin_nboxes = min(nboxes_list)\nmax_nboxes = max(nboxes_list)\navg_nboxes = int(sum(nboxes_list)/len(nboxes_list))","metadata":{"execution":{"iopub.status.busy":"2024-05-31T00:47:53.500315Z","iopub.status.idle":"2024-05-31T00:47:53.501110Z","shell.execute_reply.started":"2024-05-31T00:47:53.500879Z","shell.execute_reply":"2024-05-31T00:47:53.500906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out = pd.cut(nboxes_list, bins=np.arange(0,max_nboxes+10,10), include_lowest=True)\n\ncounts = out.value_counts().values\nlabels = [(int(i.left),int(i.right)) for i in out.value_counts().index.categories]\n\ngraph_xind = [i[0] for i in labels] \nticks = [ '('+str(i[0])+','+ str(i[1])+')' for i in labels]\n\nplt.figure(figsize=(10,5))\nplt.bar(graph_xind, counts, tick_label=ticks, width=5)\nplt.xlabel('Number of objects')\nplt.ylabel('Number of images')\nplt.title('Number of objects distribution over the dataset')\nplt.show()\n\nprint(\"Number of images having atleast one box: \", len(nboxes_list))\nprint(\"Min number of boxes per image: \", min_nboxes)\nprint(\"Max number of boxes per image: \", max_nboxes)\nprint(\"Avg number of boxes per image: \", avg_nboxes)","metadata":{"execution":{"iopub.status.busy":"2024-05-31T00:47:53.502564Z","iopub.status.idle":"2024-05-31T00:47:53.503198Z","shell.execute_reply.started":"2024-05-31T00:47:53.502998Z","shell.execute_reply":"2024-05-31T00:47:53.503017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Class wise distribution of objects","metadata":{}},{"cell_type":"code","source":"# Class distribution\nclass2nboxes = {} \n\nfor ann in root['annotations']:\n    cat_id = ann['category_id']\n    \n    if cat_id in class2nboxes.keys():\n        class2nboxes[cat_id] += 1\n    else:\n        class2nboxes[cat_id] = 1\n\nsorted_c2nb = [(k,v)for k, v in sorted(class2nboxes.items(), reverse=True, key=lambda item: item[1])]\n\n# top 20 classes\ntop = min(len(sorted_c2nb),20)\n\n# to plot\ny = [i[1] for i in sorted_c2nb[:top]]\nx = [i[0] for i in sorted_c2nb[:top]]\n\nplt.figure(figsize=(10,5))\nplt.bar(np.arange(len(y)),y, width=0.5,tick_label=x,color='orange')\nplt.xlim(-0.5,len(y))\nplt.xlabel('Category Id')\nplt.ylabel('Number of boxes')\nplt.title('Class distribution (decreasing order)')\nplt.show()\n\n\n\ncateg_map = {x['id']: x['name'] for x in root['categories']}\nfor k in categ_map.keys():\n    print(k,'->',categ_map[k], end=\", \")","metadata":{"execution":{"iopub.status.busy":"2024-05-31T00:47:53.505033Z","iopub.status.idle":"2024-05-31T00:47:53.505454Z","shell.execute_reply.started":"2024-05-31T00:47:53.505263Z","shell.execute_reply":"2024-05-31T00:47:53.505281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Class wise bounding box area distribution","metadata":{}},{"cell_type":"code","source":"# Class wise bounding box area distribution\n\nbbox_areas = {} # key: class index, value -> a list of bounding box areas\n\nfor ann in root['annotations']:\n    area = ann['area']\n    cat_id = ann['category_id']\n    \n    if area <= 0.0:\n        continue\n    \n    if cat_id in bbox_areas.keys():\n        bbox_areas[cat_id].append(area)\n    else:\n        bbox_areas[cat_id] = [area]\n\n\nbbox_avg_areas = []\n\nfor cat in bbox_areas.keys():\n    areas = bbox_areas[cat]\n    avg_area = int(sum(areas)/len(areas))\n    \n    bbox_avg_areas.append((cat,avg_area))\n\nbbox_avg_areas = sorted(bbox_avg_areas, key = lambda x: x[1])","metadata":{"execution":{"iopub.status.busy":"2024-05-31T00:47:53.507523Z","iopub.status.idle":"2024-05-31T00:47:53.508549Z","shell.execute_reply.started":"2024-05-31T00:47:53.507752Z","shell.execute_reply":"2024-05-31T00:47:53.507769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"top = min(10, len(bbox_avg_areas))\n\nplt.figure(figsize=(10,10))\n\ny = [item[1] for item in bbox_avg_areas[-top:]]\nx = [item[0] for item in bbox_avg_areas[-top:]]\n\ny2 = [item[1] for item in bbox_avg_areas[:top]]\nx2 = [item[0] for item in bbox_avg_areas[:top]]\n\nplt.subplot(211)\nplt.bar(np.arange(len(y)),y, width=0.5,tick_label=x,color='green')\nplt.xlim(-0.5,len(y))\n# plt.xlabel('Category Id')\nplt.ylabel('Average bounding box area in pixel squared')\nplt.title('Top '+str(top)+' Classes with highest avg bounding box size')\n\n\nplt.subplot(212)\nplt.bar(np.arange(len(y2)),y2, width=0.5,tick_label=x2,color='red')\nplt.xlim(-0.5,len(y2))\nplt.xlabel('Category Id')\nplt.ylabel('Average bounding box area in pixel squared')\nplt.title('Top '+str(top)+' Classes with lowest avg bounding box size')\nplt.show()\n\n\n\n\ncateg_map = {x['id']: x['name'] for x in root['categories']}\nfor k in categ_map.keys():\n    print(k,'->',categ_map[k], end=\", \")","metadata":{"execution":{"iopub.status.busy":"2024-05-31T00:47:53.510327Z","iopub.status.idle":"2024-05-31T00:47:53.511093Z","shell.execute_reply.started":"2024-05-31T00:47:53.510887Z","shell.execute_reply":"2024-05-31T00:47:53.510907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Viewing random images","metadata":{}},{"cell_type":"code","source":"# generate a random image\nimgIds = [i['id'] for i in root['images']]\ncatIds = [i['id'] for i in root['categories']]\nimg = coco_obj.imgs[imgIds[np.random.randint(0,len(imgIds))]]\nimg_path= os.path.join(file_path, img['file_name'])\nI = io.imread(img_path)\n\n# plot annotations\nplt.figure(figsize=(6,6))\nplt.axis('off')\nplt.imshow(I)\nannIds = coco_obj.getAnnIds(imgIds=[img['id']], catIds=catIds, iscrowd=None)\nanns = coco_obj.loadAnns(annIds)\n\n#  disable segmentation\nfor a in anns:\n    if 'segmentation' in a.keys():\n        a['segmentation'] = []\ncoco_obj.showAnns(anns, draw_bbox=True)\nclasses = [categ_map[a['category_id']] for a in anns]\nprint(img['id'])\nprint(classes)","metadata":{"execution":{"iopub.status.busy":"2024-05-31T00:47:53.512388Z","iopub.status.idle":"2024-05-31T00:47:53.513213Z","shell.execute_reply.started":"2024-05-31T00:47:53.513009Z","shell.execute_reply":"2024-05-31T00:47:53.513028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Make dataframe","metadata":{}},{"cell_type":"code","source":"train_df = pd.DataFrame(root['images'])\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-31T00:47:53.514323Z","iopub.status.idle":"2024-05-31T00:47:53.515042Z","shell.execute_reply.started":"2024-05-31T00:47:53.514764Z","shell.execute_reply":"2024-05-31T00:47:53.514788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img2nboxes = {} # mapping \"image id\" to \"number of boxes\"\n\nfor ann in root['annotations']:\n    img_id = ann['image_id']\n    \n    if img_id in img2nboxes.keys():\n        img2nboxes[img_id] += 1\n    else:\n        img2nboxes[img_id] = 1\n\ntrain_df['box_count'] = train_df['id'].apply(lambda x : img2nboxes[x])\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-31T00:47:53.516112Z","iopub.status.idle":"2024-05-31T00:47:53.516805Z","shell.execute_reply.started":"2024-05-31T00:47:53.516618Z","shell.execute_reply":"2024-05-31T00:47:53.516635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['location'] = train_df['id'].apply(lambda x : x[:x.rfind(\"_\") - 1])\ntrain_df['location'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-05-31T00:47:53.518268Z","iopub.status.idle":"2024-05-31T00:47:53.518833Z","shell.execute_reply.started":"2024-05-31T00:47:53.518656Z","shell.execute_reply":"2024-05-31T00:47:53.518673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.groupby(\"location\")[['width', 'height', 'box_count']].mean()","metadata":{"execution":{"iopub.status.busy":"2024-05-31T00:47:53.520005Z","iopub.status.idle":"2024-05-31T00:47:53.520690Z","shell.execute_reply.started":"2024-05-31T00:47:53.520452Z","shell.execute_reply":"2024-05-31T00:47:53.520472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}