{"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":"!pip install pyparsing==2.4.2\nimport numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport plotly.graph_objects as go\nimport seaborn as sns\nsns.set_style('darkgrid')\n\nfrom PIL import Image, ImageDraw\n# import tensorflow as tf\n\nimport os\nimport ast\nimport sys\nimport time\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\nimport greatbarrierreef\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-12-31T19:53:44.924665Z","iopub.execute_input":"2021-12-31T19:53:44.925450Z","iopub.status.idle":"2021-12-31T19:53:54.429219Z","shell.execute_reply.started":"2021-12-31T19:53:44.925395Z","shell.execute_reply":"2021-12-31T19:53:54.428150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The below code will be useful in setting up TF object detection API. Part of it will be in the next notebook.Cheers! You can also refer to (https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html) for details.","metadata":{}},{"cell_type":"code","source":"# data imports\nDATA_PATH = '/kaggle/input/tensorflow-great-barrier-reef'\nimages_path = os.path.join(DATA_PATH,'train_images')\ndf_test = pd.read_csv(\"../input/tensorflow-great-barrier-reef/test.csv\")\ndf_train = pd.read_csv(\"../input/tensorflow-great-barrier-reef/train.csv\")\nsample_submission = pd.read_csv(\"../input/tensorflow-great-barrier-reef/example_sample_submission.csv\")\nexample = np.load(\"../input/tensorflow-great-barrier-reef/example_test.npy\")","metadata":{"execution":{"iopub.status.busy":"2021-12-31T19:53:54.432049Z","iopub.execute_input":"2021-12-31T19:53:54.432608Z","iopub.status.idle":"2021-12-31T19:53:54.521131Z","shell.execute_reply.started":"2021-12-31T19:53:54.432565Z","shell.execute_reply":"2021-12-31T19:53:54.519749Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['img_path'] = os.path.join('../input/tensorflow-great-barrier-reef/train_images')+\"/video_\"+df_train.video_id.astype(str)+\"/\"+df_train.video_frame.astype(str)+\".jpg\"\ndf_train['annotations'] = df_train['annotations'].apply(lambda x: ast.literal_eval(x))\ndf_train['Number_bbox'] = df_train['annotations'].apply(lambda x:len(x)) ","metadata":{"execution":{"iopub.status.busy":"2021-12-31T19:53:54.522942Z","iopub.execute_input":"2021-12-31T19:53:54.523201Z","iopub.status.idle":"2021-12-31T19:53:55.018516Z","shell.execute_reply.started":"2021-12-31T19:53:54.523169Z","shell.execute_reply":"2021-12-31T19:53:55.017451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def bbox_areas(annotations):\n    if not annotations:\n        return [0]\n    area_list = []\n    for annotation in annotations:\n        area_list.append(annotation['width']*annotation['height'])\n    return area_list\ndf_train[\"bbox_area\"] = df_train[\"annotations\"].apply(bbox_areas)\ndf_train[\"max_area\"] = df_train[\"bbox_area\"].apply(lambda x : max(x))\ndf_train[\"min_area\"] = df_train[\"bbox_area\"].apply(lambda x : min(x))\ndf_train.head()\n","metadata":{"execution":{"iopub.status.busy":"2021-12-31T19:53:55.021472Z","iopub.execute_input":"2021-12-31T19:53:55.021790Z","iopub.status.idle":"2021-12-31T19:53:55.105917Z","shell.execute_reply.started":"2021-12-31T19:53:55.021755Z","shell.execute_reply":"2021-12-31T19:53:55.104734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def img_viz(df_train, id):\n    image = df_train['img_path'][id]\n    img = Image.open(image)\n    \n    for box in df_train['annotations'][id]:\n        shape = [box['x'], box['y'], box['x']+box['width'], box['y']+box['height']]\n        ImageDraw.Draw(img).rectangle(shape, outline =\"red\", width=3)\n    display(img)\ndf_train.sort_values(\"max_area\", ascending=False).head()\n","metadata":{"execution":{"iopub.status.busy":"2021-12-31T19:53:55.107517Z","iopub.execute_input":"2021-12-31T19:53:55.107947Z","iopub.status.idle":"2021-12-31T19:53:55.145351Z","shell.execute_reply.started":"2021-12-31T19:53:55.107914Z","shell.execute_reply":"2021-12-31T19:53:55.144584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data imports\nDATA_PATH = '/kaggle/input/tensorflow-great-barrier-reef'\nimages_path = os.path.join(DATA_PATH,'train_images')\ndf_test = pd.read_csv(\"../input/tensorflow-great-barrier-reef/test.csv\")\ndf_train = pd.read_csv(\"../input/tensorflow-great-barrier-reef/train.csv\")\nsample_submission = pd.read_csv(\"../input/tensorflow-great-barrier-reef/example_sample_submission.csv\")\nexample = np.load(\"../input/tensorflow-great-barrier-reef/example_test.npy\")\ndata = pd.read_csv(\"../input/datagreatbarrier/data.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-12-31T19:53:55.146709Z","iopub.execute_input":"2021-12-31T19:53:55.147034Z","iopub.status.idle":"2021-12-31T19:53:55.320405Z","shell.execute_reply.started":"2021-12-31T19:53:55.146992Z","shell.execute_reply":"2021-12-31T19:53:55.319185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2021-12-31T19:53:55.322287Z","iopub.execute_input":"2021-12-31T19:53:55.322714Z","iopub.status.idle":"2021-12-31T19:53:55.351157Z","shell.execute_reply.started":"2021-12-31T19:53:55.322661Z","shell.execute_reply":"2021-12-31T19:53:55.350126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"actual_train_data = data.query(\"Number_bbox>0\")","metadata":{"execution":{"iopub.status.busy":"2021-12-31T19:53:55.352807Z","iopub.execute_input":"2021-12-31T19:53:55.353118Z","iopub.status.idle":"2021-12-31T19:53:55.372647Z","shell.execute_reply.started":"2021-12-31T19:53:55.353083Z","shell.execute_reply":"2021-12-31T19:53:55.371695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/tensorflow/models.git\n# !cd models/research\n# !export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim\n# !protoc object_detection/protos/*.proto --python_out=.","metadata":{"execution":{"iopub.status.busy":"2021-12-31T19:53:55.374195Z","iopub.execute_input":"2021-12-31T19:53:55.374453Z","iopub.status.idle":"2021-12-31T19:53:56.142966Z","shell.execute_reply.started":"2021-12-31T19:53:55.374422Z","shell.execute_reply":"2021-12-31T19:53:56.141984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget -O protobuf.zip https://github.com/protocolbuffers/protobuf/releases/download/v3.19.0/protoc-3.19.0-linux-x86_64.zip -q\n!unzip -o protobuf.zip\n!rm protobuf.zip","metadata":{"execution":{"iopub.status.busy":"2021-12-31T19:53:56.147091Z","iopub.execute_input":"2021-12-31T19:53:56.147792Z","iopub.status.idle":"2021-12-31T19:53:59.264912Z","shell.execute_reply.started":"2021-12-31T19:53:56.147725Z","shell.execute_reply":"2021-12-31T19:53:59.263615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd\n!cd models/research\n!pwd","metadata":{"execution":{"iopub.status.busy":"2021-12-31T19:53:59.267868Z","iopub.execute_input":"2021-12-31T19:53:59.268211Z","iopub.status.idle":"2021-12-31T19:54:01.568900Z","shell.execute_reply.started":"2021-12-31T19:53:59.268178Z","shell.execute_reply":"2021-12-31T19:54:01.567811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %bash cd models/research\nos.chdir('models/research')\n# !pwd\n!protoc object_detection/protos/*.proto --python_out=.","metadata":{"execution":{"iopub.status.busy":"2021-12-31T19:54:01.572186Z","iopub.execute_input":"2021-12-31T19:54:01.572541Z","iopub.status.idle":"2021-12-31T19:54:02.426766Z","shell.execute_reply.started":"2021-12-31T19:54:01.572503Z","shell.execute_reply":"2021-12-31T19:54:02.425200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nos.environ['AUTOGRAPH_VERBOSITY'] = '0'\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'\nos.environ['PYTHONPATH']=os.environ['PYTHONPATH']+':/kaggle/models/research/slim:/kaggle/models/research'\nos.environ['PYTHONPATH']\n\n","metadata":{"execution":{"iopub.status.busy":"2021-12-31T19:54:02.428860Z","iopub.execute_input":"2021-12-31T19:54:02.429307Z","iopub.status.idle":"2021-12-31T19:54:02.439683Z","shell.execute_reply.started":"2021-12-31T19:54:02.429264Z","shell.execute_reply":"2021-12-31T19:54:02.438725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp object_detection/packages/tf2/setup.py .\n!python -m pip install --use-feature=2020-resolver .","metadata":{"execution":{"iopub.status.busy":"2021-12-31T19:54:02.441545Z","iopub.execute_input":"2021-12-31T19:54:02.441868Z","iopub.status.idle":"2021-12-31T19:54:23.171619Z","shell.execute_reply.started":"2021-12-31T19:54:02.441821Z","shell.execute_reply":"2021-12-31T19:54:23.170432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd\n!python object_detection/builders/model_builder_tf2_test.py","metadata":{"execution":{"iopub.status.busy":"2021-12-31T19:54:23.174061Z","iopub.execute_input":"2021-12-31T19:54:23.174474Z","iopub.status.idle":"2021-12-31T19:55:14.164911Z","shell.execute_reply.started":"2021-12-31T19:54:23.174421Z","shell.execute_reply":"2021-12-31T19:55:14.163850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['annotations'] = data['annotations'].apply(eval)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-31T20:39:14.413705Z","iopub.execute_input":"2021-12-31T20:39:14.414823Z","iopub.status.idle":"2021-12-31T20:39:14.498871Z","shell.execute_reply.started":"2021-12-31T20:39:14.414758Z","shell.execute_reply":"2021-12-31T20:39:14.497744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.chdir(\"/kaggle/working\")\n!mkdir workspace workspace/train_images workspace/test_images","metadata":{"execution":{"iopub.status.busy":"2021-12-31T20:39:23.086804Z","iopub.execute_input":"2021-12-31T20:39:23.087178Z","iopub.status.idle":"2021-12-31T20:39:23.856101Z","shell.execute_reply.started":"2021-12-31T20:39:23.087141Z","shell.execute_reply":"2021-12-31T20:39:23.854734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_map = \"\"\"item {\n    id: 1\n    name: 'starfish'\n}\"\"\"\nwith open(\"/kaggle/working/workspace/annotations\", \"w\") as label_file:\n    label_file.write(label_map)\nlabel_file.close()\n    ","metadata":{"execution":{"iopub.status.busy":"2021-12-31T20:39:24.646647Z","iopub.execute_input":"2021-12-31T20:39:24.647372Z","iopub.status.idle":"2021-12-31T20:39:24.653198Z","shell.execute_reply.started":"2021-12-31T20:39:24.647323Z","shell.execute_reply":"2021-12-31T20:39:24.652538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !cp /kaggle/input/reef-labels/labelmap.pbtxt /kaggle/working/workspace/annotations","metadata":{"execution":{"iopub.status.busy":"2021-12-31T20:39:26.789080Z","iopub.execute_input":"2021-12-31T20:39:26.789914Z","iopub.status.idle":"2021-12-31T20:39:26.795081Z","shell.execute_reply.started":"2021-12-31T20:39:26.789870Z","shell.execute_reply":"2021-12-31T20:39:26.793957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.notebook import tqdm\nimport matplotlib.pyplot as plt\nfrom PIL import ImageDraw\nfrom PIL import Image\nimport pandas as pd\nimport numpy as np\nimport json\nimport copy\nimport os\nimport cv2\nimport ast\n\n# functions\n","metadata":{"execution":{"iopub.status.busy":"2021-12-31T20:39:27.501355Z","iopub.execute_input":"2021-12-31T20:39:27.502113Z","iopub.status.idle":"2021-12-31T20:39:27.507476Z","shell.execute_reply.started":"2021-12-31T20:39:27.502070Z","shell.execute_reply":"2021-12-31T20:39:27.506792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_annotation_dict = dict(zip(data.img_path, data.annotations))\nannotated_images = {key:value for key, value in image_annotation_dict.items() if len(value)>0}","metadata":{"execution":{"iopub.status.busy":"2021-12-31T20:39:28.652915Z","iopub.execute_input":"2021-12-31T20:39:28.653643Z","iopub.status.idle":"2021-12-31T20:39:28.678141Z","shell.execute_reply.started":"2021-12-31T20:39:28.653599Z","shell.execute_reply":"2021-12-31T20:39:28.677212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_xml_template(image_path, folder=\"/kaggle/working/workspace/train_images\"):\n    img = Image.open(image_path)\n    img_width, img_height = img.size\n    file_name = '_'.join(image_path.split('/')[-2:])\n    image_info_dict = {\n    \"folder\": folder.split('/')[-1],\n    \"filename\": file_name,\n    \"path\": folder + '/' + file_name,\n    \"source\":{\"database\": \"Unknown\"},\n    \"size\":{\"width\": str(img_width), \"height\": str(img_height), \"depth\":\"3\"},\n    \"segmented\": \"0\",\n        }\n    return image_info_dict\n\ndef get_annotation_template(annotation):\n    annotation_dict = {\"name\": \"starfish\",\n                      \"pose\": \"unknown\",\n                      \"difficult\": \"0\",\n                      \"bbox\":{\"xmin\": str(annotation['x']),\n                              \"xmax\": str(annotation['x']+annotation['width']),\n                              \"ymin\": str(annotation['y']),\n                               \"ymax\": str(annotation['y']+annotation['height'])}}\n    return annotation_dict","metadata":{"execution":{"iopub.status.busy":"2021-12-31T20:39:34.940097Z","iopub.execute_input":"2021-12-31T20:39:34.940718Z","iopub.status.idle":"2021-12-31T20:39:34.951445Z","shell.execute_reply.started":"2021-12-31T20:39:34.940682Z","shell.execute_reply":"2021-12-31T20:39:34.950645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import xml.etree.ElementTree as ET\n\ndef add_child(key, value, parent):\n    child = ET.SubElement(parent, key)\n    if isinstance(value, dict):\n        for key_child, value_child in list(value.items()):\n            add_child(key_child, value_child, child)\n    else:\n        child.text = value\n    return  \n\ndef get_xml(image_data,folder=\"/kaggle/working/workspace/train_images\"):\n    image_info = get_xml_template(image_data[0], folder=folder)\n    file_path = folder+'/'+image_info[\"filename\"]\n    xml_file_path = file_path.split('.')[0] +'.xml'\n    annotation_list = image_data[1]\n    root = ET.Element(\"annotations\")\n    for k,v in image_info.items():\n        add_child(k,v,root)\n    for annotation in annotation_list:\n        annotation_root = ET.SubElement(root, \"object\")\n        annotation_info = get_annotation_template(annotation)\n        for k,v in annotation_info.items():\n            add_child(k,v,annotation_root)\n    return root,file_path,xml_file_path\n\n","metadata":{"execution":{"iopub.status.busy":"2021-12-31T20:39:35.480393Z","iopub.execute_input":"2021-12-31T20:39:35.481000Z","iopub.status.idle":"2021-12-31T20:39:35.489986Z","shell.execute_reply.started":"2021-12-31T20:39:35.480961Z","shell.execute_reply":"2021-12-31T20:39:35.489290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nimport shutil\ntrain_data, test_data = train_test_split(data,random_state=22, test_size=0.1)","metadata":{"execution":{"iopub.status.busy":"2021-12-31T20:39:35.950444Z","iopub.execute_input":"2021-12-31T20:39:35.951035Z","iopub.status.idle":"2021-12-31T20:39:35.967710Z","shell.execute_reply.started":"2021-12-31T20:39:35.950997Z","shell.execute_reply":"2021-12-31T20:39:35.965974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def transfer_images_xml(image_data_list, folder=\"/kaggle/working/workspace/train_images\"):\n    for image_data in tqdm(image_data_list):\n        xml_details, image_path, xml_path = get_xml(image_data, folder=folder)\n        shutil.copy(image_data[0], image_path)\n        xml_tree = ET.ElementTree(xml_details)\n        xml_tree.write(xml_path)","metadata":{"execution":{"iopub.status.busy":"2021-12-31T20:39:36.702769Z","iopub.execute_input":"2021-12-31T20:39:36.703083Z","iopub.status.idle":"2021-12-31T20:39:36.709711Z","shell.execute_reply.started":"2021-12-31T20:39:36.703053Z","shell.execute_reply":"2021-12-31T20:39:36.708857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_annotation_dict = tuple(zip(test_data.img_path, test_data.annotations))\n","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-12-31T20:39:37.523224Z","iopub.execute_input":"2021-12-31T20:39:37.524407Z","iopub.status.idle":"2021-12-31T20:39:37.531972Z","shell.execute_reply.started":"2021-12-31T20:39:37.524353Z","shell.execute_reply":"2021-12-31T20:39:37.531014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transfer_images_xml(test_image_annotation_dict, folder=\"/kaggle/working/workspace/test_images\")","metadata":{"execution":{"iopub.status.busy":"2021-12-31T20:41:06.432350Z","iopub.execute_input":"2021-12-31T20:41:06.432900Z","iopub.status.idle":"2021-12-31T20:41:13.174982Z","shell.execute_reply.started":"2021-12-31T20:41:06.432848Z","shell.execute_reply":"2021-12-31T20:41:13.173909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !rm /kaggle/working/workspace/test_images/*\n# !rm /kaggle/working/workspace/train_images/*\n","metadata":{"execution":{"iopub.status.busy":"2021-12-31T20:40:56.559427Z","iopub.execute_input":"2021-12-31T20:40:56.559816Z","iopub.status.idle":"2021-12-31T20:40:57.823656Z","shell.execute_reply.started":"2021-12-31T20:40:56.559779Z","shell.execute_reply":"2021-12-31T20:40:57.822217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}