{"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 notebook shows how to install and use LSTM Object Detection (https://github.com/tensorflow/models/tree/master/research/lstm_object_detection). You'll have to use TF1 for it to work.","metadata":{}},{"cell_type":"code","source":"## On Colab, instead of this just use \n# %tensorflow_version 1.x\n!pip install tensorboard==1.15.0 tensorboard-data-server==0.6.1 tensorboard-plugin-wit==1.8.1 \\\ntensorflow==1.15.2 tensorflow-addons==0.15.0 tensorflow-datasets==4.0.1 tensorflow-estimator==1.15.1 \\\ntensorflow-gan==2.0.0 tensorflow-gcs-config==2.7.0 tensorflow-hub==0.12.0 tensorflow-io==0.24.0 \\\ntensorflow-io-gcs-filesystem==0.24.0 tensorflow-metadata==1.6.0 tensorflow-model-optimization==0.7.0 \\\ntensorflow-probability==0.7.0 tensor2tensor==1.14.1","metadata":{"execution":{"iopub.status.busy":"2022-02-05T19:53:35.371659Z","iopub.execute_input":"2022-02-05T19:53:35.372015Z","iopub.status.idle":"2022-02-05T19:53:43.518752Z","shell.execute_reply.started":"2022-02-05T19:53:35.371961Z","shell.execute_reply":"2022-02-05T19:53:43.517929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from string import Template\nimport tensorflow as tf\nimport os","metadata":{"tags":[],"execution":{"iopub.status.busy":"2022-02-05T19:53:43.522184Z","iopub.execute_input":"2022-02-05T19:53:43.52241Z","iopub.status.idle":"2022-02-05T19:53:43.528536Z","shell.execute_reply.started":"2022-02-05T19:53:43.522381Z","shell.execute_reply":"2022-02-05T19:53:43.527849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/tensorflow/models/\n    \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":"2022-02-05T19:53:43.530175Z","iopub.execute_input":"2022-02-05T19:53:43.530433Z","iopub.status.idle":"2022-02-05T19:54:06.138485Z","shell.execute_reply.started":"2022-02-05T19:53:43.530398Z","shell.execute_reply":"2022-02-05T19:54:06.137659Z"},"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.\ncp object_detection/packages/tf1/setup.py .\npip install -q --user .\n\n# Test if the Object Dectection API is working correctly\npython object_detection/builders/model_builder_test.py","metadata":{"execution":{"iopub.status.busy":"2022-02-05T19:54:06.141357Z","iopub.execute_input":"2022-02-05T19:54:06.141643Z","iopub.status.idle":"2022-02-05T19:54:23.638847Z","shell.execute_reply.started":"2022-02-05T19:54:06.141602Z","shell.execute_reply":"2022-02-05T19:54:23.637986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%writefile models/research/setup.py\n\n\"\"\"Setup script for object_detection with TF2.0.\"\"\"\nimport os\nfrom setuptools import find_packages\nfrom setuptools import setup\n\nsetup(\n    name='lstm_object_detection',\n    version='0.1',\n    install_requires=[],\n    include_package_data=True,\n    packages=(\n        [p for p in find_packages() if p.startswith('lstm_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 LSTM Object Detection Library',\n    python_requires='>3.6',\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T19:54:23.640689Z","iopub.execute_input":"2022-02-05T19:54:23.640991Z","iopub.status.idle":"2022-02-05T19:54:23.648572Z","shell.execute_reply.started":"2022-02-05T19:54:23.640922Z","shell.execute_reply":"2022-02-05T19:54:23.647803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%bash\ncd models/research\n\n# Compile protos.\nprotoc lstm_object_detection/protos/*.proto --python_out=.\n\n# Install TensorFlow Object Detection API.\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 .\npip install -q --user .\n\n# Test if the Object Dectection API is working correctly\npython lstm_object_detection/model_builder_test.py","metadata":{"tags":[],"execution":{"iopub.status.busy":"2022-02-05T19:54:23.650003Z","iopub.execute_input":"2022-02-05T19:54:23.65064Z","iopub.status.idle":"2022-02-05T19:54:41.824792Z","shell.execute_reply.started":"2022-02-05T19:54:23.6506Z","shell.execute_reply":"2022-02-05T19:54:41.823969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2022-02-05T19:54:41.826588Z","iopub.execute_input":"2022-02-05T19:54:41.826997Z","iopub.status.idle":"2022-02-05T19:54:41.832926Z","shell.execute_reply.started":"2022-02-05T19:54:41.826933Z","shell.execute_reply":"2022-02-05T19:54:41.832266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**I have absolutely no idea how to set pipeline config file - if you do, please comment**","metadata":{}},{"cell_type":"code","source":"config_file_template = \"\"\"\n????\n\"\"\"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pipeline = Template(config_file_template).substitute()\n\nwith open(PIPELINE_CONFIG_PATH, 'w') as f:\n    f.write(pipeline)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MODEL_DIR='tf_lstm_det'","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir {MODEL_DIR}\n!python models/research/lstm_object_detection/train.py \\\n    --pipeline_config_path={PIPELINE_CONFIG_PATH} \\\n    --train_dir={MODEL_DIR} \\\n    --alsologtostderr","metadata":{"tags":[],"execution":{"iopub.status.busy":"2022-02-05T19:54:41.952291Z","iopub.execute_input":"2022-02-05T19:54:41.952799Z","iopub.status.idle":"2022-02-05T19:54:49.013027Z","shell.execute_reply.started":"2022-02-05T19:54:41.952762Z","shell.execute_reply":"2022-02-05T19:54:49.012189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Cleanup\n!rm -rf dataset models","metadata":{"execution":{"iopub.status.busy":"2022-02-05T19:51:51.794512Z","iopub.execute_input":"2022-02-05T19:51:51.806635Z","iopub.status.idle":"2022-02-05T19:51:52.934333Z","shell.execute_reply.started":"2022-02-05T19:51:51.806588Z","shell.execute_reply":"2022-02-05T19:51:52.933353Z"},"trusted":true},"execution_count":null,"outputs":[]}]}