{"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 is a very basic exercise on how to log images and see the bounding box using WandB.\nHow it simplifies a lot of stuff for us in vizualization.\n\nI followed the tutorial here:\nhttps://wandb.ai/stacey/yolo-drive/reports/Bounding-Boxes-for-Object-Detection--Vmlldzo4Nzg4MQ\n\nAlso find this(https://www.kaggle.com/c/tensorflow-great-barrier-reef/discussion/290062) amazing discussion thread by https://www.kaggle.com/mpwolke to understand why this competition has a huge impact on environment.\n\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-11-24T07:44:50.603265Z","iopub.execute_input":"2021-11-24T07:44:50.603619Z","iopub.status.idle":"2021-11-24T07:44:50.61354Z","shell.execute_reply.started":"2021-11-24T07:44:50.603552Z","shell.execute_reply":"2021-11-24T07:44:50.612398Z"}}},{"cell_type":"markdown","source":"<h1>Basic Imports","metadata":{}},{"cell_type":"code","source":"import os\nimport ast\nimport pandas as pd\nimport numpy as np\nimport wandb\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:58:59.922594Z","iopub.execute_input":"2021-11-26T02:58:59.922916Z","iopub.status.idle":"2021-11-26T02:59:00.648144Z","shell.execute_reply.started":"2021-11-26T02:58:59.922887Z","shell.execute_reply":"2021-11-26T02:59:00.647097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from kaggle_secrets import UserSecretsClient\nuser_secrets = UserSecretsClient()\nsecret_value_0 = user_secrets.get_secret(\"wb_api\")\nwandb.login(key=secret_value_0)","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:59:02.532210Z","iopub.execute_input":"2021-11-26T02:59:02.532487Z","iopub.status.idle":"2021-11-26T02:59:03.527427Z","shell.execute_reply.started":"2021-11-26T02:59:02.532459Z","shell.execute_reply":"2021-11-26T02:59:03.526468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1>Reading CSV files","metadata":{}},{"cell_type":"code","source":"train_csv = pd.read_csv(\"../input/tensorflow-great-barrier-reef/train.csv\")\ntest_csv = pd.read_csv(\"../input/tensorflow-great-barrier-reef/test.csv\")\ntrain_csv.head(20)","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:59:18.488024Z","iopub.execute_input":"2021-11-26T02:59:18.488892Z","iopub.status.idle":"2021-11-26T02:59:18.587889Z","shell.execute_reply.started":"2021-11-26T02:59:18.488839Z","shell.execute_reply":"2021-11-26T02:59:18.587036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1> Initializing WandB","metadata":{}},{"cell_type":"code","source":"run = wandb.init(project=\"visualization4\", name=\"coral-reef\")","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:59:27.567770Z","iopub.execute_input":"2021-11-26T02:59:27.568056Z","iopub.status.idle":"2021-11-26T02:59:34.347016Z","shell.execute_reply.started":"2021-11-26T02:59:27.568024Z","shell.execute_reply":"2021-11-26T02:59:34.346305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking the number of frames in each video/video_id\ntrain_group = train_csv.groupby('video_id').count()['image_id']\ntrain_group","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:59:34.349042Z","iopub.execute_input":"2021-11-26T02:59:34.351178Z","iopub.status.idle":"2021-11-26T02:59:34.379563Z","shell.execute_reply.started":"2021-11-26T02:59:34.349491Z","shell.execute_reply":"2021-11-26T02:59:34.378772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = []\nfor i in train_group.keys():\n    data.append([i, train_group[i]])\n    \ndata","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:59:36.696940Z","iopub.execute_input":"2021-11-26T02:59:36.697234Z","iopub.status.idle":"2021-11-26T02:59:36.707062Z","shell.execute_reply.started":"2021-11-26T02:59:36.697203Z","shell.execute_reply":"2021-11-26T02:59:36.706071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1> Bar graph to vizualize no of frames in each video","metadata":{}},{"cell_type":"code","source":"table = wandb.Table(data=data, columns = [\"video_id\", \"No of Frames\"])\nwandb.log({\"my_bar_chart_id\" : wandb.plot.bar(table, \"video_id\",\n                               \"No of Frames\", title=\"Custom Bar Chart\")})","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:59:39.296970Z","iopub.execute_input":"2021-11-26T02:59:39.297259Z","iopub.status.idle":"2021-11-26T02:59:39.575457Z","shell.execute_reply.started":"2021-11-26T02:59:39.297230Z","shell.execute_reply":"2021-11-26T02:59:39.574607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# this is the order in which my classes will be displayed\ndisplay_ids = {\"coral\" : 1}\n# this is a revese map of the integer class id to the string class label\nclass_id_to_label = { int(v) : k for k, v in display_ids.items()}","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:59:40.746803Z","iopub.execute_input":"2021-11-26T02:59:40.747097Z","iopub.status.idle":"2021-11-26T02:59:40.753363Z","shell.execute_reply.started":"2021-11-26T02:59:40.747065Z","shell.execute_reply":"2021-11-26T02:59:40.752062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_id_to_label","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:59:42.746740Z","iopub.execute_input":"2021-11-26T02:59:42.747639Z","iopub.status.idle":"2021-11-26T02:59:42.753504Z","shell.execute_reply.started":"2021-11-26T02:59:42.747598Z","shell.execute_reply":"2021-11-26T02:59:42.752946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1> Defining the image reading function","metadata":{}},{"cell_type":"code","source":"def bounding_boxes(filename, v_boxes):\n    '''\n    inputs: \n        filename: path to input image\n        v_boxes: list of bounding boxes(dict) in format (x, y, width height)\n    outputs:\n        box_image: wandb image\n    '''\n    all_boxes = []\n    image = Image.open(filename)\n    for b_i, box in enumerate(v_boxes):\n        # get coordinates and labels\n        box_data = {\"position\" : {\n          \"minX\" : box['x'], # x1\n          \"maxX\" : box['x'] + box['width'], #x2\n          \"minY\" : box['y'], # y1\n          \"maxY\" : box['y'] + box['height']}, #y2\n          \"class_id\" : 1, #Defining the label as 1, and rest of the background is 0.\n          # optionally caption each box with its class and score\n          \"box_caption\" : \"%s\" % (\"coral\"),\n          \"domain\" : \"pixel\",\n          \"scores\" : { \"score\" : 1}}\n        all_boxes.append(box_data) #List of all boxes on a single image in a list\n\n    # log to wandb: raw image, predictions, and dictionary of class labels for each class id\n    box_image = wandb.Image(image, boxes = {\"gt\": {\"box_data\": all_boxes, \"class_labels\" : class_id_to_label}}, classes = [{\"id\": 0, \"name\": \"none\"}, {\"id\": 1, \"name\": \"coral\"}])\n    return box_image","metadata":{"execution":{"iopub.status.busy":"2021-11-26T02:59:44.398509Z","iopub.execute_input":"2021-11-26T02:59:44.399407Z","iopub.status.idle":"2021-11-26T02:59:44.409952Z","shell.execute_reply.started":"2021-11-26T02:59:44.399361Z","shell.execute_reply":"2021-11-26T02:59:44.409214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_data = []\n#Path to base folder\nbase_folder_path = '../input/tensorflow-great-barrier-reef/train_images/video_0'\nimage_list = sorted(os.listdir(base_folder_path))\n# Considering first 50 images for display\nfor i in range(50):\n    bounding_box_dict = train_csv.loc[i].annotations\n    bounding_box_d = ast.literal_eval(bounding_box_dict)\n    if len(bounding_box_d)>0:\n        if train_csv.loc[i].image_id.split('-')[-1]+'.jpg' in image_list:\n            image_name = train_csv.loc[i].image_id.split('-')[-1]+'.jpg'\n            sample_image_path = os.path.join(base_folder_path, image_name)\n            wandb_image = bounding_boxes(sample_image_path, bounding_box_d)\n            my_data.append([image_name, wandb_image])\n        \ncolumns= [\"image_no\", \"image\"]\ntest_table = wandb.Table(data=my_data, columns=columns)\nrun.log({\"coral_reef_boxes\": test_table})\nrun.finish()","metadata":{"execution":{"iopub.status.busy":"2021-11-26T03:01:32.937078Z","iopub.execute_input":"2021-11-26T03:01:32.937872Z","iopub.status.idle":"2021-11-26T03:01:46.760663Z","shell.execute_reply.started":"2021-11-26T03:01:32.937828Z","shell.execute_reply":"2021-11-26T03:01:46.759706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bounding_box_count = []\nfor row_index, row in train_csv.iterrows():\n    bounding_box_count.append(len(ast.literal_eval(row.annotations)))\n\nmin_boxes = min(bounding_box_count)\nmax_boxes = max(bounding_box_count)\naverage_boxes = np.mean(bounding_box_count)\n\n","metadata":{"execution":{"iopub.status.busy":"2021-11-26T03:00:00.120893Z","iopub.execute_input":"2021-11-26T03:00:00.121132Z","iopub.status.idle":"2021-11-26T03:00:02.282416Z","shell.execute_reply.started":"2021-11-26T03:00:00.121106Z","shell.execute_reply":"2021-11-26T03:00:02.281509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"The minimum number of boxes in any training frame is {}\".format(min_boxes))\nprint(\"The maximum number of boxes in any training frame is {}\".format(max_boxes))\nprint(\"The average number of boxes in any training frame is {}\".format(average_boxes))","metadata":{"execution":{"iopub.status.busy":"2021-11-26T03:00:02.283627Z","iopub.execute_input":"2021-11-26T03:00:02.283910Z","iopub.status.idle":"2021-11-26T03:00:02.292613Z","shell.execute_reply.started":"2021-11-26T03:00:02.283878Z","shell.execute_reply":"2021-11-26T03:00:02.291760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}