{"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.0\n!pip install tensorflow==2.6.0\n!pip install keras==2.6.0\nimport os\n# os.chdir('/kaggle/working/w')\n# import shutil\n# shutil.rmtree(\"/kaggle/working/workspace\")\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\nimport tensorflow as tf\n\n\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":"2022-01-09T05:51:14.915800Z","iopub.execute_input":"2022-01-09T05:51:14.916140Z","iopub.status.idle":"2022-01-09T05:52:47.922803Z","shell.execute_reply.started":"2022-01-09T05:51:14.916033Z","shell.execute_reply":"2022-01-09T05:52:47.918493Z"},"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(\"/kaggle/input/tensorflow-great-barrier-reef/test.csv\")\ndf_train = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/train.csv\")\nsample_submission = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/example_sample_submission.csv\")\nexample = np.load(\"/kaggle/input/tensorflow-great-barrier-reef/example_test.npy\")","metadata":{"execution":{"iopub.status.busy":"2022-01-09T05:52:47.930007Z","iopub.execute_input":"2022-01-09T05:52:47.936063Z","iopub.status.idle":"2022-01-09T05:52:48.404912Z","shell.execute_reply.started":"2022-01-09T05:52:47.935988Z","shell.execute_reply":"2022-01-09T05:52:48.403913Z"},"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":"2022-01-09T05:52:48.407674Z","iopub.execute_input":"2022-01-09T05:52:48.408105Z","iopub.status.idle":"2022-01-09T05:52:48.837885Z","shell.execute_reply.started":"2022-01-09T05:52:48.408020Z","shell.execute_reply":"2022-01-09T05:52:48.837000Z"},"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))\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-09T05:52:48.840244Z","iopub.execute_input":"2022-01-09T05:52:48.840572Z","iopub.status.idle":"2022-01-09T05:52:49.229222Z","shell.execute_reply.started":"2022-01-09T05:52:48.840532Z","shell.execute_reply":"2022-01-09T05:52:49.228389Z"},"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":"2022-01-09T05:52:49.230604Z","iopub.execute_input":"2022-01-09T05:52:49.231491Z","iopub.status.idle":"2022-01-09T05:52:49.274429Z","shell.execute_reply.started":"2022-01-09T05:52:49.231445Z","shell.execute_reply":"2022-01-09T05:52:49.273359Z"},"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(\"/kaggle/input/tensorflow-great-barrier-reef/test.csv\")\ndf_train = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/train.csv\")\nsample_submission = pd.read_csv(\"/kaggle/input/tensorflow-great-barrier-reef/example_sample_submission.csv\")\nexample = np.load(\"/kaggle/input/tensorflow-great-barrier-reef/example_test.npy\")\ndata = pd.read_csv(\"/kaggle/input/datagreatbarrier/data.csv\") #processed df","metadata":{"execution":{"iopub.status.busy":"2022-01-09T05:52:49.275987Z","iopub.execute_input":"2022-01-09T05:52:49.276319Z","iopub.status.idle":"2022-01-09T05:52:49.515342Z","shell.execute_reply.started":"2022-01-09T05:52:49.276265Z","shell.execute_reply":"2022-01-09T05:52:49.514487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2022-01-09T05:52:49.516572Z","iopub.execute_input":"2022-01-09T05:52:49.517352Z","iopub.status.idle":"2022-01-09T05:52:49.537583Z","shell.execute_reply.started":"2022-01-09T05:52:49.517290Z","shell.execute_reply":"2022-01-09T05:52:49.536986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"actual_train_data = data.query(\"Number_bbox>0\")","metadata":{"execution":{"iopub.status.busy":"2022-01-09T05:52:49.539013Z","iopub.execute_input":"2022-01-09T05:52:49.539468Z","iopub.status.idle":"2022-01-09T05:52:49.561177Z","shell.execute_reply.started":"2022-01-09T05:52:49.539437Z","shell.execute_reply":"2022-01-09T05:52:49.560072Z"},"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":"2022-01-09T05:52:49.562437Z","iopub.execute_input":"2022-01-09T05:52:49.562658Z","iopub.status.idle":"2022-01-09T05:53:13.677938Z","shell.execute_reply.started":"2022-01-09T05:52:49.562632Z","shell.execute_reply":"2022-01-09T05:53:13.677196Z"},"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":"2022-01-09T05:53:13.681592Z","iopub.execute_input":"2022-01-09T05:53:13.681854Z","iopub.status.idle":"2022-01-09T05:53:17.049468Z","shell.execute_reply.started":"2022-01-09T05:53:13.681820Z","shell.execute_reply":"2022-01-09T05:53:17.048149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd\n!cd models/research\n!pwd","metadata":{"execution":{"iopub.status.busy":"2022-01-09T05:53:17.051516Z","iopub.execute_input":"2022-01-09T05:53:17.052192Z","iopub.status.idle":"2022-01-09T05:53:19.457975Z","shell.execute_reply.started":"2022-01-09T05:53:17.052139Z","shell.execute_reply":"2022-01-09T05:53:19.456851Z"},"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":"2022-01-09T05:53:19.460357Z","iopub.execute_input":"2022-01-09T05:53:19.460709Z","iopub.status.idle":"2022-01-09T05:53:20.346870Z","shell.execute_reply.started":"2022-01-09T05:53:19.460664Z","shell.execute_reply":"2022-01-09T05:53:20.345796Z"},"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":"2022-01-09T05:53:20.349267Z","iopub.execute_input":"2022-01-09T05:53:20.349657Z","iopub.status.idle":"2022-01-09T05:53:20.356489Z","shell.execute_reply.started":"2022-01-09T05:53:20.349612Z","shell.execute_reply":"2022-01-09T05:53:20.355762Z"},"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":"2022-01-09T05:53:20.358007Z","iopub.execute_input":"2022-01-09T05:53:20.358533Z","iopub.status.idle":"2022-01-09T05:55:08.661522Z","shell.execute_reply.started":"2022-01-09T05:53:20.358502Z","shell.execute_reply":"2022-01-09T05:55:08.660217Z"},"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":"2022-01-09T05:55:08.663531Z","iopub.execute_input":"2022-01-09T05:55:08.663812Z","iopub.status.idle":"2022-01-09T05:55:51.576616Z","shell.execute_reply.started":"2022-01-09T05:55:08.663777Z","shell.execute_reply":"2022-01-09T05:55:51.575594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['annotations'] = data['annotations'].apply(eval)\n","metadata":{"execution":{"iopub.status.busy":"2022-01-09T05:55:51.578190Z","iopub.execute_input":"2022-01-09T05:55:51.578461Z","iopub.status.idle":"2022-01-09T05:55:51.783700Z","shell.execute_reply.started":"2022-01-09T05:55:51.578427Z","shell.execute_reply":"2022-01-09T05:55:51.782924Z"},"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":"2022-01-09T05:55:51.784725Z","iopub.execute_input":"2022-01-09T05:55:51.784934Z","iopub.status.idle":"2022-01-09T05:55:52.592559Z","shell.execute_reply.started":"2022-01-09T05:55:51.784910Z","shell.execute_reply":"2022-01-09T05:55:52.591624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! mkdir /kaggle/working/workspace/annotations\nlabel_map = \"\"\"item {\n    id: 1\n    name: 'starfish'\n}\"\"\"\nwith open(\"/kaggle/working/workspace/annotations/label_map.pbtxt\", \"w\") as label_file:\n    label_file.write(label_map)\nlabel_file.close()\n    ","metadata":{"execution":{"iopub.status.busy":"2022-01-09T05:55:52.593906Z","iopub.execute_input":"2022-01-09T05:55:52.594573Z","iopub.status.idle":"2022-01-09T05:55:53.393708Z","shell.execute_reply.started":"2022-01-09T05:55:52.594538Z","shell.execute_reply":"2022-01-09T05:55:53.392570Z"},"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":"2022-01-09T05:55:53.395265Z","iopub.execute_input":"2022-01-09T05:55:53.395559Z","iopub.status.idle":"2022-01-09T05:55:53.399963Z","shell.execute_reply.started":"2022-01-09T05:55:53.395525Z","shell.execute_reply":"2022-01-09T05:55:53.398931Z"},"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":"2022-01-09T05:55:53.401256Z","iopub.execute_input":"2022-01-09T05:55:53.401921Z","iopub.status.idle":"2022-01-09T05:55:53.595469Z","shell.execute_reply.started":"2022-01-09T05:55:53.401888Z","shell.execute_reply":"2022-01-09T05:55:53.594690Z"},"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":"2022-01-09T05:55:53.596755Z","iopub.execute_input":"2022-01-09T05:55:53.597181Z","iopub.status.idle":"2022-01-09T05:55:53.618265Z","shell.execute_reply.started":"2022-01-09T05:55:53.597139Z","shell.execute_reply":"2022-01-09T05:55:53.617465Z"},"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                      \"bndbox\":{\"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":"2022-01-09T05:55:53.620252Z","iopub.execute_input":"2022-01-09T05:55:53.620740Z","iopub.status.idle":"2022-01-09T05:55:53.631820Z","shell.execute_reply.started":"2022-01-09T05:55:53.620698Z","shell.execute_reply":"2022-01-09T05:55:53.631219Z"},"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":"2022-01-09T05:55:53.633821Z","iopub.execute_input":"2022-01-09T05:55:53.634164Z","iopub.status.idle":"2022-01-09T05:55:53.643700Z","shell.execute_reply.started":"2022-01-09T05:55:53.634121Z","shell.execute_reply":"2022-01-09T05:55:53.642942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.columns","metadata":{"execution":{"iopub.status.busy":"2022-01-09T05:59:30.919605Z","iopub.execute_input":"2022-01-09T05:59:30.920741Z","iopub.status.idle":"2022-01-09T05:59:30.928320Z","shell.execute_reply.started":"2022-01-09T05:59:30.920680Z","shell.execute_reply":"2022-01-09T05:59:30.927235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"actual_train_data = data[data['Number_bbox']>0]","metadata":{"execution":{"iopub.status.busy":"2022-01-09T05:59:43.161953Z","iopub.execute_input":"2022-01-09T05:59:43.162402Z","iopub.status.idle":"2022-01-09T05:59:43.170440Z","shell.execute_reply.started":"2022-01-09T05:59:43.162368Z","shell.execute_reply":"2022-01-09T05:59:43.169781Z"},"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(actual_train_data,random_state=22, test_size=0.15)","metadata":{"execution":{"iopub.status.busy":"2022-01-09T05:59:50.065494Z","iopub.execute_input":"2022-01-09T05:59:50.066105Z","iopub.status.idle":"2022-01-09T05:59:50.074872Z","shell.execute_reply.started":"2022-01-09T05:59:50.066046Z","shell.execute_reply":"2022-01-09T05:59:50.074115Z"},"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":"2022-01-09T05:59:53.361277Z","iopub.execute_input":"2022-01-09T05:59:53.361732Z","iopub.status.idle":"2022-01-09T05:59:53.370571Z","shell.execute_reply.started":"2022-01-09T05:59:53.361700Z","shell.execute_reply":"2022-01-09T05:59:53.369655Z"},"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":"2022-01-09T05:59:57.132455Z","iopub.execute_input":"2022-01-09T05:59:57.132951Z","iopub.status.idle":"2022-01-09T05:59:57.139372Z","shell.execute_reply.started":"2022-01-09T05:59:57.132914Z","shell.execute_reply":"2022-01-09T05:59:57.138368Z"},"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":"2022-01-09T06:00:08.213658Z","iopub.execute_input":"2022-01-09T06:00:08.214112Z","iopub.status.idle":"2022-01-09T06:00:19.193880Z","shell.execute_reply.started":"2022-01-09T06:00:08.214081Z","shell.execute_reply":"2022-01-09T06:00:19.193040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Command to remove moved images","metadata":{}},{"cell_type":"code","source":"# !rm /kaggle/working/workspace/test_images/*\n# !rm /kaggle/working/workspace/train_images/*\n\n","metadata":{"execution":{"iopub.status.busy":"2022-01-09T06:00:19.195702Z","iopub.execute_input":"2022-01-09T06:00:19.196012Z","iopub.status.idle":"2022-01-09T06:00:19.200243Z","shell.execute_reply.started":"2022-01-09T06:00:19.195972Z","shell.execute_reply":"2022-01-09T06:00:19.199432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir /kaggle/working/workspace/scripts\n!wget https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/_downloads/da4babe668a8afb093cc7776d7e630f3/generate_tfrecord.py -P /kaggle/working/workspace/scripts","metadata":{"execution":{"iopub.status.busy":"2022-01-09T06:00:19.201593Z","iopub.execute_input":"2022-01-09T06:00:19.201876Z","iopub.status.idle":"2022-01-09T06:00:21.672457Z","shell.execute_reply.started":"2022-01-09T06:00:19.201840Z","shell.execute_reply":"2022-01-09T06:00:21.671388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python /kaggle/working/workspace/scripts/generate_tfrecord.py -x /kaggle/working/workspace/test_images -l /kaggle/working/workspace/annotations/label_map.pbtxt -o /kaggle/working/workspace/annotations/test.record","metadata":{"execution":{"iopub.status.busy":"2022-01-09T06:00:21.674984Z","iopub.execute_input":"2022-01-09T06:00:21.675237Z","iopub.status.idle":"2022-01-09T06:00:28.930165Z","shell.execute_reply.started":"2022-01-09T06:00:21.675204Z","shell.execute_reply":"2022-01-09T06:00:28.929449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_annotation_dict = tuple(zip(train_data.img_path, train_data.annotations))\ntransfer_images_xml(train_image_annotation_dict, folder=\"/kaggle/working/workspace/train_images\")\n","metadata":{"execution":{"iopub.status.busy":"2022-01-09T06:00:28.931533Z","iopub.execute_input":"2022-01-09T06:00:28.931775Z","iopub.status.idle":"2022-01-09T06:01:18.337201Z","shell.execute_reply.started":"2022-01-09T06:00:28.931746Z","shell.execute_reply":"2022-01-09T06:01:18.336360Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python /kaggle/working/workspace/scripts/generate_tfrecord.py -x /kaggle/working/workspace/train_images -l /kaggle/working/workspace/annotations/label_map.pbtxt -o /kaggle/working/workspace/annotations/train.record","metadata":{"execution":{"iopub.status.busy":"2022-01-09T06:01:18.338989Z","iopub.execute_input":"2022-01-09T06:01:18.339556Z","iopub.status.idle":"2022-01-09T06:01:59.235617Z","shell.execute_reply.started":"2022-01-09T06:01:18.339511Z","shell.execute_reply":"2022-01-09T06:01:59.234518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8.tar.gz -P /kaggle/working/workspace/pre-trained-models/\n!tar -xvzf /kaggle/working/workspace/pre-trained-models/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8.tar.gz\n","metadata":{"execution":{"iopub.status.busy":"2022-01-09T06:01:59.237735Z","iopub.execute_input":"2022-01-09T06:01:59.238009Z","iopub.status.idle":"2022-01-09T06:02:06.243731Z","shell.execute_reply.started":"2022-01-09T06:01:59.237979Z","shell.execute_reply":"2022-01-09T06:02:06.242918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"config = \"\"\"# Faster R-CNN with Resnet-50 (v1)\n# Trained on COCO, initialized from Imagenet classification checkpoint\n\n# Achieves -- mAP on COCO14 minival dataset.\n\n# This config is TPU compatible.\n\nmodel {\n  faster_rcnn {\n    num_classes: 1\n    image_resizer {\n      keep_aspect_ratio_resizer {\n        min_dimension: 640\n        max_dimension: 640\n        pad_to_max_dimension: true\n      }\n    }\n    feature_extractor {\n      type: 'faster_rcnn_resnet50_keras'\n      batch_norm_trainable: true\n    }\n    first_stage_anchor_generator {\n      grid_anchor_generator {\n        scales: [0.25, 0.5, 1.0, 2.0]\n        aspect_ratios: [0.5, 1.0, 2.0]\n        height_stride: 16\n        width_stride: 16\n      }\n    }\n    first_stage_box_predictor_conv_hyperparams {\n      op: CONV\n      regularizer {\n        l2_regularizer {\n          weight: 0.0\n        }\n      }\n      initializer {\n        truncated_normal_initializer {\n          stddev: 0.01\n        }\n      }\n    }\n    first_stage_nms_score_threshold: 0.0\n    first_stage_nms_iou_threshold: 0.7\n    first_stage_max_proposals: 300\n    first_stage_localization_loss_weight: 2.0\n    first_stage_objectness_loss_weight: 1.0\n    initial_crop_size: 14\n    maxpool_kernel_size: 2\n    maxpool_stride: 2\n    second_stage_box_predictor {\n      mask_rcnn_box_predictor {\n        use_dropout: false\n        dropout_keep_probability: 1.0\n        fc_hyperparams {\n          op: FC\n          regularizer {\n            l2_regularizer {\n              weight: 0.0\n            }\n          }\n          initializer {\n            variance_scaling_initializer {\n              factor: 1.0\n              uniform: true\n              mode: FAN_AVG\n            }\n          }\n        }\n        share_box_across_classes: true\n      }\n    }\n    second_stage_post_processing {\n      batch_non_max_suppression {\n        score_threshold: 0.0\n        iou_threshold: 0.6\n        max_detections_per_class: 100\n        max_total_detections: 300\n      }\n      score_converter: SOFTMAX\n    }\n    second_stage_localization_loss_weight: 2.0\n    second_stage_classification_loss_weight: 1.0\n    use_static_shapes: true\n    use_matmul_crop_and_resize: true\n    clip_anchors_to_image: true\n    use_static_balanced_label_sampler: true\n    use_matmul_gather_in_matcher: true\n  }\n}\n\ntrain_config: {\n  batch_size: 64\n  sync_replicas: true\n  startup_delay_steps: 0\n  replicas_to_aggregate: 8\n  num_steps: 25000\n  optimizer {\n    momentum_optimizer: {\n      learning_rate: {\n        cosine_decay_learning_rate {\n          learning_rate_base: .04\n          total_steps: 25000\n          warmup_learning_rate: .013333\n          warmup_steps: 2000\n        }\n      }\n      momentum_optimizer_value: 0.9\n    }\n    use_moving_average: false\n  }\n  fine_tune_checkpoint_version: V2\n  fine_tune_checkpoint: \"/kaggle/working/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8/checkpoint/ckpt-0\"\n  fine_tune_checkpoint_type: \"classification\"\n  data_augmentation_options {\n    random_horizontal_flip {\n    }\n  }\n\n  max_number_of_boxes: 100\n  unpad_groundtruth_tensors: false\n  use_bfloat16: true  # works only on TPUs\n}\n\ntrain_input_reader: {\n  label_map_path: \"/kaggle/working/workspace/annotations/label_map.pbtxt\"\n  tf_record_input_reader {\n    input_path: \"/kaggle/working/workspace/annotations/train.record\"\n  }\n}\n\neval_config: {\n  metrics_set: \"coco_detection_metrics\"\n  use_moving_averages: false\n  batch_size: 1;\n}\n\neval_input_reader: {\n  label_map_path: \"/kaggle/working/workspace/annotations/label_map.pbtxt\"\n  shuffle: false\n  num_epochs: 1\n  tf_record_input_reader {\n    input_path: \"/kaggle/working/workspace/annotations/test.record\"\n  }\n}\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-01-09T06:02:06.245898Z","iopub.execute_input":"2022-01-09T06:02:06.246178Z","iopub.status.idle":"2022-01-09T06:02:06.252124Z","shell.execute_reply.started":"2022-01-09T06:02:06.246146Z","shell.execute_reply":"2022-01-09T06:02:06.251506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = open(\"/kaggle/working/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8/pipeline.config\", 'w')\nfile.truncate()\nfile.write(config)\nfile.close()","metadata":{"execution":{"iopub.status.busy":"2022-01-09T06:02:06.253376Z","iopub.execute_input":"2022-01-09T06:02:06.253742Z","iopub.status.idle":"2022-01-09T06:02:06.270169Z","shell.execute_reply.started":"2022-01-09T06:02:06.253704Z","shell.execute_reply":"2022-01-09T06:02:06.269466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!wget https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/model_main_tf2.py -P /kaggle/working/workspace/scripts","metadata":{"execution":{"iopub.status.busy":"2022-01-09T06:02:06.272339Z","iopub.execute_input":"2022-01-09T06:02:06.272950Z","iopub.status.idle":"2022-01-09T06:02:07.373261Z","shell.execute_reply.started":"2022-01-09T06:02:06.272904Z","shell.execute_reply":"2022-01-09T06:02:07.372211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install tensorflow-estimator==2.6.0\n# !pip install tensorflow==2.6.0\n# !pip install keras==2.6.0","metadata":{"execution":{"iopub.status.busy":"2022-01-09T06:02:07.374887Z","iopub.execute_input":"2022-01-09T06:02:07.375234Z","iopub.status.idle":"2022-01-09T06:02:07.379896Z","shell.execute_reply.started":"2022-01-09T06:02:07.375189Z","shell.execute_reply":"2022-01-09T06:02:07.379205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !cd /kaggle/working/workspace/scripts\n\n!python /kaggle/working/workspace/scripts/model_main_tf2.py  --logtostderr --model_dir=/kaggle/working/training --pipeline_config_path=/kaggle/working/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8/pipeline.config","metadata":{"execution":{"iopub.status.busy":"2022-01-09T06:02:07.381019Z","iopub.execute_input":"2022-01-09T06:02:07.381240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}