Skip to content

Instantly share code, notes, and snippets.

@andreibosco
Last active October 24, 2021 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreibosco/d8ac62bb78686f4f4c5406b2bce33402 to your computer and use it in GitHub Desktop.
Save andreibosco/d8ac62bb78686f4f4c5406b2bce33402 to your computer and use it in GitHub Desktop.
Setup environment with TF 1.14 + OpenCV + CUDA for Object Detection

Work in progress

Setup TF1.14 environment for object detection

Commands (will try to create a dockerfile based on this later)

# create conda environment with all necessary packages
conda create -n tf114 pip python=3.7 tensorflow-gpu=1.14 gast=0.2.2 pandas pillow lxml jupyter matplotlib opencv cython
conda activate tf114
# base dir structure
mkdir /opt/tf-object_detection
cd /opt/tf-object_detection
mkdir TensorFlow
cd TensorFlow
mkdir scripts
mkdir workspace
cd workspace
mkdir annotations
mkdir images
mkdir -p custom-models/my_ssd_mobnet
mkdir pre-trained-models
# downloading TensorFlow models
cd /opt/tf-object_detection/TensorFlow
git clone -b r1.13.0 https://github.com/andreibosco/models.git --depth 1
# protobuf installation / compilation (just an example, modify for the distro being used)
apk install protobuf
cd models/research
protoc object_detection/protos/*.proto --python_out=.
# install object-detection package
cd /opt/tf-object_detection/TensorFlow/models/research/object_detection
pip install .
# add research/slim to PYTHONPATH
export PYTHONPATH=$PYTHONPATH:/opt/tf-object_detection/TensorFlow/models/research/slim
# COCO API installation
cd /opt/tf-object_detection
git clone https://github.com/cocodataset/cocoapi.git --depth 1
cd cocoapi/PythonAPI
make
cp -r pycocotools /opt/tf-object_detection/TensorFlow/models/research/
make install
# download script to generate TF records (from TF2 object detection tutorial)
cd /opt/tf-object_detection/TensorFlow/scripts
wget https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/_downloads/da4babe668a8afb093cc7776d7e630f3/generate_tfrecord.py
# download pre-trained model from TF Zoo: https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf1_detection_zoo.md#coco-trained-models
cd /opt/tf-object_detection/TensorFlow/workspace/pre-trained-models
#wget http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_coco_2018_03_29.tar.gz
wget http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_mnasfpn_shared_box_predictor_320x320_coco_sync_2020_05_18.tar.gz
tar xvzf ssd_mobilenet_v2_mnasfpn_shared_box_predictor_320x320_coco_sync_2020_05_18.tar.gz
cp ssd_mobilenet_v2_mnasfpn_shared_box_predictor_320x320_coco_sync_2020_05_18/pipeline.config /opt/tf-object_detection/TensorFlow/workspace/custom-models/my_ssd_mobnet

Manual part

  • Follow Tutorial.ipynb to create a custom model (will add this file to github later)

Create inference graph

cd /opt/tf-object_detection/TensorFlow
python models/research/object_detection/export_inference_graph.py --input_type image_tensor --pipeline_config_path workspace/custom-models/my_ssd_mobnet/pipeline.config --trained_checkpoint_prefix workspace/custom-models/my_ssd_mobnet/model.ckpt-5000 --output_directory workspace/custom-models/my_ssd_mobnet/inference_graph

TEST -- fix 'incomplete shape' warning:

python models/research/object_detection/export_inference_graph.py --input_type image_tensor --input_shape 1,300,300,3 --pipeline_config_path workspace/custom-models/my_ssd_mobnet/pipeline.config --trained_checkpoint_prefix workspace/custom-models/my_ssd_mobnet/model.ckpt-50000 --output_directory workspace/custom-models/my_ssd_mobnet/inference_graph

Create pbtxt from inference graph (config file)

cd /opt/tf-object_detection/TensorFlow/scripts
wget https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/tf_text_graph_ssd.py
wget https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/tf_text_graph_common.py
cd /opt/tf-object_detection/TensorFlow/workspace/custom-models/my_ssd_mobnet/inference_graph
python /opt/tf-object_detection/TensorFlow/scripts/tf_text_graph_ssd.py --input frozen_inference_graph.pb --config pipeline.config --output new_graph.pbtxt

Test object detection

  • Edit and run /opt/tf-object_detection/TensorFlow/models/research/object_detection/test_model.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment