{"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":"# TFOD instalation and setup","metadata":{"execution":{"iopub.status.busy":"2021-07-26T11:16:26.696775Z","iopub.execute_input":"2021-07-26T11:16:26.69715Z","iopub.status.idle":"2021-07-26T11:16:26.700951Z","shell.execute_reply.started":"2021-07-26T11:16:26.697061Z","shell.execute_reply":"2021-07-26T11:16:26.700184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check NVIDIA System Management Interface\n!nvidia-smi\n\n# If you'd like to see your GPU (maybe to see if it's CUDA compatible)\n!lspci | grep -i nvidia\n\n# If you's like to see the location of CUDA\n!whereis cuda\n\n# If you'd like to see CUDA version\n!nvcc --version\n\n# Check presence of CuDNN \n!whereis cudnn\n\n# Check version of cudnn\n!cat /usr/include/cudnn_version.h","metadata":{"execution":{"iopub.status.busy":"2021-07-26T11:16:27.53961Z","iopub.execute_input":"2021-07-26T11:16:27.539886Z","iopub.status.idle":"2021-07-26T11:16:31.599687Z","shell.execute_reply.started":"2021-07-26T11:16:27.53986Z","shell.execute_reply":"2021-07-26T11:16:31.598776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Check is TensorFlow can access GPU\nfrom tensorflow.python.client import device_lib\ndevice_lib.list_local_devices()\n\n\n\n# Alternatively, check device name\nimport tensorflow as tf\ntf.test.gpu_device_name()\n\n# Check for CUDA\ntf.test.is_built_with_cuda()","metadata":{"execution":{"iopub.status.busy":"2021-07-26T11:16:31.601443Z","iopub.execute_input":"2021-07-26T11:16:31.601798Z","iopub.status.idle":"2021-07-26T11:16:37.157931Z","shell.execute_reply.started":"2021-07-26T11:16:31.601757Z","shell.execute_reply":"2021-07-26T11:16:37.15711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Clearly, TensorFlow is able access GPU and everything seems normal. What we need to ensure is that the **TensorFlow 2 Object Detection API** is able to access the **GPU**.","metadata":{}},{"cell_type":"markdown","source":"**What options do we have?**\n1. We could upgrade to CUDA v11.2. I found this <a href=\"https://medium.com/analytics-vidhya/installing-any-version-of-cuda-on-ubuntu-and-using-tensorflow-and-torch-on-gpu-b1a954500786\">blog</a> useful for doing so. But it involves installing 2-3GB CUDA Toolkit from <a href=\"https://developer.nvidia.com/cuda-11.2.0-download-archive?target_os=Linux&target_arch=x86_64&target_distro=Ubuntu&target_version=1804&target_type=runfilelocal\">here</a> and other complicated software engineering that's especially difficult on Kaggle.\n2. We can work with an older version of TensorFlow.\n\n**How do we work with an older version?**\n1. Install the latest TF2 OD API (v2.5). You'll also find a directory structure with everything you need.\n2. Uninstall tf-models-official v2.5\n3. Install tensorflow v2.4\n4. Install tf-models-official v2.4 (it is compatible with the hardware/software configuration)","metadata":{}},{"cell_type":"markdown","source":"### TensorFlow Object Detection API Installation\n#### 1. Downloading the TensorFlow Model Garden\n>workspace/\n\n>└─ models/\n\n>        ├─ community/\n\n>        ├─ official/\n\n>        ├─ orbit/\n\n>        ├─ research/\n\n>        └── ...\n#### 2. Protobuf Installation/Compilation\n#### 3. COCO API installation\n#### 4. Install the Object Detection API","metadata":{}},{"cell_type":"code","source":"# Download models\n!git clone --depth 1 https://github.com/tensorflow/models\n\n# Compile proto files \n%cd models/research\n!pip install protobuf\n!protoc object_detection/protos/*.proto --python_out=.\n%cd ..\n%cd ..\n\n# Install cocoapi\n!pip install cython \n!git clone https://github.com/cocodataset/cocoapi.git\n%cd cocoapi/PythonAPI\n!make\n%cd ..\n%cd ..\n!cp -r cocoapi/PythonAPI/pycocotools models/research/\n\n# Install object detection api\n%cd models/research\n!cp object_detection/packages/tf2/setup.py .\n!python -m pip install .\n%cd ..\n%cd ..","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":false,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2021-07-26T11:16:37.159876Z","iopub.execute_input":"2021-07-26T11:16:37.160216Z","iopub.status.idle":"2021-07-26T11:18:46.048552Z","shell.execute_reply.started":"2021-07-26T11:16:37.160163Z","shell.execute_reply":"2021-07-26T11:18:46.047632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# View tensorflow installation\n!pip freeze | grep tensorflow\n\n# Uninstall version 2.5\n!pip uninstall tf-models-official --yes\n# Install version 2.4\n!pip install tensorflow==2.4\n!pip install tf-models-official==2.4","metadata":{"_kg_hide-output":false,"execution":{"iopub.status.busy":"2021-07-26T11:18:46.050667Z","iopub.execute_input":"2021-07-26T11:18:46.051049Z","iopub.status.idle":"2021-07-26T11:19:51.12213Z","shell.execute_reply.started":"2021-07-26T11:18:46.050991Z","shell.execute_reply":"2021-07-26T11:19:51.121218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# View tensorflow installation\n!pip freeze | grep tensorflow","metadata":{"_kg_hide-output":false,"execution":{"iopub.status.busy":"2021-07-26T11:19:51.125433Z","iopub.execute_input":"2021-07-26T11:19:51.125742Z","iopub.status.idle":"2021-07-26T11:19:52.839862Z","shell.execute_reply.started":"2021-07-26T11:19:51.12571Z","shell.execute_reply":"2021-07-26T11:19:52.838841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Verify TensorFlow 2 Object Detection API installation (optional)\n%cd models/research\n!python object_detection/builders/model_builder_tf2_test.py\n%cd ..\n%cd ..","metadata":{"_kg_hide-output":false,"execution":{"iopub.status.busy":"2021-07-26T11:19:52.841674Z","iopub.execute_input":"2021-07-26T11:19:52.842088Z","iopub.status.idle":"2021-07-26T11:20:27.295225Z","shell.execute_reply.started":"2021-07-26T11:19:52.842032Z","shell.execute_reply":"2021-07-26T11:20:27.294314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Making Directories \n!mkdir workspace\n%cd workspace\n!mkdir annotations\n!mkdir script\n!mkdir csv_files\n!mkdir pre_trained_models\n!mkdir models","metadata":{"execution":{"iopub.status.busy":"2021-07-26T11:20:27.296827Z","iopub.execute_input":"2021-07-26T11:20:27.297212Z","iopub.status.idle":"2021-07-26T11:20:31.15357Z","shell.execute_reply.started":"2021-07-26T11:20:27.297153Z","shell.execute_reply":"2021-07-26T11:20:31.152132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Skip this..........if you already having tf.records generated and uploaded to kaggle/input/**tf_records folder\n\n# or else Run this following code\n# make sure you have the necessary kaggle/input/folders are uploaded properly\n\n# copy the scripts file uploaded to kaggle/input/scripts to kaggle/.../workspace/scripts/\n%cd script\n!cp /kaggle/input/opacity-files/generate_tfrecord_csv_to_tfrecord.py .\n!ls  \n%cd ..\n\n# Copy the csv files uploaded to kaggle/input/**csv_files to kaggle/working/workspace/csv_files folders\n%cd csv_files\n!cp /kaggle/input/opacity-files/opacity_train.csv  .\n!cp /kaggle/input/opacity-files/opacity_test.csv  .\n!ls\n%cd ..\n\n# run this code to generate tfrecords --- if tf records are not present in kaggle input or kaggle/working/annotations folders\n%cd script\n! python generate_tfrecord_csv_to_tfrecord.py --csv_input=/kaggle/working/workspace/csv_files/opacity_train.csv  --output_path=/kaggle/working/workspace/annotations/train.record  --image_dir=\"\"\n! python generate_tfrecord_csv_to_tfrecord.py --csv_input=/kaggle/working/workspace/csv_files/opacity_test.csv  --output_path=/kaggle/working/workspace/annotations/test.record  --image_dir=\"\"\n\n%cd ..\n%cd annotations\n# copy and move the label_map_file\n!cp /kaggle/input/opacity-files/label_map.pbtxt   .\n!ls\n%cd ..","metadata":{"execution":{"iopub.status.busy":"2021-07-26T11:20:31.164069Z","iopub.execute_input":"2021-07-26T11:20:31.164472Z","iopub.status.idle":"2021-07-26T11:21:12.983382Z","shell.execute_reply.started":"2021-07-26T11:20:31.164427Z","shell.execute_reply":"2021-07-26T11:21:12.982447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Don't Run this cell\n\nLook at the instruction belowe before running it","metadata":{}},{"cell_type":"code","source":"# Run this only if tf.record file are already uploaded to kaggle/input/**tfrecords/folder\n# copy the available file to appropriate kaggle/working/***   folders\n\n# skip this if you are generating tf.records here on the run.....\n%cd annotations\n!cp /kaggle/input/covid-scripts/covid_label_map.pbtxt . \n!cp /kaggle/input/****/train.record . \n!cp /kaggle/input/****/test.record . \n\n!ls\n%cd ..","metadata":{"execution":{"iopub.status.busy":"2021-07-23T11:46:21.042819Z","iopub.execute_input":"2021-07-23T11:46:21.043212Z","iopub.status.idle":"2021-07-23T11:46:23.69032Z","shell.execute_reply.started":"2021-07-23T11:46:21.043159Z","shell.execute_reply":"2021-07-23T11:46:23.689029Z"},"jupyter":{"source_hidden":true,"outputs_hidden":true},"collapsed":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd pre_trained_models\n# Choose model of your choice and dpaste the download link kere \n!wget http://download.tensorflow.org/models/object_detection/tf2/20200711/efficientdet_d1_coco17_tpu-32.tar.gz\n!tar -xf efficientdet_d1_coco17_tpu-32.tar.gz\n%cd efficientdet_d1_coco17_tpu-32\n!ls\n%cd ..\n%cd ..","metadata":{"execution":{"iopub.status.busy":"2021-07-26T11:21:12.985401Z","iopub.execute_input":"2021-07-26T11:21:12.985765Z","iopub.status.idle":"2021-07-26T11:21:16.76301Z","shell.execute_reply.started":"2021-07-26T11:21:12.985724Z","shell.execute_reply":"2021-07-26T11:21:16.762133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# copying uploaded config file from kaggle/input/**config folder\n%cd models\n!mkdir efficientdet_d1_coco17_tpu-32\n%cd efficientdet_d1_coco17_tpu-32\n!cp /kaggle/input/ednetd1-config/pipeline.config  . \n!ls\n%cd ..\n%cd ..","metadata":{"execution":{"iopub.status.busy":"2021-07-26T11:21:16.764638Z","iopub.execute_input":"2021-07-26T11:21:16.765008Z","iopub.status.idle":"2021-07-26T11:21:18.713867Z","shell.execute_reply.started":"2021-07-26T11:21:16.76497Z","shell.execute_reply":"2021-07-26T11:21:18.713003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make sure that your are in kaggle/working/workspace\n# execute the following commands","metadata":{"execution":{"iopub.status.busy":"2021-07-26T11:21:18.715426Z","iopub.execute_input":"2021-07-26T11:21:18.71578Z","iopub.status.idle":"2021-07-26T11:21:18.719527Z","shell.execute_reply.started":"2021-07-26T11:21:18.71574Z","shell.execute_reply":"2021-07-26T11:21:18.718638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp  /kaggle/working/models/research/object_detection/model_main_tf2.py  .","metadata":{"execution":{"iopub.status.busy":"2021-07-26T11:25:25.722761Z","iopub.execute_input":"2021-07-26T11:25:25.723132Z","iopub.status.idle":"2021-07-26T11:25:26.364668Z","shell.execute_reply.started":"2021-07-26T11:25:25.723102Z","shell.execute_reply":"2021-07-26T11:25:26.363668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Command line to start Training","metadata":{"execution":{"iopub.status.busy":"2021-07-24T03:38:50.554015Z","iopub.execute_input":"2021-07-24T03:38:50.554415Z","iopub.status.idle":"2021-07-24T03:38:50.558166Z","shell.execute_reply.started":"2021-07-24T03:38:50.554324Z","shell.execute_reply":"2021-07-24T03:38:50.557308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python model_main_tf2.py --model_dir=models/efficientdet_d1_coco17_tpu-32 --pipeline_config_path=models/efficientdet_d1_coco17_tpu-32/pipeline.config","metadata":{"execution":{"iopub.status.busy":"2021-07-26T11:25:32.251544Z","iopub.execute_input":"2021-07-26T11:25:32.251873Z","iopub.status.idle":"2021-07-26T11:47:13.008349Z","shell.execute_reply.started":"2021-07-26T11:25:32.251842Z","shell.execute_reply":"2021-07-26T11:47:13.0074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exporting the trained model","metadata":{"execution":{"iopub.status.busy":"2021-07-23T17:10:37.700956Z","iopub.execute_input":"2021-07-23T17:10:37.701377Z","iopub.status.idle":"2021-07-23T17:10:37.71058Z","shell.execute_reply.started":"2021-07-23T17:10:37.701344Z","shell.execute_reply":"2021-07-23T17:10:37.70933Z"}}},{"cell_type":"code","source":"!cp /kaggle/working/models/research/object_detection/exporter_main_v2.py  .","metadata":{"execution":{"iopub.status.busy":"2021-07-26T11:47:20.286891Z","iopub.execute_input":"2021-07-26T11:47:20.287229Z","iopub.status.idle":"2021-07-26T11:47:20.935822Z","shell.execute_reply.started":"2021-07-26T11:47:20.287176Z","shell.execute_reply":"2021-07-26T11:47:20.934824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir trained_models","metadata":{"execution":{"iopub.status.busy":"2021-07-26T11:47:32.154499Z","iopub.execute_input":"2021-07-26T11:47:32.15483Z","iopub.status.idle":"2021-07-26T11:47:32.79818Z","shell.execute_reply.started":"2021-07-26T11:47:32.1548Z","shell.execute_reply":"2021-07-26T11:47:32.797132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!python exporter_main_v2.py --input_type image_tensor --pipeline_config_path  models/efficientdet_d1_coco17_tpu-32/pipeline.config --trained_checkpoint_dir models/efficientdet_d1_coco17_tpu-32 --output_directory trained_models","metadata":{"execution":{"iopub.status.busy":"2021-07-26T11:47:40.148671Z","iopub.execute_input":"2021-07-26T11:47:40.149004Z","iopub.status.idle":"2021-07-26T11:50:16.390118Z","shell.execute_reply.started":"2021-07-26T11:47:40.148972Z","shell.execute_reply":"2021-07-26T11:50:16.388952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" #! zip \"/kaggle/working/Opacity_model.zip\" \"/kaggle/working/workspace/trained_models\"","metadata":{"execution":{"iopub.status.busy":"2021-07-26T08:09:01.36852Z","iopub.execute_input":"2021-07-26T08:09:01.368856Z","iopub.status.idle":"2021-07-26T08:09:02.070383Z","shell.execute_reply.started":"2021-07-26T08:09:01.368828Z","shell.execute_reply":"2021-07-26T08:09:02.069372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pwd\n","metadata":{"execution":{"iopub.status.busy":"2021-07-26T12:20:27.945823Z","iopub.execute_input":"2021-07-26T12:20:27.946149Z","iopub.status.idle":"2021-07-26T12:20:28.710884Z","shell.execute_reply.started":"2021-07-26T12:20:27.94612Z","shell.execute_reply":"2021-07-26T12:20:28.709922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ..","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!cp -r /kaggle/working/workspace/trained_models   .","metadata":{"execution":{"iopub.status.busy":"2021-07-26T12:23:58.925137Z","iopub.execute_input":"2021-07-26T12:23:58.925492Z","iopub.status.idle":"2021-07-26T12:23:59.656929Z","shell.execute_reply.started":"2021-07-26T12:23:58.925462Z","shell.execute_reply":"2021-07-26T12:23:59.655924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nshutil.rmtree('/kaggle/working/cocoapi')\nshutil.rmtree('/kaggle/working/models')\nshutil.rmtree('/kaggle/working/workspace')","metadata":{},"execution_count":null,"outputs":[]}]}