{"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":"#installing fiftyone\n!pip install fiftyone","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from ast import literal_eval\nimport pandas as pd\nfrom tqdm.notebook import tqdm\n\nTRAIN_PATH = '/kaggle/input/tensorflow-great-barrier-reef/train_images'\nN_SAMP = 6000\n\ndf = pd.read_csv('/kaggle/input/tensorflow-great-barrier-reef/train.csv')\n\ndf['is_valid'] = df['video_id'] == 2\ndf['annotations'] = df['annotations'].apply(literal_eval)\ndf['path'] = df.apply(lambda row: f\"{TRAIN_PATH}/video_{row['video_id']}/{row['video_frame']}.jpg\", axis = 1)\n\ndf.tail()","metadata":{"execution":{"iopub.status.busy":"2022-02-06T15:20:54.841322Z","iopub.execute_input":"2022-02-06T15:20:54.841728Z","iopub.status.idle":"2022-02-06T15:20:55.756335Z","shell.execute_reply.started":"2022-02-06T15:20:54.841695Z","shell.execute_reply":"2022-02-06T15:20:55.755546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Convert the Csv annotation to coco:","metadata":{}},{"cell_type":"code","source":"def coco(df):\n    \n    annotion_id = 0\n    images = []\n    annotations = []\n\n    categories = [{'id': 0, 'name': 'cots'}]\n\n    for i, row in tqdm(df.iterrows(), total = len(df)):\n\n        images.append({\n            \"id\": i,\n            \"file_name\": f\"{row['image_id']}.jpg\",\n            \"height\": 720,\n            \"width\": 1280,\n        })\n        for bbox in row['annotations']:\n            annotations.append({\n                \"id\": annotion_id,\n                \"image_id\": i,\n                \"category_id\": 0,\n                \"bbox\": list(bbox.values()),\n                \"area\": bbox['width'] * bbox['height'],\n                \"segmentation\": [],\n                \"iscrowd\": 0\n            })\n            annotion_id += 1\n\n    json_file = {'categories':categories, 'images':images, 'annotations':annotations}\n    return json_file","metadata":{"execution":{"iopub.status.busy":"2022-02-06T15:21:03.504125Z","iopub.execute_input":"2022-02-06T15:21:03.504507Z","iopub.status.idle":"2022-02-06T15:21:03.515765Z","shell.execute_reply.started":"2022-02-06T15:21:03.504467Z","shell.execute_reply":"2022-02-06T15:21:03.514697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"json_train = coco(df[~df['is_valid']])\njson_valid = coco(df[ df['is_valid']])","metadata":{"execution":{"iopub.status.busy":"2022-02-06T15:21:22.729521Z","iopub.execute_input":"2022-02-06T15:21:22.729971Z","iopub.status.idle":"2022-02-06T15:21:24.911628Z","shell.execute_reply.started":"2022-02-06T15:21:22.729926Z","shell.execute_reply":"2022-02-06T15:21:24.91031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#creating 2 json file and dumping train and valid file into it.\nimport json\n\nwith open('/kaggle/working/annotations_train.json', 'w', encoding='utf-8') as f:\n    json.dump(json_train, f, ensure_ascii=True, indent=4)\n    \nwith open('/kaggle/working/annotations_valid.json', 'w', encoding='utf-8') as f:\n    json.dump(json_valid, f, ensure_ascii=True, indent=4)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T15:21:41.148215Z","iopub.execute_input":"2022-02-06T15:21:41.148864Z","iopub.status.idle":"2022-02-06T15:21:41.843987Z","shell.execute_reply.started":"2022-02-06T15:21:41.148819Z","shell.execute_reply":"2022-02-06T15:21:41.842825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Making 2 directories train2017 and test 2017\nimport os\nos.makedirs('/kaggle/working/train2017', exist_ok=True)\nos.makedirs('/kaggle/working/val2017', exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T15:21:45.659099Z","iopub.execute_input":"2022-02-06T15:21:45.659981Z","iopub.status.idle":"2022-02-06T15:21:45.665609Z","shell.execute_reply.started":"2022-02-06T15:21:45.659935Z","shell.execute_reply":"2022-02-06T15:21:45.664391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nfor i, row in tqdm(df.iterrows(), total = len(df)):\n    base_dir = 'val2017' if row['is_valid'] else 'train2017'\n    fname = f\"{row['image_id']}.jpg\"\n    shutil.copyfile(row['path'], f\"/kaggle/working/{base_dir}/{fname}\")","metadata":{"execution":{"iopub.status.busy":"2022-02-06T15:21:48.047663Z","iopub.execute_input":"2022-02-06T15:21:48.049338Z","iopub.status.idle":"2022-02-06T15:26:49.040232Z","shell.execute_reply.started":"2022-02-06T15:21:48.049265Z","shell.execute_reply":"2022-02-06T15:26:49.038056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load COCO formatted dataset\nimport fiftyone as fo\ncoco_dataset = fo.Dataset.from_dir(\n    dataset_type=fo.types.COCODetectionDataset,\n    data_path= \"/kaggle/working/train2017\",\n    labels_path=\"/kaggle/working/annotations_train.json\",\n    include_id=True\n)\n\n# Verify that the class list for our dataset was imported\nprint(coco_dataset.default_classes)  # ['airplane', 'apple', ...]\n\nprint(coco_dataset)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-02-06T15:27:35.283315Z","iopub.execute_input":"2022-02-06T15:27:35.284759Z","iopub.status.idle":"2022-02-06T15:27:58.235461Z","shell.execute_reply.started":"2022-02-06T15:27:35.284669Z","shell.execute_reply":"2022-02-06T15:27:58.234231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fo.launch_app(coco_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-02-06T15:28:04.948503Z","iopub.execute_input":"2022-02-06T15:28:04.948826Z","iopub.status.idle":"2022-02-06T15:28:05.309961Z","shell.execute_reply.started":"2022-02-06T15:28:04.948795Z","shell.execute_reply":"2022-02-06T15:28:05.309176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import fiftyone.brain as fob\n\nfob.compute_uniqueness(coco_dataset)\nrank_view = coco_dataset.sort_by(\"uniqueness\")\n    ","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-02-06T15:28:26.879486Z","iopub.execute_input":"2022-02-06T15:28:26.879782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"coco_dataset.default_classes","metadata":{"execution":{"iopub.status.busy":"2022-02-06T15:16:33.367185Z","iopub.execute_input":"2022-02-06T15:16:33.367608Z","iopub.status.idle":"2022-02-06T15:16:33.376466Z","shell.execute_reply.started":"2022-02-06T15:16:33.367554Z","shell.execute_reply":"2022-02-06T15:16:33.375248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-02-06T12:20:16.254505Z","iopub.execute_input":"2022-02-06T12:20:16.254805Z","iopub.status.idle":"2022-02-06T12:20:16.264066Z","shell.execute_reply.started":"2022-02-06T12:20:16.254776Z","shell.execute_reply":"2022-02-06T12:20:16.263282Z"},"trusted":true},"execution_count":null,"outputs":[]}]}