{"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":"## Introduction\n\n\n* **[Setup Credits](https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html)**.\n* **[[COTS]TFOD2(API Setup + Faster RCNN)](https://www.kaggle.com/datastrophy/cots-tfod2-api-setup-faster-rcnn/notebook)**.","metadata":{}},{"cell_type":"markdown","source":"## TFOD Setup","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"!git clone https://github.com/tensorflow/models\n# Check out a certain commit to ensure that future changes in the TF ODT API codebase won't affect this notebook.\n!cd models && git checkout ac8d06519","metadata":{"execution":{"iopub.status.busy":"2021-12-19T15:51:59.510922Z","iopub.execute_input":"2021-12-19T15:51:59.511466Z","iopub.status.idle":"2021-12-19T15:52:20.801972Z","shell.execute_reply.started":"2021-12-19T15:51:59.511332Z","shell.execute_reply":"2021-12-19T15:52:20.801077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%bash\ncd models/research\n\n# Compile protos.\nprotoc object_detection/protos/*.proto --python_out=.\n\n# Install TensorFlow Object Detection API.\n# Note: I fixed the version of some dependencies to make it work on Kaggle notebook. In particular:\n# * scipy==1.6.3 to avoid the missing GLIBCXX_3.4.26 error\n# * tensorflow to 2.6.0 to make it compatible with the CUDA version preinstalled on Kaggle.\n# When Kaggle notebook upgrade to TF 2.7, you can use the default setup.py script:\n# cp object_detection/packages/tf2/setup.py .\nwget https://storage.googleapis.com/odml-dataset/others/setup.py\npip install --user .\n\n# Test if the Object Dectection API is working correctly\npython object_detection/builders/model_builder_tf2_test.py","metadata":{"execution":{"iopub.status.busy":"2021-12-19T15:52:20.804088Z","iopub.execute_input":"2021-12-19T15:52:20.804345Z","iopub.status.idle":"2021-12-19T15:54:35.494268Z","shell.execute_reply.started":"2021-12-19T15:52:20.804314Z","shell.execute_reply":"2021-12-19T15:54:35.493071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %cd ./models/research","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-12-19T15:54:35.496017Z","iopub.execute_input":"2021-12-19T15:54:35.496302Z","iopub.status.idle":"2021-12-19T15:54:35.501065Z","shell.execute_reply.started":"2021-12-19T15:54:35.49627Z","shell.execute_reply":"2021-12-19T15:54:35.500143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# From within TensorFlow/models/research/\n# !protoc object_detection/protos/*.proto --python_out=.","metadata":{"execution":{"iopub.status.busy":"2021-12-19T15:54:35.502761Z","iopub.execute_input":"2021-12-19T15:54:35.503068Z","iopub.status.idle":"2021-12-19T15:54:35.514046Z","shell.execute_reply.started":"2021-12-19T15:54:35.503027Z","shell.execute_reply":"2021-12-19T15:54:35.513426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !cp object_detection/packages/tf2/setup.py .","metadata":{"execution":{"iopub.status.busy":"2021-12-19T15:54:35.517108Z","iopub.execute_input":"2021-12-19T15:54:35.517794Z","iopub.status.idle":"2021-12-19T15:54:35.526716Z","shell.execute_reply.started":"2021-12-19T15:54:35.517748Z","shell.execute_reply":"2021-12-19T15:54:35.52599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !cat setup.py","metadata":{"execution":{"iopub.status.busy":"2021-12-19T15:54:35.528475Z","iopub.execute_input":"2021-12-19T15:54:35.529104Z","iopub.status.idle":"2021-12-19T15:54:35.539016Z","shell.execute_reply.started":"2021-12-19T15:54:35.529058Z","shell.execute_reply":"2021-12-19T15:54:35.53822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%writefile setup.py\n# \"\"\"Setup script for object_detection with TF2.0.\"\"\"\n# import os\n# from setuptools import find_packages\n# from setuptools import setup\n\n# REQUIRED_PACKAGES = [\n#     # Required for apache-beam with PY3\n#     'avro-python3',\n#     'apache-beam',\n#     'pillow',\n#     'lxml',\n#     'matplotlib',\n#     'Cython',\n#     'contextlib2',\n#     'tf-slim',\n#     'six',\n#     'pycocotools',\n#     'lvis',\n#     'scipy==1.6.3',\n#     'pandas',\n#     'tf-models-official>=2.5.1',\n#     'tensorflow_io',\n#     'keras==2.6.0',\n#     'tensorflow==2.6.0'\n# ]\n\n# setup(\n#     name='object_detection',\n#     version='0.1',\n#     install_requires=REQUIRED_PACKAGES,\n#     include_package_data=True,\n#     packages=(\n#         [p for p in find_packages() if p.startswith('object_detection')] +\n#         find_packages(where=os.path.join('.', 'slim'))),\n#     package_dir={\n#         'datasets': os.path.join('slim', 'datasets'),\n#         'nets': os.path.join('slim', 'nets'),\n#         'preprocessing': os.path.join('slim', 'preprocessing'),\n#         'deployment': os.path.join('slim', 'deployment'),\n#         'scripts': os.path.join('slim', 'scripts'),\n#     },\n#     description='Tensorflow Object Detection Library',\n#     python_requires='>3.6',\n# )","metadata":{"execution":{"iopub.status.busy":"2021-12-19T15:54:35.540143Z","iopub.execute_input":"2021-12-19T15:54:35.540778Z","iopub.status.idle":"2021-12-19T15:54:35.552111Z","shell.execute_reply.started":"2021-12-19T15:54:35.540736Z","shell.execute_reply":"2021-12-19T15:54:35.551252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !cat setup.py","metadata":{"execution":{"iopub.status.busy":"2021-12-19T15:54:35.553419Z","iopub.execute_input":"2021-12-19T15:54:35.554089Z","iopub.status.idle":"2021-12-19T15:54:35.567098Z","shell.execute_reply.started":"2021-12-19T15:54:35.554051Z","shell.execute_reply":"2021-12-19T15:54:35.566174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%bash\n# pip install --user .","metadata":{"execution":{"iopub.status.busy":"2021-12-19T15:54:35.568752Z","iopub.execute_input":"2021-12-19T15:54:35.569058Z","iopub.status.idle":"2021-12-19T15:54:35.579245Z","shell.execute_reply.started":"2021-12-19T15:54:35.569019Z","shell.execute_reply":"2021-12-19T15:54:35.578389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Test if the Object Dectection API is working correctly\n# !python object_detection/builders/model_builder_tf2_test.py","metadata":{"execution":{"iopub.status.busy":"2021-12-19T15:54:35.580783Z","iopub.execute_input":"2021-12-19T15:54:35.581367Z","iopub.status.idle":"2021-12-19T15:54:35.590201Z","shell.execute_reply.started":"2021-12-19T15:54:35.581321Z","shell.execute_reply":"2021-12-19T15:54:35.589425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import contextlib2\nimport tensorflow as tf\n\ntf.test.is_gpu_available(\n    cuda_only=False, min_cuda_compute_capability=None\n)","metadata":{"execution":{"iopub.status.busy":"2021-12-19T15:54:35.591873Z","iopub.execute_input":"2021-12-19T15:54:35.592437Z","iopub.status.idle":"2021-12-19T15:54:36.806732Z","shell.execute_reply.started":"2021-12-19T15:54:35.592392Z","shell.execute_reply":"2021-12-19T15:54:36.804765Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import contextlib2\nimport io\nimport IPython\nimport json\nimport numpy as np\nimport os\nimport pathlib\nimport pandas as pd\nimport sys\nimport tensorflow as tf\nimport time\n\nfrom PIL import Image, ImageDraw","metadata":{"execution":{"iopub.status.busy":"2021-12-19T15:54:36.808208Z","iopub.status.idle":"2021-12-19T15:54:36.80873Z","shell.execute_reply.started":"2021-12-19T15:54:36.808438Z","shell.execute_reply":"2021-12-19T15:54:36.808464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}