├── .gitignore ├── README.md ├── Resolute_AI_Assignment_reference_ML.pdf ├── __pycache__ ├── get_inference.cpython-37.pyc └── visualization_utils.cpython-37.pyc ├── data └── label_map.pbtxt ├── get_inference.py ├── main.py ├── main.ui ├── main2.py ├── object_detection ├── .idea │ ├── ResoluteAI.iml │ ├── inspectionProfiles │ │ └── profiles_settings.xml │ ├── misc.xml │ ├── modules.xml │ └── workspace.xml ├── CONTRIBUTING.md ├── README.md ├── __init__.py ├── __pycache__ │ └── __init__.cpython-37.pyc ├── anchor_generators │ ├── __init__.py │ ├── flexible_grid_anchor_generator.py │ ├── flexible_grid_anchor_generator_test.py │ ├── grid_anchor_generator.py │ ├── grid_anchor_generator_test.py │ ├── multiple_grid_anchor_generator.py │ ├── multiple_grid_anchor_generator_test.py │ ├── multiscale_grid_anchor_generator.py │ └── multiscale_grid_anchor_generator_test.py ├── box_coders │ ├── __init__.py │ ├── faster_rcnn_box_coder.py │ ├── faster_rcnn_box_coder_test.py │ ├── keypoint_box_coder.py │ ├── keypoint_box_coder_test.py │ ├── mean_stddev_box_coder.py │ ├── mean_stddev_box_coder_test.py │ ├── square_box_coder.py │ └── square_box_coder_test.py ├── builders │ ├── __init__.py │ ├── anchor_generator_builder.py │ ├── anchor_generator_builder_test.py │ ├── box_coder_builder.py │ ├── box_coder_builder_test.py │ ├── box_predictor_builder.py │ ├── box_predictor_builder_test.py │ ├── calibration_builder.py │ ├── calibration_builder_test.py │ ├── dataset_builder.py │ ├── dataset_builder_test.py │ ├── graph_rewriter_builder.py │ ├── graph_rewriter_builder_test.py │ ├── hyperparams_builder.py │ ├── hyperparams_builder_test.py │ ├── image_resizer_builder.py │ ├── image_resizer_builder_test.py │ ├── input_reader_builder.py │ ├── input_reader_builder_test.py │ ├── losses_builder.py │ ├── losses_builder_test.py │ ├── matcher_builder.py │ ├── matcher_builder_test.py │ ├── model_builder.py │ ├── model_builder_test.py │ ├── optimizer_builder.py │ ├── optimizer_builder_test.py │ ├── post_processing_builder.py │ ├── post_processing_builder_test.py │ ├── preprocessor_builder.py │ ├── preprocessor_builder_test.py │ ├── region_similarity_calculator_builder.py │ ├── region_similarity_calculator_builder_test.py │ ├── target_assigner_builder.py │ └── target_assigner_builder_test.py ├── core │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-37.pyc │ │ └── standard_fields.cpython-37.pyc │ ├── anchor_generator.py │ ├── balanced_positive_negative_sampler.py │ ├── balanced_positive_negative_sampler_test.py │ ├── batch_multiclass_nms_test.py │ ├── batcher.py │ ├── batcher_test.py │ ├── box_coder.py │ ├── box_coder_test.py │ ├── box_list.py │ ├── box_list_ops.py │ ├── box_list_ops_test.py │ ├── box_list_test.py │ ├── box_predictor.py │ ├── class_agnostic_nms_test.py │ ├── data_decoder.py │ ├── data_parser.py │ ├── freezable_batch_norm.py │ ├── freezable_batch_norm_test.py │ ├── keypoint_ops.py │ ├── keypoint_ops_test.py │ ├── losses.py │ ├── losses_test.py │ ├── matcher.py │ ├── matcher_test.py │ ├── minibatch_sampler.py │ ├── minibatch_sampler_test.py │ ├── model.py │ ├── multiclass_nms_test.py │ ├── post_processing.py │ ├── prefetcher.py │ ├── prefetcher_test.py │ ├── preprocessor.py │ ├── preprocessor_cache.py │ ├── preprocessor_test.py │ ├── region_similarity_calculator.py │ ├── region_similarity_calculator_test.py │ ├── standard_fields.py │ ├── target_assigner.py │ └── target_assigner_test.py ├── data │ ├── ava_label_map_v2.1.pbtxt │ ├── face_label_map.pbtxt │ ├── fgvc_2854_classes_label_map.pbtxt │ ├── kitti_label_map.pbtxt │ ├── mscoco_complete_label_map.pbtxt │ ├── mscoco_label_map.pbtxt │ ├── mscoco_minival_ids.txt │ ├── oid_bbox_trainable_label_map.pbtxt │ ├── oid_object_detection_challenge_500_label_map.pbtxt │ ├── oid_v4_label_map.pbtxt │ ├── pascal_label_map.pbtxt │ └── pet_label_map.pbtxt ├── data_decoders │ ├── __init__.py │ ├── tf_example_decoder.py │ └── tf_example_decoder_test.py ├── dataset_tools │ ├── __init__.py │ ├── create_coco_tf_record.py │ ├── create_coco_tf_record_test.py │ ├── create_kitti_tf_record.py │ ├── create_kitti_tf_record_test.py │ ├── create_oid_tf_record.py │ ├── create_pascal_tf_record.py │ ├── create_pascal_tf_record_test.py │ ├── create_pet_tf_record.py │ ├── create_pycocotools_package.sh │ ├── download_and_preprocess_mscoco.sh │ ├── oid_hierarchical_labels_expansion.py │ ├── oid_hierarchical_labels_expansion_test.py │ ├── oid_tfrecord_creation.py │ ├── oid_tfrecord_creation_test.py │ ├── tf_record_creation_util.py │ └── tf_record_creation_util_test.py ├── dockerfiles │ └── android │ │ ├── Dockerfile │ │ └── README.md ├── eval_util.py ├── eval_util_test.py ├── export_inference_graph.py ├── export_tflite_ssd_graph.py ├── export_tflite_ssd_graph_lib.py ├── export_tflite_ssd_graph_lib_test.py ├── exporter.py ├── exporter_test.py ├── g3doc │ ├── challenge_evaluation.md │ ├── configuring_jobs.md │ ├── defining_your_own_model.md │ ├── detection_model_zoo.md │ ├── evaluation_protocols.md │ ├── exporting_models.md │ ├── faq.md │ ├── img │ │ ├── dataset_explorer.png │ │ ├── dogs_detections_output.jpg │ │ ├── example_cat.jpg │ │ ├── groupof_case_eval.png │ │ ├── kites_detections_output.jpg │ │ ├── kites_with_segment_overlay.png │ │ ├── nongroupof_case_eval.png │ │ ├── oid_bus_72e19c28aac34ed8.jpg │ │ ├── oid_monkey_3b4168c89cecbc5b.jpg │ │ ├── oxford_pet.png │ │ ├── tensorboard.png │ │ ├── tensorboard2.png │ │ └── tf-od-api-logo.png │ ├── installation.md │ ├── instance_segmentation.md │ ├── oid_inference_and_evaluation.md │ ├── preparing_inputs.md │ ├── running_locally.md │ ├── running_notebook.md │ ├── running_on_cloud.md │ ├── running_on_mobile_tensorflowlite.md │ ├── running_pets.md │ ├── tpu_compatibility.md │ ├── tpu_exporters.md │ └── using_your_own_dataset.md ├── icons │ ├── download.jpg │ └── logo.png ├── inference │ ├── __init__.py │ ├── detection_inference.py │ ├── detection_inference_test.py │ └── infer_detections.py ├── inputs.py ├── inputs_test.py ├── legacy │ ├── __init__.py │ ├── eval.py │ ├── evaluator.py │ ├── train.py │ ├── trainer.py │ └── trainer_test.py ├── matchers │ ├── __init__.py │ ├── argmax_matcher.py │ ├── argmax_matcher_test.py │ ├── bipartite_matcher.py │ └── bipartite_matcher_test.py ├── meta_architectures │ ├── __init__.py │ ├── faster_rcnn_meta_arch.py │ ├── faster_rcnn_meta_arch_test.py │ ├── faster_rcnn_meta_arch_test_lib.py │ ├── rfcn_meta_arch.py │ ├── rfcn_meta_arch_test.py │ ├── ssd_meta_arch.py │ ├── ssd_meta_arch_test.py │ └── ssd_meta_arch_test_lib.py ├── metrics │ ├── __init__.py │ ├── calibration_evaluation.py │ ├── calibration_evaluation_test.py │ ├── calibration_metrics.py │ ├── calibration_metrics_test.py │ ├── coco_evaluation.py │ ├── coco_evaluation_test.py │ ├── coco_tools.py │ ├── coco_tools_test.py │ ├── io_utils.py │ ├── offline_eval_map_corloc.py │ ├── offline_eval_map_corloc_test.py │ ├── oid_challenge_evaluation.py │ ├── oid_challenge_evaluation_utils.py │ ├── oid_challenge_evaluation_utils_test.py │ ├── oid_vrd_challenge_evaluation.py │ ├── oid_vrd_challenge_evaluation_utils.py │ ├── oid_vrd_challenge_evaluation_utils_test.py │ ├── tf_example_parser.py │ └── tf_example_parser_test.py ├── model_hparams.py ├── model_lib.py ├── model_lib_test.py ├── model_lib_v2.py ├── model_lib_v2_test.py ├── model_main.py ├── model_tpu_main.py ├── object_detection_tutorial.ipynb ├── predictors │ ├── __init__.py │ ├── convolutional_box_predictor.py │ ├── convolutional_box_predictor_test.py │ ├── convolutional_keras_box_predictor.py │ ├── convolutional_keras_box_predictor_test.py │ ├── heads │ │ ├── __init__.py │ │ ├── box_head.py │ │ ├── box_head_test.py │ │ ├── class_head.py │ │ ├── class_head_test.py │ │ ├── head.py │ │ ├── keras_box_head.py │ │ ├── keras_box_head_test.py │ │ ├── keras_class_head.py │ │ ├── keras_class_head_test.py │ │ ├── keras_mask_head.py │ │ ├── keras_mask_head_test.py │ │ ├── keypoint_head.py │ │ ├── keypoint_head_test.py │ │ ├── mask_head.py │ │ └── mask_head_test.py │ ├── mask_rcnn_box_predictor.py │ ├── mask_rcnn_box_predictor_test.py │ ├── mask_rcnn_keras_box_predictor.py │ ├── mask_rcnn_keras_box_predictor_test.py │ ├── rfcn_box_predictor.py │ ├── rfcn_box_predictor_test.py │ ├── rfcn_keras_box_predictor.py │ └── rfcn_keras_box_predictor_test.py ├── protos │ ├── __init__.py │ ├── __pycache__ │ │ ├── __init__.cpython-37.pyc │ │ └── string_int_label_map_pb2.cpython-37.pyc │ ├── anchor_generator.proto │ ├── anchor_generator_pb2.py │ ├── argmax_matcher.proto │ ├── argmax_matcher_pb2.py │ ├── bipartite_matcher.proto │ ├── bipartite_matcher_pb2.py │ ├── box_coder.proto │ ├── box_coder_pb2.py │ ├── box_predictor.proto │ ├── box_predictor_pb2.py │ ├── calibration.proto │ ├── calibration_pb2.py │ ├── eval.proto │ ├── eval_pb2.py │ ├── faster_rcnn.proto │ ├── faster_rcnn_box_coder.proto │ ├── faster_rcnn_box_coder_pb2.py │ ├── faster_rcnn_pb2.py │ ├── flexible_grid_anchor_generator.proto │ ├── flexible_grid_anchor_generator_pb2.py │ ├── graph_rewriter.proto │ ├── graph_rewriter_pb2.py │ ├── grid_anchor_generator.proto │ ├── grid_anchor_generator_pb2.py │ ├── hyperparams.proto │ ├── hyperparams_pb2.py │ ├── image_resizer.proto │ ├── image_resizer_pb2.py │ ├── input_reader.proto │ ├── input_reader_pb2.py │ ├── keypoint_box_coder.proto │ ├── keypoint_box_coder_pb2.py │ ├── losses.proto │ ├── losses_pb2.py │ ├── matcher.proto │ ├── matcher_pb2.py │ ├── mean_stddev_box_coder.proto │ ├── mean_stddev_box_coder_pb2.py │ ├── model.proto │ ├── model_pb2.py │ ├── multiscale_anchor_generator.proto │ ├── multiscale_anchor_generator_pb2.py │ ├── optimizer.proto │ ├── optimizer_pb2.py │ ├── pipeline.proto │ ├── pipeline_pb2.py │ ├── post_processing.proto │ ├── post_processing_pb2.py │ ├── preprocessor.proto │ ├── preprocessor_pb2.py │ ├── region_similarity_calculator.proto │ ├── region_similarity_calculator_pb2.py │ ├── square_box_coder.proto │ ├── square_box_coder_pb2.py │ ├── ssd.proto │ ├── ssd_anchor_generator.proto │ ├── ssd_anchor_generator_pb2.py │ ├── ssd_pb2.py │ ├── string_int_label_map.proto │ ├── string_int_label_map_pb2.py │ ├── target_assigner.proto │ ├── train.proto │ └── train_pb2.py ├── samples │ ├── cloud │ │ └── cloud.yml │ └── configs │ │ ├── embedded_ssd_mobilenet_v1_coco.config │ │ ├── facessd_mobilenet_v2_quantized_320x320_open_image_v4.config │ │ ├── faster_rcnn_inception_resnet_v2_atrous_coco.config │ │ ├── faster_rcnn_inception_resnet_v2_atrous_cosine_lr_coco.config │ │ ├── faster_rcnn_inception_resnet_v2_atrous_oid.config │ │ ├── faster_rcnn_inception_resnet_v2_atrous_oid_v4.config │ │ ├── faster_rcnn_inception_resnet_v2_atrous_pets.config │ │ ├── faster_rcnn_inception_v2_coco.config │ │ ├── faster_rcnn_inception_v2_pets.config │ │ ├── faster_rcnn_nas_coco.config │ │ ├── faster_rcnn_resnet101_atrous_coco.config │ │ ├── faster_rcnn_resnet101_ava_v2.1.config │ │ ├── faster_rcnn_resnet101_coco.config │ │ ├── faster_rcnn_resnet101_fgvc.config │ │ ├── faster_rcnn_resnet101_kitti.config │ │ ├── faster_rcnn_resnet101_pets.config │ │ ├── faster_rcnn_resnet101_voc07.config │ │ ├── faster_rcnn_resnet152_coco.config │ │ ├── faster_rcnn_resnet152_pets.config │ │ ├── faster_rcnn_resnet50_coco.config │ │ ├── faster_rcnn_resnet50_fgvc.config │ │ ├── faster_rcnn_resnet50_pets.config │ │ ├── mask_rcnn_inception_resnet_v2_atrous_coco.config │ │ ├── mask_rcnn_inception_v2_coco.config │ │ ├── mask_rcnn_resnet101_atrous_coco.config │ │ ├── mask_rcnn_resnet101_pets.config │ │ ├── mask_rcnn_resnet50_atrous_coco.config │ │ ├── rfcn_resnet101_coco.config │ │ ├── rfcn_resnet101_pets.config │ │ ├── ssd_inception_v2_coco.config │ │ ├── ssd_inception_v2_pets.config │ │ ├── ssd_inception_v3_pets.config │ │ ├── ssd_mobilenet_v1_0.75_depth_300x300_coco14_sync.config │ │ ├── ssd_mobilenet_v1_0.75_depth_quantized_300x300_coco14_sync.config │ │ ├── ssd_mobilenet_v1_0.75_depth_quantized_300x300_pets_sync.config │ │ ├── ssd_mobilenet_v1_300x300_coco14_sync.config │ │ ├── ssd_mobilenet_v1_coco.config │ │ ├── ssd_mobilenet_v1_focal_loss_pets.config │ │ ├── ssd_mobilenet_v1_focal_loss_pets_inference.config │ │ ├── ssd_mobilenet_v1_fpn_shared_box_predictor_640x640_coco14_sync.config │ │ ├── ssd_mobilenet_v1_pets.config │ │ ├── ssd_mobilenet_v1_ppn_shared_box_predictor_300x300_coco14_sync.config │ │ ├── ssd_mobilenet_v1_quantized_300x300_coco14_sync.config │ │ ├── ssd_mobilenet_v2_coco.config │ │ ├── ssd_mobilenet_v2_fpnlite_quantized_shared_box_predictor_256x256_depthmultiplier_75_coco14_sync.config │ │ ├── ssd_mobilenet_v2_fullyconv_coco.config │ │ ├── ssd_mobilenet_v2_oid_v4.config │ │ ├── ssd_mobilenet_v2_pets_keras.config │ │ ├── ssd_mobilenet_v2_quantized_300x300_coco.config │ │ ├── ssd_resnet101_v1_fpn_shared_box_predictor_oid_512x512_sync.config │ │ ├── ssd_resnet50_v1_fpn_shared_box_predictor_640x640_coco14_sync.config │ │ ├── ssdlite_mobilenet_edgetpu_320x320_coco.config │ │ ├── ssdlite_mobilenet_edgetpu_320x320_coco_quant.config │ │ ├── ssdlite_mobilenet_v1_coco.config │ │ ├── ssdlite_mobilenet_v2_coco.config │ │ ├── ssdlite_mobilenet_v3_large_320x320_coco.config │ │ └── ssdlite_mobilenet_v3_small_320x320_coco.config ├── test_data │ └── pets_examples.record ├── test_images │ ├── image1.jpg │ ├── image2.jpg │ └── image_info.txt ├── tpu_exporters │ ├── __init__.py │ ├── export_saved_model_tpu.py │ ├── export_saved_model_tpu_lib.py │ ├── export_saved_model_tpu_lib_test.py │ ├── faster_rcnn.py │ ├── ssd.py │ ├── testdata │ │ ├── __init__.py │ │ ├── faster_rcnn │ │ │ └── faster_rcnn_resnet101_atrous_coco.config │ │ └── ssd │ │ │ └── ssd_pipeline.config │ ├── utils.py │ └── utils_test.py └── utils │ ├── __init__.py │ ├── __pycache__ │ ├── __init__.cpython-37.pyc │ ├── label_map_util.cpython-37.pyc │ ├── ops.cpython-37.pyc │ ├── shape_utils.cpython-37.pyc │ ├── spatial_transform_ops.cpython-37.pyc │ ├── static_shape.cpython-37.pyc │ └── visualization_utils.cpython-37.pyc │ ├── autoaugment_utils.py │ ├── category_util.py │ ├── category_util_test.py │ ├── config_util.py │ ├── config_util_test.py │ ├── context_manager.py │ ├── context_manager_test.py │ ├── dataset_util.py │ ├── dataset_util_test.py │ ├── json_utils.py │ ├── json_utils_test.py │ ├── label_map_util.py │ ├── label_map_util_test.py │ ├── learning_schedules.py │ ├── learning_schedules_test.py │ ├── metrics.py │ ├── metrics_test.py │ ├── model_util.py │ ├── model_util_test.py │ ├── np_box_list.py │ ├── np_box_list_ops.py │ ├── np_box_list_ops_test.py │ ├── np_box_list_test.py │ ├── np_box_mask_list.py │ ├── np_box_mask_list_ops.py │ ├── np_box_mask_list_ops_test.py │ ├── np_box_mask_list_test.py │ ├── np_box_ops.py │ ├── np_box_ops_test.py │ ├── np_mask_ops.py │ ├── np_mask_ops_test.py │ ├── object_detection_evaluation.py │ ├── object_detection_evaluation_test.py │ ├── ops.py │ ├── ops_test.py │ ├── patch_ops.py │ ├── patch_ops_test.py │ ├── per_image_evaluation.py │ ├── per_image_evaluation_test.py │ ├── per_image_vrd_evaluation.py │ ├── per_image_vrd_evaluation_test.py │ ├── shape_utils.py │ ├── shape_utils_test.py │ ├── spatial_transform_ops.py │ ├── spatial_transform_ops_test.py │ ├── static_shape.py │ ├── static_shape_test.py │ ├── test_case.py │ ├── test_utils.py │ ├── test_utils_test.py │ ├── variables_helper.py │ ├── variables_helper_test.py │ ├── visualization_utils.py │ ├── visualization_utils_test.py │ ├── vrd_evaluation.py │ └── vrd_evaluation_test.py ├── output └── img.xml ├── readme_images ├── 1.PNG ├── 2.PNG └── 3.PNG ├── test.py ├── test_images ├── 1.jpg ├── 10.jpg ├── 11.jpg ├── 12.jpg ├── 13.jpg ├── 14.jpg ├── 16.jpg ├── 2.jpg ├── 5.jpg ├── 6.jpg ├── 7.jpg └── 8.jpeg └── train.py /.gitignore: -------------------------------------------------------------------------------- 1 | models/ -------------------------------------------------------------------------------- /Resolute_AI_Assignment_reference_ML.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/Resolute_AI_Assignment_reference_ML.pdf -------------------------------------------------------------------------------- /__pycache__/get_inference.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/__pycache__/get_inference.cpython-37.pyc -------------------------------------------------------------------------------- /__pycache__/visualization_utils.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/__pycache__/visualization_utils.cpython-37.pyc -------------------------------------------------------------------------------- /object_detection/.idea/ResoluteAI.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /object_detection/.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /object_detection/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /object_detection/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /object_detection/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to the Tensorflow Object Detection API 2 | 3 | Patches to Tensorflow Object Detection API are welcome! 4 | 5 | We require contributors to fill out either the individual or corporate 6 | Contributor License Agreement (CLA). 7 | 8 | * If you are an individual writing original source code and you're sure you own the intellectual property, then you'll need to sign an [individual CLA](http://code.google.com/legal/individual-cla-v1.0.html). 9 | * If you work for a company that wants to allow you to contribute your work, then you'll need to sign a [corporate CLA](http://code.google.com/legal/corporate-cla-v1.0.html). 10 | 11 | Please follow the 12 | [Tensorflow contributing guidelines](https://github.com/tensorflow/tensorflow/blob/master/CONTRIBUTING.md) 13 | when submitting pull requests. 14 | -------------------------------------------------------------------------------- /object_detection/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/__init__.py -------------------------------------------------------------------------------- /object_detection/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /object_detection/anchor_generators/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/anchor_generators/__init__.py -------------------------------------------------------------------------------- /object_detection/box_coders/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/box_coders/__init__.py -------------------------------------------------------------------------------- /object_detection/box_coders/mean_stddev_box_coder.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Mean stddev box coder. 17 | 18 | This box coder use the following coding schema to encode boxes: 19 | rel_code = (box_corner - anchor_corner_mean) / anchor_corner_stddev. 20 | """ 21 | from object_detection.core import box_coder 22 | from object_detection.core import box_list 23 | 24 | 25 | class MeanStddevBoxCoder(box_coder.BoxCoder): 26 | """Mean stddev box coder.""" 27 | 28 | def __init__(self, stddev=0.01): 29 | """Constructor for MeanStddevBoxCoder. 30 | 31 | Args: 32 | stddev: The standard deviation used to encode and decode boxes. 33 | """ 34 | self._stddev = stddev 35 | 36 | @property 37 | def code_size(self): 38 | return 4 39 | 40 | def _encode(self, boxes, anchors): 41 | """Encode a box collection with respect to anchor collection. 42 | 43 | Args: 44 | boxes: BoxList holding N boxes to be encoded. 45 | anchors: BoxList of N anchors. 46 | 47 | Returns: 48 | a tensor representing N anchor-encoded boxes 49 | 50 | Raises: 51 | ValueError: if the anchors still have deprecated stddev field. 52 | """ 53 | box_corners = boxes.get() 54 | if anchors.has_field('stddev'): 55 | raise ValueError("'stddev' is a parameter of MeanStddevBoxCoder and " 56 | "should not be specified in the box list.") 57 | means = anchors.get() 58 | return (box_corners - means) / self._stddev 59 | 60 | def _decode(self, rel_codes, anchors): 61 | """Decode. 62 | 63 | Args: 64 | rel_codes: a tensor representing N anchor-encoded boxes. 65 | anchors: BoxList of anchors. 66 | 67 | Returns: 68 | boxes: BoxList holding N bounding boxes 69 | 70 | Raises: 71 | ValueError: if the anchors still have deprecated stddev field and expects 72 | the decode method to use stddev value from that field. 73 | """ 74 | means = anchors.get() 75 | if anchors.has_field('stddev'): 76 | raise ValueError("'stddev' is a parameter of MeanStddevBoxCoder and " 77 | "should not be specified in the box list.") 78 | box_corners = rel_codes * self._stddev + means 79 | return box_list.BoxList(box_corners) 80 | -------------------------------------------------------------------------------- /object_detection/box_coders/mean_stddev_box_coder_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for object_detection.box_coder.mean_stddev_boxcoder.""" 17 | 18 | import tensorflow as tf 19 | 20 | from object_detection.box_coders import mean_stddev_box_coder 21 | from object_detection.core import box_list 22 | 23 | 24 | class MeanStddevBoxCoderTest(tf.test.TestCase): 25 | 26 | def testGetCorrectRelativeCodesAfterEncoding(self): 27 | box_corners = [[0.0, 0.0, 0.5, 0.5], [0.0, 0.0, 0.5, 0.5]] 28 | boxes = box_list.BoxList(tf.constant(box_corners)) 29 | expected_rel_codes = [[0.0, 0.0, 0.0, 0.0], [-5.0, -5.0, -5.0, -3.0]] 30 | prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 1.0, 0.8]]) 31 | priors = box_list.BoxList(prior_means) 32 | 33 | coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1) 34 | rel_codes = coder.encode(boxes, priors) 35 | with self.test_session() as sess: 36 | rel_codes_out = sess.run(rel_codes) 37 | self.assertAllClose(rel_codes_out, expected_rel_codes) 38 | 39 | def testGetCorrectBoxesAfterDecoding(self): 40 | rel_codes = tf.constant([[0.0, 0.0, 0.0, 0.0], [-5.0, -5.0, -5.0, -3.0]]) 41 | expected_box_corners = [[0.0, 0.0, 0.5, 0.5], [0.0, 0.0, 0.5, 0.5]] 42 | prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 1.0, 0.8]]) 43 | priors = box_list.BoxList(prior_means) 44 | 45 | coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1) 46 | decoded_boxes = coder.decode(rel_codes, priors) 47 | decoded_box_corners = decoded_boxes.get() 48 | with self.test_session() as sess: 49 | decoded_out = sess.run(decoded_box_corners) 50 | self.assertAllClose(decoded_out, expected_box_corners) 51 | 52 | 53 | if __name__ == '__main__': 54 | tf.test.main() 55 | -------------------------------------------------------------------------------- /object_detection/builders/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/builders/__init__.py -------------------------------------------------------------------------------- /object_detection/builders/box_coder_builder.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """A function to build an object detection box coder from configuration.""" 17 | from object_detection.box_coders import faster_rcnn_box_coder 18 | from object_detection.box_coders import keypoint_box_coder 19 | from object_detection.box_coders import mean_stddev_box_coder 20 | from object_detection.box_coders import square_box_coder 21 | from object_detection.protos import box_coder_pb2 22 | 23 | 24 | def build(box_coder_config): 25 | """Builds a box coder object based on the box coder config. 26 | 27 | Args: 28 | box_coder_config: A box_coder.proto object containing the config for the 29 | desired box coder. 30 | 31 | Returns: 32 | BoxCoder based on the config. 33 | 34 | Raises: 35 | ValueError: On empty box coder proto. 36 | """ 37 | if not isinstance(box_coder_config, box_coder_pb2.BoxCoder): 38 | raise ValueError('box_coder_config not of type box_coder_pb2.BoxCoder.') 39 | 40 | if box_coder_config.WhichOneof('box_coder_oneof') == 'faster_rcnn_box_coder': 41 | return faster_rcnn_box_coder.FasterRcnnBoxCoder(scale_factors=[ 42 | box_coder_config.faster_rcnn_box_coder.y_scale, 43 | box_coder_config.faster_rcnn_box_coder.x_scale, 44 | box_coder_config.faster_rcnn_box_coder.height_scale, 45 | box_coder_config.faster_rcnn_box_coder.width_scale 46 | ]) 47 | if box_coder_config.WhichOneof('box_coder_oneof') == 'keypoint_box_coder': 48 | return keypoint_box_coder.KeypointBoxCoder( 49 | box_coder_config.keypoint_box_coder.num_keypoints, 50 | scale_factors=[ 51 | box_coder_config.keypoint_box_coder.y_scale, 52 | box_coder_config.keypoint_box_coder.x_scale, 53 | box_coder_config.keypoint_box_coder.height_scale, 54 | box_coder_config.keypoint_box_coder.width_scale 55 | ]) 56 | if (box_coder_config.WhichOneof('box_coder_oneof') == 57 | 'mean_stddev_box_coder'): 58 | return mean_stddev_box_coder.MeanStddevBoxCoder( 59 | stddev=box_coder_config.mean_stddev_box_coder.stddev) 60 | if box_coder_config.WhichOneof('box_coder_oneof') == 'square_box_coder': 61 | return square_box_coder.SquareBoxCoder(scale_factors=[ 62 | box_coder_config.square_box_coder.y_scale, 63 | box_coder_config.square_box_coder.x_scale, 64 | box_coder_config.square_box_coder.length_scale 65 | ]) 66 | raise ValueError('Empty box coder.') 67 | -------------------------------------------------------------------------------- /object_detection/builders/graph_rewriter_builder.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Functions for quantized training and evaluation.""" 16 | 17 | import tensorflow as tf 18 | 19 | 20 | def build(graph_rewriter_config, is_training): 21 | """Returns a function that modifies default graph based on options. 22 | 23 | Args: 24 | graph_rewriter_config: graph_rewriter_pb2.GraphRewriter proto. 25 | is_training: whether in training of eval mode. 26 | """ 27 | def graph_rewrite_fn(): 28 | """Function to quantize weights and activation of the default graph.""" 29 | if (graph_rewriter_config.quantization.weight_bits != 8 or 30 | graph_rewriter_config.quantization.activation_bits != 8): 31 | raise ValueError('Only 8bit quantization is supported') 32 | 33 | # Quantize the graph by inserting quantize ops for weights and activations 34 | if is_training: 35 | tf.contrib.quantize.experimental_create_training_graph( 36 | input_graph=tf.get_default_graph(), 37 | quant_delay=graph_rewriter_config.quantization.delay 38 | ) 39 | else: 40 | tf.contrib.quantize.experimental_create_eval_graph( 41 | input_graph=tf.get_default_graph() 42 | ) 43 | 44 | tf.contrib.layers.summarize_collection('quant_vars') 45 | return graph_rewrite_fn 46 | -------------------------------------------------------------------------------- /object_detection/builders/graph_rewriter_builder_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Tests for graph_rewriter_builder.""" 16 | import mock 17 | import tensorflow as tf 18 | from object_detection.builders import graph_rewriter_builder 19 | from object_detection.protos import graph_rewriter_pb2 20 | 21 | 22 | class QuantizationBuilderTest(tf.test.TestCase): 23 | 24 | def testQuantizationBuilderSetsUpCorrectTrainArguments(self): 25 | with mock.patch.object( 26 | tf.contrib.quantize, 27 | 'experimental_create_training_graph') as mock_quant_fn: 28 | with mock.patch.object(tf.contrib.layers, 29 | 'summarize_collection') as mock_summarize_col: 30 | graph_rewriter_proto = graph_rewriter_pb2.GraphRewriter() 31 | graph_rewriter_proto.quantization.delay = 10 32 | graph_rewriter_proto.quantization.weight_bits = 8 33 | graph_rewriter_proto.quantization.activation_bits = 8 34 | graph_rewrite_fn = graph_rewriter_builder.build( 35 | graph_rewriter_proto, is_training=True) 36 | graph_rewrite_fn() 37 | _, kwargs = mock_quant_fn.call_args 38 | self.assertEqual(kwargs['input_graph'], tf.get_default_graph()) 39 | self.assertEqual(kwargs['quant_delay'], 10) 40 | mock_summarize_col.assert_called_with('quant_vars') 41 | 42 | def testQuantizationBuilderSetsUpCorrectEvalArguments(self): 43 | with mock.patch.object(tf.contrib.quantize, 44 | 'experimental_create_eval_graph') as mock_quant_fn: 45 | with mock.patch.object(tf.contrib.layers, 46 | 'summarize_collection') as mock_summarize_col: 47 | graph_rewriter_proto = graph_rewriter_pb2.GraphRewriter() 48 | graph_rewriter_proto.quantization.delay = 10 49 | graph_rewrite_fn = graph_rewriter_builder.build( 50 | graph_rewriter_proto, is_training=False) 51 | graph_rewrite_fn() 52 | _, kwargs = mock_quant_fn.call_args 53 | self.assertEqual(kwargs['input_graph'], tf.get_default_graph()) 54 | mock_summarize_col.assert_called_with('quant_vars') 55 | 56 | 57 | if __name__ == '__main__': 58 | tf.test.main() 59 | -------------------------------------------------------------------------------- /object_detection/builders/input_reader_builder.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Input reader builder. 17 | 18 | Creates data sources for DetectionModels from an InputReader config. See 19 | input_reader.proto for options. 20 | 21 | Note: If users wishes to also use their own InputReaders with the Object 22 | Detection configuration framework, they should define their own builder function 23 | that wraps the build function. 24 | """ 25 | 26 | import tensorflow as tf 27 | 28 | from object_detection.data_decoders import tf_example_decoder 29 | from object_detection.protos import input_reader_pb2 30 | 31 | parallel_reader = tf.contrib.slim.parallel_reader 32 | 33 | 34 | def build(input_reader_config): 35 | """Builds a tensor dictionary based on the InputReader config. 36 | 37 | Args: 38 | input_reader_config: A input_reader_pb2.InputReader object. 39 | 40 | Returns: 41 | A tensor dict based on the input_reader_config. 42 | 43 | Raises: 44 | ValueError: On invalid input reader proto. 45 | ValueError: If no input paths are specified. 46 | """ 47 | if not isinstance(input_reader_config, input_reader_pb2.InputReader): 48 | raise ValueError('input_reader_config not of type ' 49 | 'input_reader_pb2.InputReader.') 50 | 51 | if input_reader_config.WhichOneof('input_reader') == 'tf_record_input_reader': 52 | config = input_reader_config.tf_record_input_reader 53 | if not config.input_path: 54 | raise ValueError('At least one input path must be specified in ' 55 | '`input_reader_config`.') 56 | _, string_tensor = parallel_reader.parallel_read( 57 | config.input_path[:], # Convert `RepeatedScalarContainer` to list. 58 | reader_class=tf.TFRecordReader, 59 | num_epochs=(input_reader_config.num_epochs 60 | if input_reader_config.num_epochs else None), 61 | num_readers=input_reader_config.num_readers, 62 | shuffle=input_reader_config.shuffle, 63 | dtypes=[tf.string, tf.string], 64 | capacity=input_reader_config.queue_capacity, 65 | min_after_dequeue=input_reader_config.min_after_dequeue) 66 | 67 | label_map_proto_file = None 68 | if input_reader_config.HasField('label_map_path'): 69 | label_map_proto_file = input_reader_config.label_map_path 70 | decoder = tf_example_decoder.TfExampleDecoder( 71 | load_instance_masks=input_reader_config.load_instance_masks, 72 | instance_mask_type=input_reader_config.mask_type, 73 | label_map_proto_file=label_map_proto_file) 74 | return decoder.decode(string_tensor) 75 | 76 | raise ValueError('Unsupported input_reader_config.') 77 | -------------------------------------------------------------------------------- /object_detection/builders/matcher_builder.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """A function to build an object detection matcher from configuration.""" 17 | 18 | from object_detection.matchers import argmax_matcher 19 | from object_detection.matchers import bipartite_matcher 20 | from object_detection.protos import matcher_pb2 21 | 22 | 23 | def build(matcher_config): 24 | """Builds a matcher object based on the matcher config. 25 | 26 | Args: 27 | matcher_config: A matcher.proto object containing the config for the desired 28 | Matcher. 29 | 30 | Returns: 31 | Matcher based on the config. 32 | 33 | Raises: 34 | ValueError: On empty matcher proto. 35 | """ 36 | if not isinstance(matcher_config, matcher_pb2.Matcher): 37 | raise ValueError('matcher_config not of type matcher_pb2.Matcher.') 38 | if matcher_config.WhichOneof('matcher_oneof') == 'argmax_matcher': 39 | matcher = matcher_config.argmax_matcher 40 | matched_threshold = unmatched_threshold = None 41 | if not matcher.ignore_thresholds: 42 | matched_threshold = matcher.matched_threshold 43 | unmatched_threshold = matcher.unmatched_threshold 44 | return argmax_matcher.ArgMaxMatcher( 45 | matched_threshold=matched_threshold, 46 | unmatched_threshold=unmatched_threshold, 47 | negatives_lower_than_unmatched=matcher.negatives_lower_than_unmatched, 48 | force_match_for_each_row=matcher.force_match_for_each_row, 49 | use_matmul_gather=matcher.use_matmul_gather) 50 | if matcher_config.WhichOneof('matcher_oneof') == 'bipartite_matcher': 51 | matcher = matcher_config.bipartite_matcher 52 | return bipartite_matcher.GreedyBipartiteMatcher(matcher.use_matmul_gather) 53 | raise ValueError('Empty matcher.') 54 | -------------------------------------------------------------------------------- /object_detection/builders/region_similarity_calculator_builder.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Builder for region similarity calculators.""" 17 | 18 | from object_detection.core import region_similarity_calculator 19 | from object_detection.protos import region_similarity_calculator_pb2 20 | 21 | 22 | def build(region_similarity_calculator_config): 23 | """Builds region similarity calculator based on the configuration. 24 | 25 | Builds one of [IouSimilarity, IoaSimilarity, NegSqDistSimilarity] objects. See 26 | core/region_similarity_calculator.proto for details. 27 | 28 | Args: 29 | region_similarity_calculator_config: RegionSimilarityCalculator 30 | configuration proto. 31 | 32 | Returns: 33 | region_similarity_calculator: RegionSimilarityCalculator object. 34 | 35 | Raises: 36 | ValueError: On unknown region similarity calculator. 37 | """ 38 | 39 | if not isinstance( 40 | region_similarity_calculator_config, 41 | region_similarity_calculator_pb2.RegionSimilarityCalculator): 42 | raise ValueError( 43 | 'region_similarity_calculator_config not of type ' 44 | 'region_similarity_calculator_pb2.RegionsSimilarityCalculator') 45 | 46 | similarity_calculator = region_similarity_calculator_config.WhichOneof( 47 | 'region_similarity') 48 | if similarity_calculator == 'iou_similarity': 49 | return region_similarity_calculator.IouSimilarity() 50 | if similarity_calculator == 'ioa_similarity': 51 | return region_similarity_calculator.IoaSimilarity() 52 | if similarity_calculator == 'neg_sq_dist_similarity': 53 | return region_similarity_calculator.NegSqDistSimilarity() 54 | if similarity_calculator == 'thresholded_iou_similarity': 55 | return region_similarity_calculator.ThresholdedIouSimilarity( 56 | region_similarity_calculator_config.thresholded_iou_similarity 57 | .iou_threshold) 58 | 59 | raise ValueError('Unknown region similarity calculator.') 60 | -------------------------------------------------------------------------------- /object_detection/builders/region_similarity_calculator_builder_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for region_similarity_calculator_builder.""" 17 | 18 | import tensorflow as tf 19 | 20 | from google.protobuf import text_format 21 | from object_detection.builders import region_similarity_calculator_builder 22 | from object_detection.core import region_similarity_calculator 23 | from object_detection.protos import region_similarity_calculator_pb2 as sim_calc_pb2 24 | 25 | 26 | class RegionSimilarityCalculatorBuilderTest(tf.test.TestCase): 27 | 28 | def testBuildIoaSimilarityCalculator(self): 29 | similarity_calc_text_proto = """ 30 | ioa_similarity { 31 | } 32 | """ 33 | similarity_calc_proto = sim_calc_pb2.RegionSimilarityCalculator() 34 | text_format.Merge(similarity_calc_text_proto, similarity_calc_proto) 35 | similarity_calc = region_similarity_calculator_builder.build( 36 | similarity_calc_proto) 37 | self.assertTrue(isinstance(similarity_calc, 38 | region_similarity_calculator.IoaSimilarity)) 39 | 40 | def testBuildIouSimilarityCalculator(self): 41 | similarity_calc_text_proto = """ 42 | iou_similarity { 43 | } 44 | """ 45 | similarity_calc_proto = sim_calc_pb2.RegionSimilarityCalculator() 46 | text_format.Merge(similarity_calc_text_proto, similarity_calc_proto) 47 | similarity_calc = region_similarity_calculator_builder.build( 48 | similarity_calc_proto) 49 | self.assertTrue(isinstance(similarity_calc, 50 | region_similarity_calculator.IouSimilarity)) 51 | 52 | def testBuildNegSqDistSimilarityCalculator(self): 53 | similarity_calc_text_proto = """ 54 | neg_sq_dist_similarity { 55 | } 56 | """ 57 | similarity_calc_proto = sim_calc_pb2.RegionSimilarityCalculator() 58 | text_format.Merge(similarity_calc_text_proto, similarity_calc_proto) 59 | similarity_calc = region_similarity_calculator_builder.build( 60 | similarity_calc_proto) 61 | self.assertTrue(isinstance(similarity_calc, 62 | region_similarity_calculator. 63 | NegSqDistSimilarity)) 64 | 65 | 66 | if __name__ == '__main__': 67 | tf.test.main() 68 | -------------------------------------------------------------------------------- /object_detection/builders/target_assigner_builder.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """A function to build an object detection box coder from configuration.""" 17 | from object_detection.builders import box_coder_builder 18 | from object_detection.builders import matcher_builder 19 | from object_detection.builders import region_similarity_calculator_builder 20 | from object_detection.core import target_assigner 21 | 22 | 23 | def build(target_assigner_config): 24 | """Builds a TargetAssigner object based on the config. 25 | 26 | Args: 27 | target_assigner_config: A target_assigner proto message containing config 28 | for the desired target assigner. 29 | 30 | Returns: 31 | TargetAssigner object based on the config. 32 | """ 33 | matcher_instance = matcher_builder.build(target_assigner_config.matcher) 34 | similarity_calc_instance = region_similarity_calculator_builder.build( 35 | target_assigner_config.similarity_calculator) 36 | box_coder = box_coder_builder.build(target_assigner_config.box_coder) 37 | return target_assigner.TargetAssigner( 38 | matcher=matcher_instance, 39 | similarity_calc=similarity_calc_instance, 40 | box_coder_instance=box_coder) 41 | -------------------------------------------------------------------------------- /object_detection/builders/target_assigner_builder_test.py: -------------------------------------------------------------------------------- 1 | """Tests for google3.third_party.tensorflow_models.object_detection.builders.target_assigner_builder.""" 2 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | 17 | import tensorflow as tf 18 | 19 | from google.protobuf import text_format 20 | 21 | 22 | from object_detection.builders import target_assigner_builder 23 | from object_detection.core import target_assigner 24 | from object_detection.protos import target_assigner_pb2 25 | 26 | 27 | class TargetAssignerBuilderTest(tf.test.TestCase): 28 | 29 | def test_build_a_target_assigner(self): 30 | target_assigner_text_proto = """ 31 | matcher { 32 | argmax_matcher {matched_threshold: 0.5} 33 | } 34 | similarity_calculator { 35 | iou_similarity {} 36 | } 37 | box_coder { 38 | faster_rcnn_box_coder {} 39 | } 40 | """ 41 | target_assigner_proto = target_assigner_pb2.TargetAssigner() 42 | text_format.Merge(target_assigner_text_proto, target_assigner_proto) 43 | target_assigner_instance = target_assigner_builder.build( 44 | target_assigner_proto) 45 | self.assertIsInstance(target_assigner_instance, 46 | target_assigner.TargetAssigner) 47 | 48 | 49 | if __name__ == '__main__': 50 | tf.test.main() 51 | -------------------------------------------------------------------------------- /object_detection/core/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /object_detection/core/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/core/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /object_detection/core/__pycache__/standard_fields.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/core/__pycache__/standard_fields.cpython-37.pyc -------------------------------------------------------------------------------- /object_detection/core/box_coder_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for object_detection.core.box_coder.""" 17 | 18 | import tensorflow as tf 19 | 20 | from object_detection.core import box_coder 21 | from object_detection.core import box_list 22 | 23 | 24 | class MockBoxCoder(box_coder.BoxCoder): 25 | """Test BoxCoder that encodes/decodes using the multiply-by-two function.""" 26 | 27 | def code_size(self): 28 | return 4 29 | 30 | def _encode(self, boxes, anchors): 31 | return 2.0 * boxes.get() 32 | 33 | def _decode(self, rel_codes, anchors): 34 | return box_list.BoxList(rel_codes / 2.0) 35 | 36 | 37 | class BoxCoderTest(tf.test.TestCase): 38 | 39 | def test_batch_decode(self): 40 | mock_anchor_corners = tf.constant( 41 | [[0, 0.1, 0.2, 0.3], [0.2, 0.4, 0.4, 0.6]], tf.float32) 42 | mock_anchors = box_list.BoxList(mock_anchor_corners) 43 | mock_box_coder = MockBoxCoder() 44 | 45 | expected_boxes = [[[0.0, 0.1, 0.5, 0.6], [0.5, 0.6, 0.7, 0.8]], 46 | [[0.1, 0.2, 0.3, 0.4], [0.7, 0.8, 0.9, 1.0]]] 47 | 48 | encoded_boxes_list = [mock_box_coder.encode( 49 | box_list.BoxList(tf.constant(boxes)), mock_anchors) 50 | for boxes in expected_boxes] 51 | encoded_boxes = tf.stack(encoded_boxes_list) 52 | decoded_boxes = box_coder.batch_decode( 53 | encoded_boxes, mock_box_coder, mock_anchors) 54 | 55 | with self.test_session() as sess: 56 | decoded_boxes_result = sess.run(decoded_boxes) 57 | self.assertAllClose(expected_boxes, decoded_boxes_result) 58 | 59 | 60 | if __name__ == '__main__': 61 | tf.test.main() 62 | -------------------------------------------------------------------------------- /object_detection/core/data_decoder.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Interface for data decoders. 17 | 18 | Data decoders decode the input data and return a dictionary of tensors keyed by 19 | the entries in core.reader.Fields. 20 | """ 21 | from __future__ import absolute_import 22 | from __future__ import division 23 | from __future__ import print_function 24 | from abc import ABCMeta 25 | from abc import abstractmethod 26 | import six 27 | 28 | 29 | class DataDecoder(six.with_metaclass(ABCMeta, object)): 30 | """Interface for data decoders.""" 31 | 32 | @abstractmethod 33 | def decode(self, data): 34 | """Return a single image and associated labels. 35 | 36 | Args: 37 | data: a string tensor holding a serialized protocol buffer corresponding 38 | to data for a single image. 39 | 40 | Returns: 41 | tensor_dict: a dictionary containing tensors. Possible keys are defined in 42 | reader.Fields. 43 | """ 44 | pass 45 | -------------------------------------------------------------------------------- /object_detection/core/data_parser.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Interface for data parsers. 16 | 17 | Data parser parses input data and returns a dictionary of numpy arrays 18 | keyed by the entries in standard_fields.py. Since the parser parses records 19 | to numpy arrays (materialized tensors) directly, it is used to read data for 20 | evaluation/visualization; to parse the data during training, DataDecoder should 21 | be used. 22 | """ 23 | from __future__ import absolute_import 24 | from __future__ import division 25 | from __future__ import print_function 26 | from abc import ABCMeta 27 | from abc import abstractmethod 28 | import six 29 | 30 | 31 | class DataToNumpyParser(six.with_metaclass(ABCMeta, object)): 32 | """Abstract interface for data parser that produces numpy arrays.""" 33 | 34 | @abstractmethod 35 | def parse(self, input_data): 36 | """Parses input and returns a numpy array or a dictionary of numpy arrays. 37 | 38 | Args: 39 | input_data: an input data 40 | 41 | Returns: 42 | A numpy array or a dictionary of numpy arrays or None, if input 43 | cannot be parsed. 44 | """ 45 | pass 46 | -------------------------------------------------------------------------------- /object_detection/core/freezable_batch_norm.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """A freezable batch norm layer that uses Keras batch normalization.""" 17 | import tensorflow as tf 18 | 19 | 20 | class FreezableBatchNorm(tf.keras.layers.BatchNormalization): 21 | """Batch normalization layer (Ioffe and Szegedy, 2014). 22 | 23 | This is a `freezable` batch norm layer that supports setting the `training` 24 | parameter in the __init__ method rather than having to set it either via 25 | the Keras learning phase or via the `call` method parameter. This layer will 26 | forward all other parameters to the default Keras `BatchNormalization` 27 | layer 28 | 29 | This is class is necessary because Object Detection model training sometimes 30 | requires batch normalization layers to be `frozen` and used as if it was 31 | evaluation time, despite still training (and potentially using dropout layers) 32 | 33 | Like the default Keras BatchNormalization layer, this will normalize the 34 | activations of the previous layer at each batch, 35 | i.e. applies a transformation that maintains the mean activation 36 | close to 0 and the activation standard deviation close to 1. 37 | 38 | Arguments: 39 | training: If False, the layer will normalize using the moving average and 40 | std. dev, without updating the learned avg and std. dev. 41 | If None or True, the layer will follow the keras BatchNormalization layer 42 | strategy of checking the Keras learning phase at `call` time to decide 43 | what to do. 44 | **kwargs: The keyword arguments to forward to the keras BatchNormalization 45 | layer constructor. 46 | 47 | Input shape: 48 | Arbitrary. Use the keyword argument `input_shape` 49 | (tuple of integers, does not include the samples axis) 50 | when using this layer as the first layer in a model. 51 | 52 | Output shape: 53 | Same shape as input. 54 | 55 | References: 56 | - [Batch Normalization: Accelerating Deep Network Training by Reducing 57 | Internal Covariate Shift](https://arxiv.org/abs/1502.03167) 58 | """ 59 | 60 | def __init__(self, training=None, **kwargs): 61 | super(FreezableBatchNorm, self).__init__(**kwargs) 62 | self._training = training 63 | 64 | def call(self, inputs, training=None): 65 | # Override the call arg only if the batchnorm is frozen. (Ignore None) 66 | if self._training is False: # pylint: disable=g-bool-id-comparison 67 | training = self._training 68 | return super(FreezableBatchNorm, self).call(inputs, training=training) 69 | -------------------------------------------------------------------------------- /object_detection/core/minibatch_sampler.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Base minibatch sampler module. 17 | 18 | The job of the minibatch_sampler is to subsample a minibatch based on some 19 | criterion. 20 | 21 | The main function call is: 22 | subsample(indicator, batch_size, **params). 23 | Indicator is a 1d boolean tensor where True denotes which examples can be 24 | sampled. It returns a boolean indicator where True denotes an example has been 25 | sampled.. 26 | 27 | Subclasses should implement the Subsample function and can make use of the 28 | @staticmethod SubsampleIndicator. 29 | """ 30 | 31 | from __future__ import absolute_import 32 | from __future__ import division 33 | from __future__ import print_function 34 | 35 | from abc import ABCMeta 36 | from abc import abstractmethod 37 | 38 | import six 39 | import tensorflow as tf 40 | 41 | from object_detection.utils import ops 42 | 43 | 44 | class MinibatchSampler(six.with_metaclass(ABCMeta, object)): 45 | """Abstract base class for subsampling minibatches.""" 46 | 47 | def __init__(self): 48 | """Constructs a minibatch sampler.""" 49 | pass 50 | 51 | @abstractmethod 52 | def subsample(self, indicator, batch_size, **params): 53 | """Returns subsample of entries in indicator. 54 | 55 | Args: 56 | indicator: boolean tensor of shape [N] whose True entries can be sampled. 57 | batch_size: desired batch size. 58 | **params: additional keyword arguments for specific implementations of 59 | the MinibatchSampler. 60 | 61 | Returns: 62 | sample_indicator: boolean tensor of shape [N] whose True entries have been 63 | sampled. If sum(indicator) >= batch_size, sum(is_sampled) = batch_size 64 | """ 65 | pass 66 | 67 | @staticmethod 68 | def subsample_indicator(indicator, num_samples): 69 | """Subsample indicator vector. 70 | 71 | Given a boolean indicator vector with M elements set to `True`, the function 72 | assigns all but `num_samples` of these previously `True` elements to 73 | `False`. If `num_samples` is greater than M, the original indicator vector 74 | is returned. 75 | 76 | Args: 77 | indicator: a 1-dimensional boolean tensor indicating which elements 78 | are allowed to be sampled and which are not. 79 | num_samples: int32 scalar tensor 80 | 81 | Returns: 82 | a boolean tensor with the same shape as input (indicator) tensor 83 | """ 84 | indices = tf.where(indicator) 85 | indices = tf.random_shuffle(indices) 86 | indices = tf.reshape(indices, [-1]) 87 | 88 | num_samples = tf.minimum(tf.size(indices), num_samples) 89 | selected_indices = tf.slice(indices, [0], tf.reshape(num_samples, [1])) 90 | 91 | selected_indicator = ops.indices_to_dense_vector(selected_indices, 92 | tf.shape(indicator)[0]) 93 | 94 | return tf.equal(selected_indicator, 1) 95 | -------------------------------------------------------------------------------- /object_detection/core/minibatch_sampler_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for google3.research.vale.object_detection.minibatch_sampler.""" 17 | 18 | import numpy as np 19 | import tensorflow as tf 20 | 21 | from object_detection.core import minibatch_sampler 22 | 23 | 24 | class MinibatchSamplerTest(tf.test.TestCase): 25 | 26 | def test_subsample_indicator_when_more_true_elements_than_num_samples(self): 27 | np_indicator = [True, False, True, False, True, True, False] 28 | indicator = tf.constant(np_indicator) 29 | samples = minibatch_sampler.MinibatchSampler.subsample_indicator( 30 | indicator, 3) 31 | with self.test_session() as sess: 32 | samples_out = sess.run(samples) 33 | self.assertTrue(np.sum(samples_out), 3) 34 | self.assertAllEqual(samples_out, 35 | np.logical_and(samples_out, np_indicator)) 36 | 37 | def test_subsample_when_more_true_elements_than_num_samples_no_shape(self): 38 | np_indicator = [True, False, True, False, True, True, False] 39 | indicator = tf.placeholder(tf.bool) 40 | feed_dict = {indicator: np_indicator} 41 | 42 | samples = minibatch_sampler.MinibatchSampler.subsample_indicator( 43 | indicator, 3) 44 | with self.test_session() as sess: 45 | samples_out = sess.run(samples, feed_dict=feed_dict) 46 | self.assertTrue(np.sum(samples_out), 3) 47 | self.assertAllEqual(samples_out, 48 | np.logical_and(samples_out, np_indicator)) 49 | 50 | def test_subsample_indicator_when_less_true_elements_than_num_samples(self): 51 | np_indicator = [True, False, True, False, True, True, False] 52 | indicator = tf.constant(np_indicator) 53 | samples = minibatch_sampler.MinibatchSampler.subsample_indicator( 54 | indicator, 5) 55 | with self.test_session() as sess: 56 | samples_out = sess.run(samples) 57 | self.assertTrue(np.sum(samples_out), 4) 58 | self.assertAllEqual(samples_out, 59 | np.logical_and(samples_out, np_indicator)) 60 | 61 | def test_subsample_indicator_when_num_samples_is_zero(self): 62 | np_indicator = [True, False, True, False, True, True, False] 63 | indicator = tf.constant(np_indicator) 64 | samples_none = minibatch_sampler.MinibatchSampler.subsample_indicator( 65 | indicator, 0) 66 | with self.test_session() as sess: 67 | samples_none_out = sess.run(samples_none) 68 | self.assertAllEqual( 69 | np.zeros_like(samples_none_out, dtype=bool), 70 | samples_none_out) 71 | 72 | def test_subsample_indicator_when_indicator_all_false(self): 73 | indicator_empty = tf.zeros([0], dtype=tf.bool) 74 | samples_empty = minibatch_sampler.MinibatchSampler.subsample_indicator( 75 | indicator_empty, 4) 76 | with self.test_session() as sess: 77 | samples_empty_out = sess.run(samples_empty) 78 | self.assertEqual(0, samples_empty_out.size) 79 | 80 | 81 | if __name__ == '__main__': 82 | tf.test.main() 83 | -------------------------------------------------------------------------------- /object_detection/core/prefetcher.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Provides functions to prefetch tensors to feed into models.""" 17 | import tensorflow as tf 18 | 19 | 20 | def prefetch(tensor_dict, capacity): 21 | """Creates a prefetch queue for tensors. 22 | 23 | Creates a FIFO queue to asynchronously enqueue tensor_dicts and returns a 24 | dequeue op that evaluates to a tensor_dict. This function is useful in 25 | prefetching preprocessed tensors so that the data is readily available for 26 | consumers. 27 | 28 | Example input pipeline when you don't need batching: 29 | ---------------------------------------------------- 30 | key, string_tensor = slim.parallel_reader.parallel_read(...) 31 | tensor_dict = decoder.decode(string_tensor) 32 | tensor_dict = preprocessor.preprocess(tensor_dict, ...) 33 | prefetch_queue = prefetcher.prefetch(tensor_dict, capacity=20) 34 | tensor_dict = prefetch_queue.dequeue() 35 | outputs = Model(tensor_dict) 36 | ... 37 | ---------------------------------------------------- 38 | 39 | For input pipelines with batching, refer to core/batcher.py 40 | 41 | Args: 42 | tensor_dict: a dictionary of tensors to prefetch. 43 | capacity: the size of the prefetch queue. 44 | 45 | Returns: 46 | a FIFO prefetcher queue 47 | """ 48 | names = list(tensor_dict.keys()) 49 | dtypes = [t.dtype for t in tensor_dict.values()] 50 | shapes = [t.get_shape() for t in tensor_dict.values()] 51 | prefetch_queue = tf.PaddingFIFOQueue(capacity, dtypes=dtypes, 52 | shapes=shapes, 53 | names=names, 54 | name='prefetch_queue') 55 | enqueue_op = prefetch_queue.enqueue(tensor_dict) 56 | tf.train.queue_runner.add_queue_runner(tf.train.queue_runner.QueueRunner( 57 | prefetch_queue, [enqueue_op])) 58 | tf.summary.scalar( 59 | 'queue/%s/fraction_of_%d_full' % (prefetch_queue.name, capacity), 60 | tf.cast(prefetch_queue.size(), dtype=tf.float32) * (1. / capacity)) 61 | return prefetch_queue 62 | -------------------------------------------------------------------------------- /object_detection/data/face_label_map.pbtxt: -------------------------------------------------------------------------------- 1 | item { 2 | name: "face" 3 | id: 1 4 | display_name: "face" 5 | } 6 | 7 | -------------------------------------------------------------------------------- /object_detection/data/kitti_label_map.pbtxt: -------------------------------------------------------------------------------- 1 | item { 2 | id: 1 3 | name: 'car' 4 | } 5 | 6 | item { 7 | id: 2 8 | name: 'pedestrian' 9 | } 10 | -------------------------------------------------------------------------------- /object_detection/data/pascal_label_map.pbtxt: -------------------------------------------------------------------------------- 1 | item { 2 | id: 1 3 | name: 'aeroplane' 4 | } 5 | 6 | item { 7 | id: 2 8 | name: 'bicycle' 9 | } 10 | 11 | item { 12 | id: 3 13 | name: 'bird' 14 | } 15 | 16 | item { 17 | id: 4 18 | name: 'boat' 19 | } 20 | 21 | item { 22 | id: 5 23 | name: 'bottle' 24 | } 25 | 26 | item { 27 | id: 6 28 | name: 'bus' 29 | } 30 | 31 | item { 32 | id: 7 33 | name: 'car' 34 | } 35 | 36 | item { 37 | id: 8 38 | name: 'cat' 39 | } 40 | 41 | item { 42 | id: 9 43 | name: 'chair' 44 | } 45 | 46 | item { 47 | id: 10 48 | name: 'cow' 49 | } 50 | 51 | item { 52 | id: 11 53 | name: 'diningtable' 54 | } 55 | 56 | item { 57 | id: 12 58 | name: 'dog' 59 | } 60 | 61 | item { 62 | id: 13 63 | name: 'horse' 64 | } 65 | 66 | item { 67 | id: 14 68 | name: 'motorbike' 69 | } 70 | 71 | item { 72 | id: 15 73 | name: 'person' 74 | } 75 | 76 | item { 77 | id: 16 78 | name: 'pottedplant' 79 | } 80 | 81 | item { 82 | id: 17 83 | name: 'sheep' 84 | } 85 | 86 | item { 87 | id: 18 88 | name: 'sofa' 89 | } 90 | 91 | item { 92 | id: 19 93 | name: 'train' 94 | } 95 | 96 | item { 97 | id: 20 98 | name: 'tvmonitor' 99 | } 100 | -------------------------------------------------------------------------------- /object_detection/data/pet_label_map.pbtxt: -------------------------------------------------------------------------------- 1 | item { 2 | id: 1 3 | name: 'Abyssinian' 4 | } 5 | 6 | item { 7 | id: 2 8 | name: 'american_bulldog' 9 | } 10 | 11 | item { 12 | id: 3 13 | name: 'american_pit_bull_terrier' 14 | } 15 | 16 | item { 17 | id: 4 18 | name: 'basset_hound' 19 | } 20 | 21 | item { 22 | id: 5 23 | name: 'beagle' 24 | } 25 | 26 | item { 27 | id: 6 28 | name: 'Bengal' 29 | } 30 | 31 | item { 32 | id: 7 33 | name: 'Birman' 34 | } 35 | 36 | item { 37 | id: 8 38 | name: 'Bombay' 39 | } 40 | 41 | item { 42 | id: 9 43 | name: 'boxer' 44 | } 45 | 46 | item { 47 | id: 10 48 | name: 'British_Shorthair' 49 | } 50 | 51 | item { 52 | id: 11 53 | name: 'chihuahua' 54 | } 55 | 56 | item { 57 | id: 12 58 | name: 'Egyptian_Mau' 59 | } 60 | 61 | item { 62 | id: 13 63 | name: 'english_cocker_spaniel' 64 | } 65 | 66 | item { 67 | id: 14 68 | name: 'english_setter' 69 | } 70 | 71 | item { 72 | id: 15 73 | name: 'german_shorthaired' 74 | } 75 | 76 | item { 77 | id: 16 78 | name: 'great_pyrenees' 79 | } 80 | 81 | item { 82 | id: 17 83 | name: 'havanese' 84 | } 85 | 86 | item { 87 | id: 18 88 | name: 'japanese_chin' 89 | } 90 | 91 | item { 92 | id: 19 93 | name: 'keeshond' 94 | } 95 | 96 | item { 97 | id: 20 98 | name: 'leonberger' 99 | } 100 | 101 | item { 102 | id: 21 103 | name: 'Maine_Coon' 104 | } 105 | 106 | item { 107 | id: 22 108 | name: 'miniature_pinscher' 109 | } 110 | 111 | item { 112 | id: 23 113 | name: 'newfoundland' 114 | } 115 | 116 | item { 117 | id: 24 118 | name: 'Persian' 119 | } 120 | 121 | item { 122 | id: 25 123 | name: 'pomeranian' 124 | } 125 | 126 | item { 127 | id: 26 128 | name: 'pug' 129 | } 130 | 131 | item { 132 | id: 27 133 | name: 'Ragdoll' 134 | } 135 | 136 | item { 137 | id: 28 138 | name: 'Russian_Blue' 139 | } 140 | 141 | item { 142 | id: 29 143 | name: 'saint_bernard' 144 | } 145 | 146 | item { 147 | id: 30 148 | name: 'samoyed' 149 | } 150 | 151 | item { 152 | id: 31 153 | name: 'scottish_terrier' 154 | } 155 | 156 | item { 157 | id: 32 158 | name: 'shiba_inu' 159 | } 160 | 161 | item { 162 | id: 33 163 | name: 'Siamese' 164 | } 165 | 166 | item { 167 | id: 34 168 | name: 'Sphynx' 169 | } 170 | 171 | item { 172 | id: 35 173 | name: 'staffordshire_bull_terrier' 174 | } 175 | 176 | item { 177 | id: 36 178 | name: 'wheaten_terrier' 179 | } 180 | 181 | item { 182 | id: 37 183 | name: 'yorkshire_terrier' 184 | } 185 | -------------------------------------------------------------------------------- /object_detection/data_decoders/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/data_decoders/__init__.py -------------------------------------------------------------------------------- /object_detection/dataset_tools/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/dataset_tools/__init__.py -------------------------------------------------------------------------------- /object_detection/dataset_tools/create_pycocotools_package.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2018 The TensorFlow Authors. All Rights Reserved. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================== 16 | 17 | # Script to download pycocotools and make package for CMLE jobs. 18 | # 19 | # usage: 20 | # bash object_detection/dataset_tools/create_pycocotools_package.sh \ 21 | # /tmp/pycocotools 22 | set -e 23 | 24 | if [ -z "$1" ]; then 25 | echo "usage create_pycocotools_package.sh [output dir]" 26 | exit 27 | fi 28 | 29 | # Create the output directory. 30 | OUTPUT_DIR="${1%/}" 31 | SCRATCH_DIR="${OUTPUT_DIR}/raw" 32 | mkdir -p "${OUTPUT_DIR}" 33 | mkdir -p "${SCRATCH_DIR}" 34 | 35 | cd ${SCRATCH_DIR} 36 | git clone https://github.com/cocodataset/cocoapi.git 37 | cd cocoapi/PythonAPI && mv ../common ./ 38 | 39 | sed "s/\.\.\/common/common/g" setup.py > setup.py.updated 40 | cp -f setup.py.updated setup.py 41 | rm setup.py.updated 42 | 43 | sed "s/\.\.\/common/common/g" pycocotools/_mask.pyx > _mask.pyx.updated 44 | cp -f _mask.pyx.updated pycocotools/_mask.pyx 45 | rm _mask.pyx.updated 46 | 47 | sed "s/import matplotlib\.pyplot as plt/import matplotlib;matplotlib\.use\(\'Agg\'\);import matplotlib\.pyplot as plt/g" pycocotools/coco.py > coco.py.updated 48 | cp -f coco.py.updated pycocotools/coco.py 49 | rm coco.py.updated 50 | 51 | cd "${OUTPUT_DIR}" 52 | tar -czf pycocotools-2.0.tar.gz -C "${SCRATCH_DIR}/cocoapi/" PythonAPI/ 53 | rm -rf ${SCRATCH_DIR} 54 | -------------------------------------------------------------------------------- /object_detection/dataset_tools/tf_record_creation_util.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | r"""Utilities for creating TFRecords of TF examples for the Open Images dataset. 16 | """ 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | import tensorflow as tf 22 | 23 | 24 | def open_sharded_output_tfrecords(exit_stack, base_path, num_shards): 25 | """Opens all TFRecord shards for writing and adds them to an exit stack. 26 | 27 | Args: 28 | exit_stack: A context2.ExitStack used to automatically closed the TFRecords 29 | opened in this function. 30 | base_path: The base path for all shards 31 | num_shards: The number of shards 32 | 33 | Returns: 34 | The list of opened TFRecords. Position k in the list corresponds to shard k. 35 | """ 36 | tf_record_output_filenames = [ 37 | '{}-{:05d}-of-{:05d}'.format(base_path, idx, num_shards) 38 | for idx in range(num_shards) 39 | ] 40 | 41 | tfrecords = [ 42 | exit_stack.enter_context(tf.python_io.TFRecordWriter(file_name)) 43 | for file_name in tf_record_output_filenames 44 | ] 45 | 46 | return tfrecords 47 | -------------------------------------------------------------------------------- /object_detection/dataset_tools/tf_record_creation_util_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Tests for tf_record_creation_util.py.""" 16 | 17 | import os 18 | import contextlib2 19 | import tensorflow as tf 20 | 21 | from object_detection.dataset_tools import tf_record_creation_util 22 | 23 | 24 | class OpenOutputTfrecordsTests(tf.test.TestCase): 25 | 26 | def test_sharded_tfrecord_writes(self): 27 | with contextlib2.ExitStack() as tf_record_close_stack: 28 | output_tfrecords = tf_record_creation_util.open_sharded_output_tfrecords( 29 | tf_record_close_stack, 30 | os.path.join(tf.test.get_temp_dir(), 'test.tfrec'), 10) 31 | for idx in range(10): 32 | output_tfrecords[idx].write('test_{}'.format(idx)) 33 | 34 | for idx in range(10): 35 | tf_record_path = '{}-{:05d}-of-00010'.format( 36 | os.path.join(tf.test.get_temp_dir(), 'test.tfrec'), idx) 37 | records = list(tf.python_io.tf_record_iterator(tf_record_path)) 38 | self.assertAllEqual(records, ['test_{}'.format(idx)]) 39 | 40 | 41 | if __name__ == '__main__': 42 | tf.test.main() 43 | -------------------------------------------------------------------------------- /object_detection/dockerfiles/android/README.md: -------------------------------------------------------------------------------- 1 | # Dockerfile for the TPU and TensorFlow Lite Object Detection tutorial 2 | 3 | This Docker image automates the setup involved with training 4 | object detection models on Google Cloud and building the Android TensorFlow Lite 5 | demo app. We recommend using this container if you decide to work through our 6 | tutorial on ["Training and serving a real-time mobile object detector in 7 | 30 minutes with Cloud TPUs"](https://medium.com/tensorflow/training-and-serving-a-realtime-mobile-object-detector-in-30-minutes-with-cloud-tpus-b78971cf1193), though of course it may be useful even if you would 8 | like to use the Object Detection API outside the context of the tutorial. 9 | 10 | A couple words of warning: 11 | 12 | 1. Docker containers do not have persistent storage. This means that any changes 13 | you make to files inside the container will not persist if you restart 14 | the container. When running through the tutorial, 15 | **do not close the container**. 16 | 2. To be able to deploy the [Android app]( 17 | https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/android) 18 | (which you will build at the end of the tutorial), 19 | you will need to kill any instances of `adb` running on the host machine. You 20 | can accomplish this by closing all instances of Android Studio, and then 21 | running `adb kill-server`. 22 | 23 | You can install Docker by following the [instructions here]( 24 | https://docs.docker.com/install/). 25 | 26 | ## Running The Container 27 | 28 | From this directory, build the Dockerfile as follows (this takes a while): 29 | 30 | ``` 31 | docker build --tag detect-tf . 32 | ``` 33 | 34 | Run the container: 35 | 36 | ``` 37 | docker run --rm -it --privileged -p 6006:6006 detect-tf 38 | ``` 39 | 40 | When running the container, you will find yourself inside the `/tensorflow` 41 | directory, which is the path to the TensorFlow [source 42 | tree](https://github.com/tensorflow/tensorflow). 43 | 44 | ## Text Editing 45 | 46 | The tutorial also 47 | requires you to occasionally edit files inside the source tree. 48 | This Docker images comes with `vim`, `nano`, and `emacs` preinstalled for your 49 | convenience. 50 | 51 | ## What's In This Container 52 | 53 | This container is derived from the nightly build of TensorFlow, and contains the 54 | sources for TensorFlow at `/tensorflow`, as well as the 55 | [TensorFlow Models](https://github.com/tensorflow/models) which are available at 56 | `/tensorflow/models` (and contain the Object Detection API as a subdirectory 57 | at `/tensorflow/models/research/object_detection`). 58 | The Oxford-IIIT Pets dataset, the COCO pre-trained SSD + MobileNet (v1) 59 | checkpoint, and example 60 | trained model are all available in `/tmp` in their respective folders. 61 | 62 | This container also has the `gsutil` and `gcloud` utilities, the `bazel` build 63 | tool, and all dependencies necessary to use the Object Detection API, and 64 | compile and install the TensorFlow Lite Android demo app. 65 | 66 | At various points throughout the tutorial, you may see references to the 67 | *research directory*. This refers to the `research` folder within the 68 | models repository, located at 69 | `/tensorflow/models/resesarch`. 70 | -------------------------------------------------------------------------------- /object_detection/g3doc/exporting_models.md: -------------------------------------------------------------------------------- 1 | # Exporting a trained model for inference 2 | 3 | After your model has been trained, you should export it to a Tensorflow 4 | graph proto. A checkpoint will typically consist of three files: 5 | 6 | * model.ckpt-${CHECKPOINT_NUMBER}.data-00000-of-00001 7 | * model.ckpt-${CHECKPOINT_NUMBER}.index 8 | * model.ckpt-${CHECKPOINT_NUMBER}.meta 9 | 10 | After you've identified a candidate checkpoint to export, run the following 11 | command from tensorflow/models/research: 12 | 13 | ``` bash 14 | # From tensorflow/models/research/ 15 | INPUT_TYPE=image_tensor 16 | PIPELINE_CONFIG_PATH={path to pipeline config file} 17 | TRAINED_CKPT_PREFIX={path to model.ckpt} 18 | EXPORT_DIR={path to folder that will be used for export} 19 | python object_detection/export_inference_graph.py \ 20 | --input_type=${INPUT_TYPE} \ 21 | --pipeline_config_path=${PIPELINE_CONFIG_PATH} \ 22 | --trained_checkpoint_prefix=${TRAINED_CKPT_PREFIX} \ 23 | --output_directory=${EXPORT_DIR} 24 | ``` 25 | 26 | NOTE: We are configuring our exported model to ingest 4-D image tensors. We can 27 | also configure the exported model to take encoded images or serialized 28 | `tf.Example`s. 29 | 30 | After export, you should see the directory ${EXPORT_DIR} containing the following: 31 | 32 | * saved_model/, a directory containing the saved model format of the exported model 33 | * frozen_inference_graph.pb, the frozen graph format of the exported model 34 | * model.ckpt.*, the model checkpoints used for exporting 35 | * checkpoint, a file specifying to restore included checkpoint files 36 | * pipeline.config, pipeline config file for the exported model 37 | -------------------------------------------------------------------------------- /object_detection/g3doc/faq.md: -------------------------------------------------------------------------------- 1 | # Frequently Asked Questions 2 | 3 | ## Q: How can I ensure that all the groundtruth boxes are used during train and eval? 4 | A: For the object detecion framework to be TPU-complient, we must pad our input 5 | tensors to static shapes. This means that we must pad to a fixed number of 6 | bounding boxes, configured by `InputReader.max_number_of_boxes`. It is 7 | important to set this value to a number larger than the maximum number of 8 | groundtruth boxes in the dataset. If an image is encountered with more 9 | bounding boxes, the excess boxes will be clipped. 10 | 11 | ## Q: AttributeError: 'module' object has no attribute 'BackupHandler' 12 | A: This BackupHandler (tf.contrib.slim.tfexample_decoder.BackupHandler) was 13 | introduced in tensorflow 1.5.0 so runing with earlier versions may cause this 14 | issue. It now has been replaced by 15 | object_detection.data_decoders.tf_example_decoder.BackupHandler. Whoever sees 16 | this issue should be able to resolve it by syncing your fork to HEAD. 17 | Same for LookupTensor. 18 | 19 | ## Q: AttributeError: 'module' object has no attribute 'LookupTensor' 20 | A: Similar to BackupHandler, syncing your fork to HEAD should make it work. 21 | 22 | ## Q: Why can't I get the inference time as reported in model zoo? 23 | A: The inference time reported in model zoo is mean time of testing hundreds of 24 | images with an internal machine. As mentioned in 25 | [Tensorflow detection model zoo](detection_model_zoo.md), this speed depends 26 | highly on one's specific hardware configuration and should be treated more as 27 | relative timing. 28 | -------------------------------------------------------------------------------- /object_detection/g3doc/img/dataset_explorer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/g3doc/img/dataset_explorer.png -------------------------------------------------------------------------------- /object_detection/g3doc/img/dogs_detections_output.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/g3doc/img/dogs_detections_output.jpg -------------------------------------------------------------------------------- /object_detection/g3doc/img/example_cat.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/g3doc/img/example_cat.jpg -------------------------------------------------------------------------------- /object_detection/g3doc/img/groupof_case_eval.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/g3doc/img/groupof_case_eval.png -------------------------------------------------------------------------------- /object_detection/g3doc/img/kites_detections_output.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/g3doc/img/kites_detections_output.jpg -------------------------------------------------------------------------------- /object_detection/g3doc/img/kites_with_segment_overlay.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/g3doc/img/kites_with_segment_overlay.png -------------------------------------------------------------------------------- /object_detection/g3doc/img/nongroupof_case_eval.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/g3doc/img/nongroupof_case_eval.png -------------------------------------------------------------------------------- /object_detection/g3doc/img/oid_bus_72e19c28aac34ed8.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/g3doc/img/oid_bus_72e19c28aac34ed8.jpg -------------------------------------------------------------------------------- /object_detection/g3doc/img/oid_monkey_3b4168c89cecbc5b.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/g3doc/img/oid_monkey_3b4168c89cecbc5b.jpg -------------------------------------------------------------------------------- /object_detection/g3doc/img/oxford_pet.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/g3doc/img/oxford_pet.png -------------------------------------------------------------------------------- /object_detection/g3doc/img/tensorboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/g3doc/img/tensorboard.png -------------------------------------------------------------------------------- /object_detection/g3doc/img/tensorboard2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/g3doc/img/tensorboard2.png -------------------------------------------------------------------------------- /object_detection/g3doc/img/tf-od-api-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/g3doc/img/tf-od-api-logo.png -------------------------------------------------------------------------------- /object_detection/g3doc/preparing_inputs.md: -------------------------------------------------------------------------------- 1 | # Preparing Inputs 2 | 3 | Tensorflow Object Detection API reads data using the TFRecord file format. Two 4 | sample scripts (`create_pascal_tf_record.py` and `create_pet_tf_record.py`) are 5 | provided to convert from the PASCAL VOC dataset and Oxford-IIIT Pet dataset to 6 | TFRecords. 7 | 8 | ## Generating the PASCAL VOC TFRecord files. 9 | 10 | The raw 2012 PASCAL VOC data set is located 11 | [here](http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar). 12 | To download, extract and convert it to TFRecords, run the following commands 13 | below: 14 | 15 | ```bash 16 | # From tensorflow/models/research/ 17 | wget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar 18 | tar -xvf VOCtrainval_11-May-2012.tar 19 | python object_detection/dataset_tools/create_pascal_tf_record.py \ 20 | --label_map_path=object_detection/data/pascal_label_map.pbtxt \ 21 | --data_dir=VOCdevkit --year=VOC2012 --set=train \ 22 | --output_path=pascal_train.record 23 | python object_detection/dataset_tools/create_pascal_tf_record.py \ 24 | --label_map_path=object_detection/data/pascal_label_map.pbtxt \ 25 | --data_dir=VOCdevkit --year=VOC2012 --set=val \ 26 | --output_path=pascal_val.record 27 | ``` 28 | 29 | You should end up with two TFRecord files named `pascal_train.record` and 30 | `pascal_val.record` in the `tensorflow/models/research/` directory. 31 | 32 | The label map for the PASCAL VOC data set can be found at 33 | `object_detection/data/pascal_label_map.pbtxt`. 34 | 35 | ## Generating the Oxford-IIIT Pet TFRecord files. 36 | 37 | The Oxford-IIIT Pet data set is located 38 | [here](http://www.robots.ox.ac.uk/~vgg/data/pets/). To download, extract and 39 | convert it to TFRecrods, run the following commands below: 40 | 41 | ```bash 42 | # From tensorflow/models/research/ 43 | wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz 44 | wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz 45 | tar -xvf annotations.tar.gz 46 | tar -xvf images.tar.gz 47 | python object_detection/dataset_tools/create_pet_tf_record.py \ 48 | --label_map_path=object_detection/data/pet_label_map.pbtxt \ 49 | --data_dir=`pwd` \ 50 | --output_dir=`pwd` 51 | ``` 52 | 53 | You should end up with two 10-sharded TFRecord files named 54 | `pet_faces_train.record-?????-of-00010` and 55 | `pet_faces_val.record-?????-of-00010` in the `tensorflow/models/research/` 56 | directory. 57 | 58 | The label map for the Pet dataset can be found at 59 | `object_detection/data/pet_label_map.pbtxt`. 60 | -------------------------------------------------------------------------------- /object_detection/g3doc/running_locally.md: -------------------------------------------------------------------------------- 1 | # Running Locally 2 | 3 | This page walks through the steps required to train an object detection model 4 | on a local machine. It assumes the reader has completed the 5 | following prerequisites: 6 | 7 | 1. The Tensorflow Object Detection API has been installed as documented in the 8 | [installation instructions](installation.md). This includes installing library 9 | dependencies, compiling the configuration protobufs and setting up the Python 10 | environment. 11 | 2. A valid data set has been created. See [this page](preparing_inputs.md) for 12 | instructions on how to generate a dataset for the PASCAL VOC challenge or the 13 | Oxford-IIIT Pet dataset. 14 | 3. A Object Detection pipeline configuration has been written. See 15 | [this page](configuring_jobs.md) for details on how to write a pipeline configuration. 16 | 17 | ## Recommended Directory Structure for Training and Evaluation 18 | 19 | ``` 20 | +data 21 | -label_map file 22 | -train TFRecord file 23 | -eval TFRecord file 24 | +models 25 | + model 26 | -pipeline config file 27 | +train 28 | +eval 29 | ``` 30 | 31 | ## Running the Training Job 32 | 33 | A local training job can be run with the following command: 34 | 35 | ```bash 36 | # From the tensorflow/models/research/ directory 37 | PIPELINE_CONFIG_PATH={path to pipeline config file} 38 | MODEL_DIR={path to model directory} 39 | NUM_TRAIN_STEPS=50000 40 | SAMPLE_1_OF_N_EVAL_EXAMPLES=1 41 | python object_detection/model_main.py \ 42 | --pipeline_config_path=${PIPELINE_CONFIG_PATH} \ 43 | --model_dir=${MODEL_DIR} \ 44 | --num_train_steps=${NUM_TRAIN_STEPS} \ 45 | --sample_1_of_n_eval_examples=$SAMPLE_1_OF_N_EVAL_EXAMPLES \ 46 | --alsologtostderr 47 | ``` 48 | 49 | where `${PIPELINE_CONFIG_PATH}` points to the pipeline config and 50 | `${MODEL_DIR}` points to the directory in which training checkpoints 51 | and events will be written to. Note that this binary will interleave both 52 | training and evaluation. 53 | 54 | ## Running Tensorboard 55 | 56 | Progress for training and eval jobs can be inspected using Tensorboard. If 57 | using the recommended directory structure, Tensorboard can be run using the 58 | following command: 59 | 60 | ```bash 61 | tensorboard --logdir=${MODEL_DIR} 62 | ``` 63 | 64 | where `${MODEL_DIR}` points to the directory that contains the 65 | train and eval directories. Please note it may take Tensorboard a couple minutes 66 | to populate with data. 67 | -------------------------------------------------------------------------------- /object_detection/g3doc/running_notebook.md: -------------------------------------------------------------------------------- 1 | # Quick Start: Jupyter notebook for off-the-shelf inference 2 | 3 | If you'd like to hit the ground running and run detection on a few example 4 | images right out of the box, we recommend trying out the Jupyter notebook demo. 5 | To run the Jupyter notebook, run the following command from 6 | `tensorflow/models/research/object_detection`: 7 | 8 | ``` 9 | # From tensorflow/models/research/object_detection 10 | jupyter notebook 11 | ``` 12 | 13 | The notebook should open in your favorite web browser. Click the 14 | [`object_detection_tutorial.ipynb`](../object_detection_tutorial.ipynb) link to 15 | open the demo. 16 | -------------------------------------------------------------------------------- /object_detection/g3doc/tpu_exporters.md: -------------------------------------------------------------------------------- 1 | # Object Detection TPU Inference Exporter 2 | 3 | This package contains SavedModel Exporter for TPU Inference of object detection 4 | models. 5 | 6 | ## Usage 7 | 8 | This Exporter is intended for users who have trained models with CPUs / GPUs, 9 | but would like to use them for inference on TPU without changing their code or 10 | re-training their models. 11 | 12 | Users are assumed to have: 13 | 14 | + `PIPELINE_CONFIG`: A pipeline_pb2.TrainEvalPipelineConfig config file; 15 | + `CHECKPOINT`: A model checkpoint trained on any device; 16 | 17 | and need to correctly set: 18 | 19 | + `EXPORT_DIR`: Path to export SavedModel; 20 | + `INPUT_PLACEHOLDER`: Name of input placeholder in model's signature_def_map; 21 | + `INPUT_TYPE`: Type of input node, which can be one of 'image_tensor', 22 | 'encoded_image_string_tensor', or 'tf_example'; 23 | + `USE_BFLOAT16`: Whether to use bfloat16 instead of float32 on TPU. 24 | 25 | The model can be exported with: 26 | 27 | ``` 28 | python object_detection/tpu_exporters/export_saved_model_tpu.py \ 29 | --pipeline_config_file= \ 30 | --ckpt_path= \ 31 | --export_dir= \ 32 | --input_placeholder_name= \ 33 | --input_type= \ 34 | --use_bfloat16= 35 | ``` 36 | -------------------------------------------------------------------------------- /object_detection/icons/download.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/icons/download.jpg -------------------------------------------------------------------------------- /object_detection/icons/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/icons/logo.png -------------------------------------------------------------------------------- /object_detection/inference/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/inference/__init__.py -------------------------------------------------------------------------------- /object_detection/legacy/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/legacy/__init__.py -------------------------------------------------------------------------------- /object_detection/matchers/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/matchers/__init__.py -------------------------------------------------------------------------------- /object_detection/matchers/bipartite_matcher.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Bipartite matcher implementation.""" 17 | 18 | import tensorflow as tf 19 | 20 | from tensorflow.contrib.image.python.ops import image_ops 21 | from object_detection.core import matcher 22 | 23 | 24 | class GreedyBipartiteMatcher(matcher.Matcher): 25 | """Wraps a Tensorflow greedy bipartite matcher.""" 26 | 27 | def __init__(self, use_matmul_gather=False): 28 | """Constructs a Matcher. 29 | 30 | Args: 31 | use_matmul_gather: Force constructed match objects to use matrix 32 | multiplication based gather instead of standard tf.gather. 33 | (Default: False). 34 | """ 35 | super(GreedyBipartiteMatcher, self).__init__( 36 | use_matmul_gather=use_matmul_gather) 37 | 38 | def _match(self, similarity_matrix, valid_rows): 39 | """Bipartite matches a collection rows and columns. A greedy bi-partite. 40 | 41 | TODO(rathodv): Add num_valid_columns options to match only that many columns 42 | with all the rows. 43 | 44 | Args: 45 | similarity_matrix: Float tensor of shape [N, M] with pairwise similarity 46 | where higher values mean more similar. 47 | valid_rows: A boolean tensor of shape [N] indicating the rows that are 48 | valid. 49 | 50 | Returns: 51 | match_results: int32 tensor of shape [M] with match_results[i]=-1 52 | meaning that column i is not matched and otherwise that it is matched to 53 | row match_results[i]. 54 | """ 55 | valid_row_sim_matrix = tf.gather(similarity_matrix, 56 | tf.squeeze(tf.where(valid_rows), axis=-1)) 57 | invalid_row_sim_matrix = tf.gather( 58 | similarity_matrix, 59 | tf.squeeze(tf.where(tf.logical_not(valid_rows)), axis=-1)) 60 | similarity_matrix = tf.concat( 61 | [valid_row_sim_matrix, invalid_row_sim_matrix], axis=0) 62 | # Convert similarity matrix to distance matrix as tf.image.bipartite tries 63 | # to find minimum distance matches. 64 | distance_matrix = -1 * similarity_matrix 65 | num_valid_rows = tf.reduce_sum(tf.cast(valid_rows, dtype=tf.float32)) 66 | _, match_results = image_ops.bipartite_match( 67 | distance_matrix, num_valid_rows=num_valid_rows) 68 | match_results = tf.reshape(match_results, [-1]) 69 | match_results = tf.cast(match_results, tf.int32) 70 | return match_results 71 | -------------------------------------------------------------------------------- /object_detection/matchers/bipartite_matcher_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for object_detection.core.bipartite_matcher.""" 17 | 18 | import tensorflow as tf 19 | 20 | from object_detection.matchers import bipartite_matcher 21 | 22 | 23 | class GreedyBipartiteMatcherTest(tf.test.TestCase): 24 | 25 | def test_get_expected_matches_when_all_rows_are_valid(self): 26 | similarity_matrix = tf.constant([[0.50, 0.1, 0.8], [0.15, 0.2, 0.3]]) 27 | valid_rows = tf.ones([2], dtype=tf.bool) 28 | expected_match_results = [-1, 1, 0] 29 | 30 | matcher = bipartite_matcher.GreedyBipartiteMatcher() 31 | match = matcher.match(similarity_matrix, valid_rows=valid_rows) 32 | with self.test_session() as sess: 33 | match_results_out = sess.run(match._match_results) 34 | self.assertAllEqual(match_results_out, expected_match_results) 35 | 36 | def test_get_expected_matches_with_all_rows_be_default(self): 37 | similarity_matrix = tf.constant([[0.50, 0.1, 0.8], [0.15, 0.2, 0.3]]) 38 | expected_match_results = [-1, 1, 0] 39 | 40 | matcher = bipartite_matcher.GreedyBipartiteMatcher() 41 | match = matcher.match(similarity_matrix) 42 | with self.test_session() as sess: 43 | match_results_out = sess.run(match._match_results) 44 | self.assertAllEqual(match_results_out, expected_match_results) 45 | 46 | def test_get_no_matches_with_zero_valid_rows(self): 47 | similarity_matrix = tf.constant([[0.50, 0.1, 0.8], [0.15, 0.2, 0.3]]) 48 | valid_rows = tf.zeros([2], dtype=tf.bool) 49 | expected_match_results = [-1, -1, -1] 50 | 51 | matcher = bipartite_matcher.GreedyBipartiteMatcher() 52 | match = matcher.match(similarity_matrix, valid_rows) 53 | with self.test_session() as sess: 54 | match_results_out = sess.run(match._match_results) 55 | self.assertAllEqual(match_results_out, expected_match_results) 56 | 57 | def test_get_expected_matches_with_only_one_valid_row(self): 58 | similarity_matrix = tf.constant([[0.50, 0.1, 0.8], [0.15, 0.2, 0.3]]) 59 | valid_rows = tf.constant([True, False], dtype=tf.bool) 60 | expected_match_results = [-1, -1, 0] 61 | 62 | matcher = bipartite_matcher.GreedyBipartiteMatcher() 63 | match = matcher.match(similarity_matrix, valid_rows) 64 | with self.test_session() as sess: 65 | match_results_out = sess.run(match._match_results) 66 | self.assertAllEqual(match_results_out, expected_match_results) 67 | 68 | def test_get_expected_matches_with_only_one_valid_row_at_bottom(self): 69 | similarity_matrix = tf.constant([[0.15, 0.2, 0.3], [0.50, 0.1, 0.8]]) 70 | valid_rows = tf.constant([False, True], dtype=tf.bool) 71 | expected_match_results = [-1, -1, 0] 72 | 73 | matcher = bipartite_matcher.GreedyBipartiteMatcher() 74 | match = matcher.match(similarity_matrix, valid_rows) 75 | with self.test_session() as sess: 76 | match_results_out = sess.run(match._match_results) 77 | self.assertAllEqual(match_results_out, expected_match_results) 78 | 79 | 80 | if __name__ == '__main__': 81 | tf.test.main() 82 | -------------------------------------------------------------------------------- /object_detection/meta_architectures/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/meta_architectures/__init__.py -------------------------------------------------------------------------------- /object_detection/meta_architectures/rfcn_meta_arch_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for object_detection.meta_architectures.rfcn_meta_arch.""" 17 | 18 | import tensorflow as tf 19 | 20 | from object_detection.meta_architectures import faster_rcnn_meta_arch_test_lib 21 | from object_detection.meta_architectures import rfcn_meta_arch 22 | 23 | 24 | class RFCNMetaArchTest( 25 | faster_rcnn_meta_arch_test_lib.FasterRCNNMetaArchTestBase): 26 | 27 | def _get_second_stage_box_predictor_text_proto( 28 | self, share_box_across_classes=False): 29 | del share_box_across_classes 30 | box_predictor_text_proto = """ 31 | rfcn_box_predictor { 32 | conv_hyperparams { 33 | op: CONV 34 | activation: NONE 35 | regularizer { 36 | l2_regularizer { 37 | weight: 0.0005 38 | } 39 | } 40 | initializer { 41 | variance_scaling_initializer { 42 | factor: 1.0 43 | uniform: true 44 | mode: FAN_AVG 45 | } 46 | } 47 | } 48 | } 49 | """ 50 | return box_predictor_text_proto 51 | 52 | def _get_model(self, box_predictor, **common_kwargs): 53 | return rfcn_meta_arch.RFCNMetaArch( 54 | second_stage_rfcn_box_predictor=box_predictor, **common_kwargs) 55 | 56 | def _get_box_classifier_features_shape(self, 57 | image_size, 58 | batch_size, 59 | max_num_proposals, 60 | initial_crop_size, 61 | maxpool_stride, 62 | num_features): 63 | return (batch_size, image_size, image_size, num_features) 64 | 65 | 66 | if __name__ == '__main__': 67 | tf.test.main() 68 | -------------------------------------------------------------------------------- /object_detection/metrics/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/metrics/__init__.py -------------------------------------------------------------------------------- /object_detection/metrics/io_utils.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Common IO utils used in offline metric computation. 16 | """ 17 | 18 | from __future__ import absolute_import 19 | from __future__ import division 20 | from __future__ import print_function 21 | 22 | import csv 23 | 24 | 25 | def write_csv(fid, metrics): 26 | """Writes metrics key-value pairs to CSV file. 27 | 28 | Args: 29 | fid: File identifier of an opened file. 30 | metrics: A dictionary with metrics to be written. 31 | """ 32 | metrics_writer = csv.writer(fid, delimiter=',') 33 | for metric_name, metric_value in metrics.items(): 34 | metrics_writer.writerow([metric_name, str(metric_value)]) 35 | -------------------------------------------------------------------------------- /object_detection/metrics/offline_eval_map_corloc_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Tests for utilities in offline_eval_map_corloc binary.""" 16 | 17 | import tensorflow as tf 18 | 19 | from object_detection.metrics import offline_eval_map_corloc as offline_eval 20 | 21 | 22 | class OfflineEvalMapCorlocTest(tf.test.TestCase): 23 | 24 | def test_generateShardedFilenames(self): 25 | test_filename = '/path/to/file' 26 | result = offline_eval._generate_sharded_filenames(test_filename) 27 | self.assertEqual(result, [test_filename]) 28 | 29 | test_filename = '/path/to/file-00000-of-00050' 30 | result = offline_eval._generate_sharded_filenames(test_filename) 31 | self.assertEqual(result, [test_filename]) 32 | 33 | result = offline_eval._generate_sharded_filenames('/path/to/@3.record') 34 | self.assertEqual(result, [ 35 | '/path/to/-00000-of-00003.record', '/path/to/-00001-of-00003.record', 36 | '/path/to/-00002-of-00003.record' 37 | ]) 38 | 39 | result = offline_eval._generate_sharded_filenames('/path/to/abc@3') 40 | self.assertEqual(result, [ 41 | '/path/to/abc-00000-of-00003', '/path/to/abc-00001-of-00003', 42 | '/path/to/abc-00002-of-00003' 43 | ]) 44 | 45 | result = offline_eval._generate_sharded_filenames('/path/to/@1') 46 | self.assertEqual(result, ['/path/to/-00000-of-00001']) 47 | 48 | def test_generateFilenames(self): 49 | test_filenames = ['/path/to/file', '/path/to/@3.record'] 50 | result = offline_eval._generate_filenames(test_filenames) 51 | self.assertEqual(result, [ 52 | '/path/to/file', '/path/to/-00000-of-00003.record', 53 | '/path/to/-00001-of-00003.record', '/path/to/-00002-of-00003.record' 54 | ]) 55 | 56 | 57 | if __name__ == '__main__': 58 | tf.test.main() 59 | -------------------------------------------------------------------------------- /object_detection/model_hparams.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Hyperparameters for the object detection model in TF.learn. 16 | 17 | This file consolidates and documents the hyperparameters used by the model. 18 | """ 19 | 20 | from __future__ import absolute_import 21 | from __future__ import division 22 | from __future__ import print_function 23 | 24 | import tensorflow as tf 25 | 26 | 27 | def create_hparams(hparams_overrides=None): 28 | """Returns hyperparameters, including any flag value overrides. 29 | 30 | Args: 31 | hparams_overrides: Optional hparams overrides, represented as a 32 | string containing comma-separated hparam_name=value pairs. 33 | 34 | Returns: 35 | The hyperparameters as a tf.HParams object. 36 | """ 37 | hparams = tf.contrib.training.HParams( 38 | # Whether a fine tuning checkpoint (provided in the pipeline config) 39 | # should be loaded for training. 40 | load_pretrained=True) 41 | # Override any of the preceding hyperparameter values. 42 | if hparams_overrides: 43 | hparams = hparams.parse(hparams_overrides) 44 | return hparams 45 | -------------------------------------------------------------------------------- /object_detection/predictors/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/predictors/__init__.py -------------------------------------------------------------------------------- /object_detection/predictors/heads/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/predictors/heads/__init__.py -------------------------------------------------------------------------------- /object_detection/predictors/heads/head.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Base head class. 17 | 18 | All the different kinds of prediction heads in different models will inherit 19 | from this class. What is in common between all head classes is that they have a 20 | `predict` function that receives `features` as its first argument. 21 | 22 | How to add a new prediction head to an existing meta architecture? 23 | For example, how can we add a `3d shape` prediction head to Mask RCNN? 24 | 25 | We have to take the following steps to add a new prediction head to an 26 | existing meta arch: 27 | (a) Add a class for predicting the head. This class should inherit from the 28 | `Head` class below and have a `predict` function that receives the features 29 | and predicts the output. The output is always a tf.float32 tensor. 30 | (b) Add the head to the meta architecture. For example in case of Mask RCNN, 31 | go to box_predictor_builder and put in the logic for adding the new head to the 32 | Mask RCNN box predictor. 33 | (c) Add the logic for computing the loss for the new head. 34 | (d) Add the necessary metrics for the new head. 35 | (e) (optional) Add visualization for the new head. 36 | """ 37 | from abc import abstractmethod 38 | 39 | import tensorflow as tf 40 | 41 | 42 | class Head(object): 43 | """Mask RCNN head base class.""" 44 | 45 | def __init__(self): 46 | """Constructor.""" 47 | pass 48 | 49 | @abstractmethod 50 | def predict(self, features, num_predictions_per_location): 51 | """Returns the head's predictions. 52 | 53 | Args: 54 | features: A float tensor of features. 55 | num_predictions_per_location: Int containing number of predictions per 56 | location. 57 | 58 | Returns: 59 | A tf.float32 tensor. 60 | """ 61 | pass 62 | 63 | 64 | class KerasHead(tf.keras.Model): 65 | """Keras head base class.""" 66 | 67 | def call(self, features): 68 | """The Keras model call will delegate to the `_predict` method.""" 69 | return self._predict(features) 70 | 71 | @abstractmethod 72 | def _predict(self, features): 73 | """Returns the head's predictions. 74 | 75 | Args: 76 | features: A float tensor of features. 77 | 78 | Returns: 79 | A tf.float32 tensor. 80 | """ 81 | pass 82 | -------------------------------------------------------------------------------- /object_detection/predictors/heads/keypoint_head_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for object_detection.predictors.heads.keypoint_head.""" 17 | import tensorflow as tf 18 | 19 | from google.protobuf import text_format 20 | from object_detection.builders import hyperparams_builder 21 | from object_detection.predictors.heads import keypoint_head 22 | from object_detection.protos import hyperparams_pb2 23 | from object_detection.utils import test_case 24 | 25 | 26 | class MaskRCNNKeypointHeadTest(test_case.TestCase): 27 | 28 | def _build_arg_scope_with_hyperparams(self, 29 | op_type=hyperparams_pb2.Hyperparams.FC): 30 | hyperparams = hyperparams_pb2.Hyperparams() 31 | hyperparams_text_proto = """ 32 | activation: NONE 33 | regularizer { 34 | l2_regularizer { 35 | } 36 | } 37 | initializer { 38 | truncated_normal_initializer { 39 | } 40 | } 41 | """ 42 | text_format.Merge(hyperparams_text_proto, hyperparams) 43 | hyperparams.op = op_type 44 | return hyperparams_builder.build(hyperparams, is_training=True) 45 | 46 | def test_prediction_size(self): 47 | keypoint_prediction_head = keypoint_head.MaskRCNNKeypointHead( 48 | conv_hyperparams_fn=self._build_arg_scope_with_hyperparams()) 49 | roi_pooled_features = tf.random_uniform( 50 | [64, 14, 14, 1024], minval=-2.0, maxval=2.0, dtype=tf.float32) 51 | prediction = keypoint_prediction_head.predict( 52 | features=roi_pooled_features, num_predictions_per_location=1) 53 | self.assertAllEqual([64, 1, 17, 56, 56], prediction.get_shape().as_list()) 54 | 55 | 56 | if __name__ == '__main__': 57 | tf.test.main() 58 | -------------------------------------------------------------------------------- /object_detection/predictors/rfcn_box_predictor_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for object_detection.predictors.rfcn_box_predictor.""" 17 | import numpy as np 18 | import tensorflow as tf 19 | 20 | from google.protobuf import text_format 21 | from object_detection.builders import hyperparams_builder 22 | from object_detection.predictors import rfcn_box_predictor as box_predictor 23 | from object_detection.protos import hyperparams_pb2 24 | from object_detection.utils import test_case 25 | 26 | 27 | class RfcnBoxPredictorTest(test_case.TestCase): 28 | 29 | def _build_arg_scope_with_conv_hyperparams(self): 30 | conv_hyperparams = hyperparams_pb2.Hyperparams() 31 | conv_hyperparams_text_proto = """ 32 | regularizer { 33 | l2_regularizer { 34 | } 35 | } 36 | initializer { 37 | truncated_normal_initializer { 38 | } 39 | } 40 | """ 41 | text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) 42 | return hyperparams_builder.build(conv_hyperparams, is_training=True) 43 | 44 | def test_get_correct_box_encoding_and_class_prediction_shapes(self): 45 | 46 | def graph_fn(image_features, proposal_boxes): 47 | rfcn_box_predictor = box_predictor.RfcnBoxPredictor( 48 | is_training=False, 49 | num_classes=2, 50 | conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), 51 | num_spatial_bins=[3, 3], 52 | depth=4, 53 | crop_size=[12, 12], 54 | box_code_size=4 55 | ) 56 | box_predictions = rfcn_box_predictor.predict( 57 | [image_features], num_predictions_per_location=[1], 58 | scope='BoxPredictor', 59 | proposal_boxes=proposal_boxes) 60 | box_encodings = tf.concat( 61 | box_predictions[box_predictor.BOX_ENCODINGS], axis=1) 62 | class_predictions_with_background = tf.concat( 63 | box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], 64 | axis=1) 65 | return (box_encodings, class_predictions_with_background) 66 | 67 | image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) 68 | proposal_boxes = np.random.rand(4, 2, 4).astype(np.float32) 69 | (box_encodings, class_predictions_with_background) = self.execute( 70 | graph_fn, [image_features, proposal_boxes]) 71 | 72 | self.assertAllEqual(box_encodings.shape, [8, 1, 2, 4]) 73 | self.assertAllEqual(class_predictions_with_background.shape, [8, 1, 3]) 74 | 75 | 76 | if __name__ == '__main__': 77 | tf.test.main() 78 | -------------------------------------------------------------------------------- /object_detection/predictors/rfcn_keras_box_predictor_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for object_detection.predictors.rfcn_box_predictor.""" 17 | import numpy as np 18 | import tensorflow as tf 19 | 20 | from google.protobuf import text_format 21 | from object_detection.builders import hyperparams_builder 22 | from object_detection.predictors import rfcn_keras_box_predictor as box_predictor 23 | from object_detection.protos import hyperparams_pb2 24 | from object_detection.utils import test_case 25 | 26 | 27 | class RfcnKerasBoxPredictorTest(test_case.TestCase): 28 | 29 | def _build_conv_hyperparams(self): 30 | conv_hyperparams = hyperparams_pb2.Hyperparams() 31 | conv_hyperparams_text_proto = """ 32 | regularizer { 33 | l2_regularizer { 34 | } 35 | } 36 | initializer { 37 | truncated_normal_initializer { 38 | } 39 | } 40 | """ 41 | text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) 42 | return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) 43 | 44 | def test_get_correct_box_encoding_and_class_prediction_shapes(self): 45 | 46 | def graph_fn(image_features, proposal_boxes): 47 | rfcn_box_predictor = box_predictor.RfcnKerasBoxPredictor( 48 | is_training=False, 49 | num_classes=2, 50 | conv_hyperparams=self._build_conv_hyperparams(), 51 | freeze_batchnorm=False, 52 | num_spatial_bins=[3, 3], 53 | depth=4, 54 | crop_size=[12, 12], 55 | box_code_size=4 56 | ) 57 | box_predictions = rfcn_box_predictor( 58 | [image_features], 59 | proposal_boxes=proposal_boxes) 60 | box_encodings = tf.concat( 61 | box_predictions[box_predictor.BOX_ENCODINGS], axis=1) 62 | class_predictions_with_background = tf.concat( 63 | box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], 64 | axis=1) 65 | return (box_encodings, class_predictions_with_background) 66 | 67 | image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) 68 | proposal_boxes = np.random.rand(4, 2, 4).astype(np.float32) 69 | (box_encodings, class_predictions_with_background) = self.execute( 70 | graph_fn, [image_features, proposal_boxes]) 71 | 72 | self.assertAllEqual(box_encodings.shape, [8, 1, 2, 4]) 73 | self.assertAllEqual(class_predictions_with_background.shape, [8, 1, 3]) 74 | 75 | 76 | if __name__ == '__main__': 77 | tf.test.main() 78 | -------------------------------------------------------------------------------- /object_detection/protos/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/protos/__init__.py -------------------------------------------------------------------------------- /object_detection/protos/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/protos/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /object_detection/protos/__pycache__/string_int_label_map_pb2.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/protos/__pycache__/string_int_label_map_pb2.cpython-37.pyc -------------------------------------------------------------------------------- /object_detection/protos/anchor_generator.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | import "object_detection/protos/flexible_grid_anchor_generator.proto"; 6 | import "object_detection/protos/grid_anchor_generator.proto"; 7 | import "object_detection/protos/multiscale_anchor_generator.proto"; 8 | import "object_detection/protos/ssd_anchor_generator.proto"; 9 | 10 | // Configuration proto for the anchor generator to use in the object detection 11 | // pipeline. See core/anchor_generator.py for details. 12 | message AnchorGenerator { 13 | oneof anchor_generator_oneof { 14 | GridAnchorGenerator grid_anchor_generator = 1; 15 | SsdAnchorGenerator ssd_anchor_generator = 2; 16 | MultiscaleAnchorGenerator multiscale_anchor_generator = 3; 17 | FlexibleGridAnchorGenerator flexible_grid_anchor_generator = 4; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /object_detection/protos/argmax_matcher.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | // Configuration proto for ArgMaxMatcher. See 6 | // matchers/argmax_matcher.py for details. 7 | message ArgMaxMatcher { 8 | // Threshold for positive matches. 9 | optional float matched_threshold = 1 [default = 0.5]; 10 | 11 | // Threshold for negative matches. 12 | optional float unmatched_threshold = 2 [default = 0.5]; 13 | 14 | // Whether to construct ArgMaxMatcher without thresholds. 15 | optional bool ignore_thresholds = 3 [default = false]; 16 | 17 | // If True then negative matches are the ones below the unmatched_threshold, 18 | // whereas ignored matches are in between the matched and umatched 19 | // threshold. If False, then negative matches are in between the matched 20 | // and unmatched threshold, and everything lower than unmatched is ignored. 21 | optional bool negatives_lower_than_unmatched = 4 [default = true]; 22 | 23 | // Whether to ensure each row is matched to at least one column. 24 | optional bool force_match_for_each_row = 5 [default = false]; 25 | 26 | // Force constructed match objects to use matrix multiplication based gather 27 | // instead of standard tf.gather 28 | optional bool use_matmul_gather = 6 [default = false]; 29 | } 30 | -------------------------------------------------------------------------------- /object_detection/protos/bipartite_matcher.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | // Configuration proto for bipartite matcher. See 6 | // matchers/bipartite_matcher.py for details. 7 | message BipartiteMatcher { 8 | // Force constructed match objects to use matrix multiplication based gather 9 | // instead of standard tf.gather 10 | optional bool use_matmul_gather = 6 [default = false]; 11 | } 12 | -------------------------------------------------------------------------------- /object_detection/protos/bipartite_matcher_pb2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by the protocol buffer compiler. DO NOT EDIT! 3 | # source: object_detection/protos/bipartite_matcher.proto 4 | 5 | import sys 6 | _b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1')) 7 | from google.protobuf import descriptor as _descriptor 8 | from google.protobuf import message as _message 9 | from google.protobuf import reflection as _reflection 10 | from google.protobuf import symbol_database as _symbol_database 11 | # @@protoc_insertion_point(imports) 12 | 13 | _sym_db = _symbol_database.Default() 14 | 15 | 16 | 17 | 18 | DESCRIPTOR = _descriptor.FileDescriptor( 19 | name='object_detection/protos/bipartite_matcher.proto', 20 | package='object_detection.protos', 21 | syntax='proto2', 22 | serialized_options=None, 23 | serialized_pb=_b('\n/object_detection/protos/bipartite_matcher.proto\x12\x17object_detection.protos\"4\n\x10\x42ipartiteMatcher\x12 \n\x11use_matmul_gather\x18\x06 \x01(\x08:\x05\x66\x61lse') 24 | ) 25 | 26 | 27 | 28 | 29 | _BIPARTITEMATCHER = _descriptor.Descriptor( 30 | name='BipartiteMatcher', 31 | full_name='object_detection.protos.BipartiteMatcher', 32 | filename=None, 33 | file=DESCRIPTOR, 34 | containing_type=None, 35 | fields=[ 36 | _descriptor.FieldDescriptor( 37 | name='use_matmul_gather', full_name='object_detection.protos.BipartiteMatcher.use_matmul_gather', index=0, 38 | number=6, type=8, cpp_type=7, label=1, 39 | has_default_value=True, default_value=False, 40 | message_type=None, enum_type=None, containing_type=None, 41 | is_extension=False, extension_scope=None, 42 | serialized_options=None, file=DESCRIPTOR), 43 | ], 44 | extensions=[ 45 | ], 46 | nested_types=[], 47 | enum_types=[ 48 | ], 49 | serialized_options=None, 50 | is_extendable=False, 51 | syntax='proto2', 52 | extension_ranges=[], 53 | oneofs=[ 54 | ], 55 | serialized_start=76, 56 | serialized_end=128, 57 | ) 58 | 59 | DESCRIPTOR.message_types_by_name['BipartiteMatcher'] = _BIPARTITEMATCHER 60 | _sym_db.RegisterFileDescriptor(DESCRIPTOR) 61 | 62 | BipartiteMatcher = _reflection.GeneratedProtocolMessageType('BipartiteMatcher', (_message.Message,), { 63 | 'DESCRIPTOR' : _BIPARTITEMATCHER, 64 | '__module__' : 'object_detection.protos.bipartite_matcher_pb2' 65 | # @@protoc_insertion_point(class_scope:object_detection.protos.BipartiteMatcher) 66 | }) 67 | _sym_db.RegisterMessage(BipartiteMatcher) 68 | 69 | 70 | # @@protoc_insertion_point(module_scope) 71 | -------------------------------------------------------------------------------- /object_detection/protos/box_coder.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | import "object_detection/protos/faster_rcnn_box_coder.proto"; 6 | import "object_detection/protos/keypoint_box_coder.proto"; 7 | import "object_detection/protos/mean_stddev_box_coder.proto"; 8 | import "object_detection/protos/square_box_coder.proto"; 9 | 10 | // Configuration proto for the box coder to be used in the object detection 11 | // pipeline. See core/box_coder.py for details. 12 | message BoxCoder { 13 | oneof box_coder_oneof { 14 | FasterRcnnBoxCoder faster_rcnn_box_coder = 1; 15 | MeanStddevBoxCoder mean_stddev_box_coder = 2; 16 | SquareBoxCoder square_box_coder = 3; 17 | KeypointBoxCoder keypoint_box_coder = 4; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /object_detection/protos/calibration.proto: -------------------------------------------------------------------------------- 1 | // These protos contain the calibration parameters necessary for transforming 2 | // a model's original detection scores or logits. The parameters result from 3 | // fitting a calibration function on the model's outputs. 4 | 5 | syntax = "proto2"; 6 | 7 | package object_detection.protos; 8 | 9 | // Message wrapper for various calibration configurations. 10 | message CalibrationConfig { 11 | oneof calibrator { 12 | // Class-agnostic calibration via linear interpolation (usually output from 13 | // isotonic regression). 14 | FunctionApproximation function_approximation = 1; 15 | 16 | // Per-class calibration via linear interpolation. 17 | ClassIdFunctionApproximations class_id_function_approximations = 2; 18 | 19 | // Class-agnostic sigmoid calibration. 20 | SigmoidCalibration sigmoid_calibration = 3; 21 | 22 | // Per-class sigmoid calibration. 23 | ClassIdSigmoidCalibrations class_id_sigmoid_calibrations = 4; 24 | 25 | // Temperature scaling calibration. 26 | TemperatureScalingCalibration temperature_scaling_calibration = 5; 27 | } 28 | } 29 | 30 | // Message for class-agnostic domain/range mapping for function 31 | // approximations. 32 | message FunctionApproximation { 33 | // Message mapping class labels to indices 34 | optional XYPairs x_y_pairs = 1; 35 | } 36 | 37 | // Message for class-specific domain/range mapping for function 38 | // approximations. 39 | message ClassIdFunctionApproximations { 40 | // Message mapping class ids to indices. 41 | map class_id_xy_pairs_map = 1; 42 | } 43 | 44 | // Message for class-agnostic Sigmoid Calibration. 45 | message SigmoidCalibration { 46 | // Message mapping class index to Sigmoid Parameters 47 | optional SigmoidParameters sigmoid_parameters = 1; 48 | } 49 | 50 | // Message for class-specific Sigmoid Calibration. 51 | message ClassIdSigmoidCalibrations { 52 | // Message mapping class index to Sigmoid Parameters. 53 | map class_id_sigmoid_parameters_map = 1; 54 | } 55 | 56 | // Message for Temperature Scaling Calibration. 57 | message TemperatureScalingCalibration { 58 | optional float scaler = 1; 59 | } 60 | 61 | // Description of data used to fit the calibration model. CLASS_SPECIFIC 62 | // indicates that the calibration parameters are derived from detections 63 | // pertaining to a single class. ALL_CLASSES indicates that parameters were 64 | // obtained by fitting a model on detections from all classes (including the 65 | // background class). 66 | enum TrainingDataType { 67 | DATA_TYPE_UNKNOWN = 0; 68 | ALL_CLASSES = 1; 69 | CLASS_SPECIFIC = 2; 70 | } 71 | 72 | // Message to store a domain/range pair for function to be approximated. 73 | message XYPairs { 74 | message XYPair { 75 | optional float x = 1; 76 | optional float y = 2; 77 | } 78 | 79 | // Sequence of x/y pairs for function approximation. 80 | repeated XYPair x_y_pair = 1; 81 | 82 | // Description of data used to fit the calibration model. 83 | optional TrainingDataType training_data_type = 2; 84 | } 85 | 86 | // Message defining parameters for sigmoid calibration. 87 | message SigmoidParameters { 88 | optional float a = 1 [default = -1.0]; 89 | optional float b = 2 [default = 0.0]; 90 | } 91 | -------------------------------------------------------------------------------- /object_detection/protos/faster_rcnn_box_coder.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | // Configuration proto for FasterRCNNBoxCoder. See 6 | // box_coders/faster_rcnn_box_coder.py for details. 7 | message FasterRcnnBoxCoder { 8 | // Scale factor for anchor encoded box center. 9 | optional float y_scale = 1 [default = 10.0]; 10 | optional float x_scale = 2 [default = 10.0]; 11 | 12 | // Scale factor for anchor encoded box height. 13 | optional float height_scale = 3 [default = 5.0]; 14 | 15 | // Scale factor for anchor encoded box width. 16 | optional float width_scale = 4 [default = 5.0]; 17 | } 18 | -------------------------------------------------------------------------------- /object_detection/protos/flexible_grid_anchor_generator.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | message FlexibleGridAnchorGenerator { 6 | repeated AnchorGrid anchor_grid = 1; 7 | 8 | // Whether to produce anchors in normalized coordinates. 9 | optional bool normalize_coordinates = 2 [default = true]; 10 | } 11 | 12 | message AnchorGrid { 13 | // The base sizes in pixels for each anchor in this anchor layer. 14 | repeated float base_sizes = 1; 15 | 16 | // The aspect ratios for each anchor in this anchor layer. 17 | repeated float aspect_ratios = 2; 18 | 19 | // The anchor height stride in pixels. 20 | optional uint32 height_stride = 3; 21 | 22 | // The anchor width stride in pixels. 23 | optional uint32 width_stride = 4; 24 | 25 | // The anchor height offset in pixels. 26 | optional uint32 height_offset = 5 [default = 0]; 27 | 28 | // The anchor width offset in pixels. 29 | optional uint32 width_offset = 6 [default = 0]; 30 | } 31 | -------------------------------------------------------------------------------- /object_detection/protos/graph_rewriter.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | // Message to configure graph rewriter for the tf graph. 6 | message GraphRewriter { 7 | optional Quantization quantization = 1; 8 | extensions 1000 to max; 9 | } 10 | 11 | // Message for quantization options. See 12 | // tensorflow/contrib/quantize/python/quantize.py for details. 13 | message Quantization { 14 | // Number of steps to delay before quantization takes effect during training. 15 | optional int32 delay = 1 [default = 500000]; 16 | 17 | // Number of bits to use for quantizing weights. 18 | // Only 8 bit is supported for now. 19 | optional int32 weight_bits = 2 [default = 8]; 20 | 21 | // Number of bits to use for quantizing activations. 22 | // Only 8 bit is supported for now. 23 | optional int32 activation_bits = 3 [default = 8]; 24 | 25 | // Whether to use symmetric weight quantization. 26 | optional bool symmetric = 4 [default = false]; 27 | } 28 | -------------------------------------------------------------------------------- /object_detection/protos/grid_anchor_generator.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | // Configuration proto for GridAnchorGenerator. See 6 | // anchor_generators/grid_anchor_generator.py for details. 7 | message GridAnchorGenerator { 8 | // Anchor height in pixels. 9 | optional int32 height = 1 [default = 256]; 10 | 11 | // Anchor width in pixels. 12 | optional int32 width = 2 [default = 256]; 13 | 14 | // Anchor stride in height dimension in pixels. 15 | optional int32 height_stride = 3 [default = 16]; 16 | 17 | // Anchor stride in width dimension in pixels. 18 | optional int32 width_stride = 4 [default = 16]; 19 | 20 | // Anchor height offset in pixels. 21 | optional int32 height_offset = 5 [default = 0]; 22 | 23 | // Anchor width offset in pixels. 24 | optional int32 width_offset = 6 [default = 0]; 25 | 26 | // At any given location, len(scales) * len(aspect_ratios) anchors are 27 | // generated with all possible combinations of scales and aspect ratios. 28 | 29 | // List of scales for the anchors. 30 | repeated float scales = 7; 31 | 32 | // List of aspect ratios for the anchors. 33 | repeated float aspect_ratios = 8; 34 | } 35 | -------------------------------------------------------------------------------- /object_detection/protos/image_resizer.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | // Configuration proto for image resizing operations. 6 | // See builders/image_resizer_builder.py for details. 7 | message ImageResizer { 8 | oneof image_resizer_oneof { 9 | KeepAspectRatioResizer keep_aspect_ratio_resizer = 1; 10 | FixedShapeResizer fixed_shape_resizer = 2; 11 | IdentityResizer identity_resizer = 3; 12 | ConditionalShapeResizer conditional_shape_resizer = 4; 13 | } 14 | } 15 | 16 | // Enumeration type for image resizing methods provided in TensorFlow. 17 | enum ResizeType { 18 | BILINEAR = 0; // Corresponds to tf.image.ResizeMethod.BILINEAR 19 | NEAREST_NEIGHBOR = 1; // Corresponds to tf.image.ResizeMethod.NEAREST_NEIGHBOR 20 | BICUBIC = 2; // Corresponds to tf.image.ResizeMethod.BICUBIC 21 | AREA = 3; // Corresponds to tf.image.ResizeMethod.AREA 22 | } 23 | 24 | message IdentityResizer { 25 | } 26 | 27 | // Configuration proto for image resizer that keeps aspect ratio. 28 | message KeepAspectRatioResizer { 29 | // Desired size of the smaller image dimension in pixels. 30 | optional int32 min_dimension = 1 [default = 600]; 31 | 32 | // Desired size of the larger image dimension in pixels. 33 | optional int32 max_dimension = 2 [default = 1024]; 34 | 35 | // Desired method when resizing image. 36 | optional ResizeType resize_method = 3 [default = BILINEAR]; 37 | 38 | // Whether to pad the image with zeros so the output spatial size is 39 | // [max_dimension, max_dimension]. Note that the zeros are padded to the 40 | // bottom and the right of the resized image. 41 | optional bool pad_to_max_dimension = 4 [default = false]; 42 | 43 | // Whether to also resize the image channels from 3 to 1 (RGB to grayscale). 44 | optional bool convert_to_grayscale = 5 [default = false]; 45 | 46 | // Per-channel pad value. This is only used when pad_to_max_dimension is True. 47 | // If unspecified, a default pad value of 0 is applied to all channels. 48 | repeated float per_channel_pad_value = 6; 49 | } 50 | 51 | // Configuration proto for image resizer that resizes to a fixed shape. 52 | message FixedShapeResizer { 53 | // Desired height of image in pixels. 54 | optional int32 height = 1 [default = 300]; 55 | 56 | // Desired width of image in pixels. 57 | optional int32 width = 2 [default = 300]; 58 | 59 | // Desired method when resizing image. 60 | optional ResizeType resize_method = 3 [default = BILINEAR]; 61 | 62 | // Whether to also resize the image channels from 3 to 1 (RGB to grayscale). 63 | optional bool convert_to_grayscale = 4 [default = false]; 64 | } 65 | 66 | // Configuration proto for image resizer that resizes only if input image height 67 | // or width is greater or smaller than a certain size. 68 | // Aspect ratio is maintained. 69 | message ConditionalShapeResizer { 70 | 71 | // Enumeration for the condition on which to resize an image. 72 | enum ResizeCondition { 73 | INVALID = 0; // Default value. 74 | GREATER = 1; // Resizes image if a dimension is greater than specified size. 75 | SMALLER = 2; // Resizes image if a dimension is smaller than specified size. 76 | } 77 | 78 | // Condition which must be true to resize the image. 79 | optional ResizeCondition condition = 1 [default = GREATER]; 80 | 81 | // Threshold for the image size. If any image dimension is above or below this 82 | // (as specified by condition) the image will be resized so that it meets the 83 | // threshold. 84 | optional int32 size_threshold = 2 [default = 300]; 85 | 86 | // Desired method when resizing image. 87 | optional ResizeType resize_method = 3 [default = BILINEAR]; 88 | 89 | // Whether to also resize the image channels from 3 to 1 (RGB to grayscale). 90 | optional bool convert_to_grayscale = 4 [default = false]; 91 | 92 | } 93 | -------------------------------------------------------------------------------- /object_detection/protos/keypoint_box_coder.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | // Configuration proto for KeypointBoxCoder. See 6 | // box_coders/keypoint_box_coder.py for details. 7 | message KeypointBoxCoder { 8 | optional int32 num_keypoints = 1; 9 | 10 | // Scale factor for anchor encoded box center and keypoints. 11 | optional float y_scale = 2 [default = 10.0]; 12 | optional float x_scale = 3 [default = 10.0]; 13 | 14 | // Scale factor for anchor encoded box height. 15 | optional float height_scale = 4 [default = 5.0]; 16 | 17 | // Scale factor for anchor encoded box width. 18 | optional float width_scale = 5 [default = 5.0]; 19 | } 20 | -------------------------------------------------------------------------------- /object_detection/protos/matcher.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | import "object_detection/protos/argmax_matcher.proto"; 6 | import "object_detection/protos/bipartite_matcher.proto"; 7 | 8 | // Configuration proto for the matcher to be used in the object detection 9 | // pipeline. See core/matcher.py for details. 10 | message Matcher { 11 | oneof matcher_oneof { 12 | ArgMaxMatcher argmax_matcher = 1; 13 | BipartiteMatcher bipartite_matcher = 2; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /object_detection/protos/mean_stddev_box_coder.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | // Configuration proto for MeanStddevBoxCoder. See 6 | // box_coders/mean_stddev_box_coder.py for details. 7 | message MeanStddevBoxCoder { 8 | // The standard deviation used to encode and decode boxes. 9 | optional float stddev = 1 [default=0.01]; 10 | } 11 | -------------------------------------------------------------------------------- /object_detection/protos/mean_stddev_box_coder_pb2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by the protocol buffer compiler. DO NOT EDIT! 3 | # source: object_detection/protos/mean_stddev_box_coder.proto 4 | 5 | import sys 6 | _b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1')) 7 | from google.protobuf import descriptor as _descriptor 8 | from google.protobuf import message as _message 9 | from google.protobuf import reflection as _reflection 10 | from google.protobuf import symbol_database as _symbol_database 11 | # @@protoc_insertion_point(imports) 12 | 13 | _sym_db = _symbol_database.Default() 14 | 15 | 16 | 17 | 18 | DESCRIPTOR = _descriptor.FileDescriptor( 19 | name='object_detection/protos/mean_stddev_box_coder.proto', 20 | package='object_detection.protos', 21 | syntax='proto2', 22 | serialized_options=None, 23 | serialized_pb=_b('\n3object_detection/protos/mean_stddev_box_coder.proto\x12\x17object_detection.protos\"*\n\x12MeanStddevBoxCoder\x12\x14\n\x06stddev\x18\x01 \x01(\x02:\x04\x30.01') 24 | ) 25 | 26 | 27 | 28 | 29 | _MEANSTDDEVBOXCODER = _descriptor.Descriptor( 30 | name='MeanStddevBoxCoder', 31 | full_name='object_detection.protos.MeanStddevBoxCoder', 32 | filename=None, 33 | file=DESCRIPTOR, 34 | containing_type=None, 35 | fields=[ 36 | _descriptor.FieldDescriptor( 37 | name='stddev', full_name='object_detection.protos.MeanStddevBoxCoder.stddev', index=0, 38 | number=1, type=2, cpp_type=6, label=1, 39 | has_default_value=True, default_value=float(0.01), 40 | message_type=None, enum_type=None, containing_type=None, 41 | is_extension=False, extension_scope=None, 42 | serialized_options=None, file=DESCRIPTOR), 43 | ], 44 | extensions=[ 45 | ], 46 | nested_types=[], 47 | enum_types=[ 48 | ], 49 | serialized_options=None, 50 | is_extendable=False, 51 | syntax='proto2', 52 | extension_ranges=[], 53 | oneofs=[ 54 | ], 55 | serialized_start=80, 56 | serialized_end=122, 57 | ) 58 | 59 | DESCRIPTOR.message_types_by_name['MeanStddevBoxCoder'] = _MEANSTDDEVBOXCODER 60 | _sym_db.RegisterFileDescriptor(DESCRIPTOR) 61 | 62 | MeanStddevBoxCoder = _reflection.GeneratedProtocolMessageType('MeanStddevBoxCoder', (_message.Message,), { 63 | 'DESCRIPTOR' : _MEANSTDDEVBOXCODER, 64 | '__module__' : 'object_detection.protos.mean_stddev_box_coder_pb2' 65 | # @@protoc_insertion_point(class_scope:object_detection.protos.MeanStddevBoxCoder) 66 | }) 67 | _sym_db.RegisterMessage(MeanStddevBoxCoder) 68 | 69 | 70 | # @@protoc_insertion_point(module_scope) 71 | -------------------------------------------------------------------------------- /object_detection/protos/model.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | import "object_detection/protos/faster_rcnn.proto"; 6 | import "object_detection/protos/ssd.proto"; 7 | 8 | // Top level configuration for DetectionModels. 9 | message DetectionModel { 10 | oneof model { 11 | FasterRcnn faster_rcnn = 1; 12 | Ssd ssd = 2; 13 | 14 | // This can be used to define experimental models. To define your own 15 | // experimental meta architecture, populate a key in the 16 | // model_builder.EXPERIMENTAL_META_ARCHITECURE_BUILDER_MAP dict and set its 17 | // value to a function that builds your model. 18 | ExperimentalModel experimental_model = 3; 19 | } 20 | } 21 | 22 | message ExperimentalModel { 23 | optional string name = 1; 24 | } 25 | -------------------------------------------------------------------------------- /object_detection/protos/multiscale_anchor_generator.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | // Configuration proto for RetinaNet anchor generator described in 6 | // https://arxiv.org/abs/1708.02002. See 7 | // anchor_generators/multiscale_grid_anchor_generator.py for details. 8 | message MultiscaleAnchorGenerator { 9 | // minimum level in feature pyramid 10 | optional int32 min_level = 1 [default = 3]; 11 | 12 | // maximum level in feature pyramid 13 | optional int32 max_level = 2 [default = 7]; 14 | 15 | // Scale of anchor to feature stride 16 | optional float anchor_scale = 3 [default = 4.0]; 17 | 18 | // Aspect ratios for anchors at each grid point. 19 | repeated float aspect_ratios = 4; 20 | 21 | // Number of intermediate scale each scale octave 22 | optional int32 scales_per_octave = 5 [default = 2]; 23 | 24 | // Whether to produce anchors in normalized coordinates. 25 | optional bool normalize_coordinates = 6 [default = true]; 26 | } 27 | -------------------------------------------------------------------------------- /object_detection/protos/optimizer.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | // Messages for configuring the optimizing strategy for training object 6 | // detection models. 7 | 8 | // Top level optimizer message. 9 | message Optimizer { 10 | oneof optimizer { 11 | RMSPropOptimizer rms_prop_optimizer = 1; 12 | MomentumOptimizer momentum_optimizer = 2; 13 | AdamOptimizer adam_optimizer = 3; 14 | } 15 | optional bool use_moving_average = 4 [default = true]; 16 | optional float moving_average_decay = 5 [default = 0.9999]; 17 | } 18 | 19 | // Configuration message for the RMSPropOptimizer 20 | // See: https://www.tensorflow.org/api_docs/python/tf/train/RMSPropOptimizer 21 | message RMSPropOptimizer { 22 | optional LearningRate learning_rate = 1; 23 | optional float momentum_optimizer_value = 2 [default = 0.9]; 24 | optional float decay = 3 [default = 0.9]; 25 | optional float epsilon = 4 [default = 1.0]; 26 | } 27 | 28 | // Configuration message for the MomentumOptimizer 29 | // See: https://www.tensorflow.org/api_docs/python/tf/train/MomentumOptimizer 30 | message MomentumOptimizer { 31 | optional LearningRate learning_rate = 1; 32 | optional float momentum_optimizer_value = 2 [default = 0.9]; 33 | } 34 | 35 | // Configuration message for the AdamOptimizer 36 | // See: https://www.tensorflow.org/api_docs/python/tf/train/AdamOptimizer 37 | message AdamOptimizer { 38 | optional LearningRate learning_rate = 1; 39 | } 40 | 41 | 42 | // Configuration message for optimizer learning rate. 43 | message LearningRate { 44 | oneof learning_rate { 45 | ConstantLearningRate constant_learning_rate = 1; 46 | ExponentialDecayLearningRate exponential_decay_learning_rate = 2; 47 | ManualStepLearningRate manual_step_learning_rate = 3; 48 | CosineDecayLearningRate cosine_decay_learning_rate = 4; 49 | } 50 | } 51 | 52 | // Configuration message for a constant learning rate. 53 | message ConstantLearningRate { 54 | optional float learning_rate = 1 [default = 0.002]; 55 | } 56 | 57 | // Configuration message for an exponentially decaying learning rate. 58 | // See https://www.tensorflow.org/versions/master/api_docs/python/train/ \ 59 | // decaying_the_learning_rate#exponential_decay 60 | message ExponentialDecayLearningRate { 61 | optional float initial_learning_rate = 1 [default = 0.002]; 62 | optional uint32 decay_steps = 2 [default = 4000000]; 63 | optional float decay_factor = 3 [default = 0.95]; 64 | optional bool staircase = 4 [default = true]; 65 | optional float burnin_learning_rate = 5 [default = 0.0]; 66 | optional uint32 burnin_steps = 6 [default = 0]; 67 | optional float min_learning_rate = 7 [default = 0.0]; 68 | } 69 | 70 | // Configuration message for a manually defined learning rate schedule. 71 | message ManualStepLearningRate { 72 | optional float initial_learning_rate = 1 [default = 0.002]; 73 | message LearningRateSchedule { 74 | optional uint32 step = 1; 75 | optional float learning_rate = 2 [default = 0.002]; 76 | } 77 | repeated LearningRateSchedule schedule = 2; 78 | 79 | // Whether to linearly interpolate learning rates for steps in 80 | // [0, schedule[0].step]. 81 | optional bool warmup = 3 [default = false]; 82 | } 83 | 84 | // Configuration message for a cosine decaying learning rate as defined in 85 | // object_detection/utils/learning_schedules.py 86 | message CosineDecayLearningRate { 87 | optional float learning_rate_base = 1 [default = 0.002]; 88 | optional uint32 total_steps = 2 [default = 4000000]; 89 | optional float warmup_learning_rate = 3 [default = 0.0002]; 90 | optional uint32 warmup_steps = 4 [default = 10000]; 91 | optional uint32 hold_base_rate_steps = 5 [default = 0]; 92 | } 93 | -------------------------------------------------------------------------------- /object_detection/protos/pipeline.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | import "object_detection/protos/eval.proto"; 6 | import "object_detection/protos/graph_rewriter.proto"; 7 | import "object_detection/protos/input_reader.proto"; 8 | import "object_detection/protos/model.proto"; 9 | import "object_detection/protos/train.proto"; 10 | 11 | // Convenience message for configuring a training and eval pipeline. Allows all 12 | // of the pipeline parameters to be configured from one file. 13 | // Next id: 7 14 | message TrainEvalPipelineConfig { 15 | optional DetectionModel model = 1; 16 | optional TrainConfig train_config = 2; 17 | optional InputReader train_input_reader = 3; 18 | optional EvalConfig eval_config = 4; 19 | repeated InputReader eval_input_reader = 5; 20 | optional GraphRewriter graph_rewriter = 6; 21 | extensions 1000 to max; 22 | } 23 | -------------------------------------------------------------------------------- /object_detection/protos/post_processing.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | import "object_detection/protos/calibration.proto"; 6 | 7 | // Configuration proto for non-max-suppression operation on a batch of 8 | // detections. 9 | message BatchNonMaxSuppression { 10 | // Scalar threshold for score (low scoring boxes are removed). 11 | optional float score_threshold = 1 [default = 0.0]; 12 | 13 | // Scalar threshold for IOU (boxes that have high IOU overlap 14 | // with previously selected boxes are removed). 15 | optional float iou_threshold = 2 [default = 0.6]; 16 | 17 | // Maximum number of detections to retain per class. 18 | optional int32 max_detections_per_class = 3 [default = 100]; 19 | 20 | // Maximum number of detections to retain across all classes. 21 | optional int32 max_total_detections = 5 [default = 100]; 22 | 23 | // Whether to use the implementation of NMS that guarantees static shapes. 24 | optional bool use_static_shapes = 6 [default = false]; 25 | 26 | // Whether to use class agnostic NMS. 27 | // Class-agnostic NMS function implements a class-agnostic version 28 | // of Non Maximal Suppression where if max_classes_per_detection=k, 29 | // 1) we keep the top-k scores for each detection and 30 | // 2) during NMS, each detection only uses the highest class score for sorting. 31 | // 3) Compared to regular NMS, the worst runtime of this version is O(N^2) 32 | // instead of O(KN^2) where N is the number of detections and K the number of 33 | // classes. 34 | optional bool use_class_agnostic_nms = 7 [default = false]; 35 | 36 | // Number of classes retained per detection in class agnostic NMS. 37 | 38 | optional int32 max_classes_per_detection = 8 [default = 1]; 39 | 40 | // Soft NMS sigma parameter; Bodla et al, https://arxiv.org/abs/1704.04503) 41 | optional float soft_nms_sigma = 9 [default = 0.0]; 42 | 43 | // Whether to use partitioned version of non_max_suppression. 44 | optional bool use_partitioned_nms = 10 [default = false]; 45 | 46 | // Whether to use tf.image.combined_non_max_suppression. 47 | optional bool use_combined_nms = 11 [default = false]; 48 | } 49 | 50 | // Configuration proto for post-processing predicted boxes and 51 | // scores. 52 | message PostProcessing { 53 | // Non max suppression parameters. 54 | optional BatchNonMaxSuppression batch_non_max_suppression = 1; 55 | 56 | // Enum to specify how to convert the detection scores. 57 | enum ScoreConverter { 58 | // Input scores equals output scores. 59 | IDENTITY = 0; 60 | 61 | // Applies a sigmoid on input scores. 62 | SIGMOID = 1; 63 | 64 | // Applies a softmax on input scores 65 | SOFTMAX = 2; 66 | } 67 | 68 | // Score converter to use. 69 | optional ScoreConverter score_converter = 2 [default = IDENTITY]; 70 | // Scale logit (input) value before conversion in post-processing step. 71 | // Typically used for softmax distillation, though can be used to scale for 72 | // other reasons. 73 | optional float logit_scale = 3 [default = 1.0]; 74 | // Calibrate score outputs. Calibration is applied after score converter 75 | // and before non max suppression. 76 | optional CalibrationConfig calibration_config = 4; 77 | } 78 | -------------------------------------------------------------------------------- /object_detection/protos/region_similarity_calculator.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | // Configuration proto for region similarity calculators. See 6 | // core/region_similarity_calculator.py for details. 7 | message RegionSimilarityCalculator { 8 | oneof region_similarity { 9 | NegSqDistSimilarity neg_sq_dist_similarity = 1; 10 | IouSimilarity iou_similarity = 2; 11 | IoaSimilarity ioa_similarity = 3; 12 | ThresholdedIouSimilarity thresholded_iou_similarity = 4; 13 | } 14 | } 15 | 16 | // Configuration for negative squared distance similarity calculator. 17 | message NegSqDistSimilarity { 18 | } 19 | 20 | // Configuration for intersection-over-union (IOU) similarity calculator. 21 | message IouSimilarity { 22 | } 23 | 24 | // Configuration for intersection-over-area (IOA) similarity calculator. 25 | message IoaSimilarity { 26 | } 27 | 28 | // Configuration for thresholded-intersection-over-union similarity calculator. 29 | message ThresholdedIouSimilarity { 30 | 31 | // IOU threshold used for filtering scores. 32 | optional float iou_threshold = 1 [default = 0.5]; 33 | } 34 | -------------------------------------------------------------------------------- /object_detection/protos/square_box_coder.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | // Configuration proto for SquareBoxCoder. See 6 | // box_coders/square_box_coder.py for details. 7 | message SquareBoxCoder { 8 | // Scale factor for anchor encoded box center. 9 | optional float y_scale = 1 [default = 10.0]; 10 | optional float x_scale = 2 [default = 10.0]; 11 | 12 | // Scale factor for anchor encoded box length. 13 | optional float length_scale = 3 [default = 5.0]; 14 | } 15 | -------------------------------------------------------------------------------- /object_detection/protos/square_box_coder_pb2.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Generated by the protocol buffer compiler. DO NOT EDIT! 3 | # source: object_detection/protos/square_box_coder.proto 4 | 5 | import sys 6 | _b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1')) 7 | from google.protobuf import descriptor as _descriptor 8 | from google.protobuf import message as _message 9 | from google.protobuf import reflection as _reflection 10 | from google.protobuf import symbol_database as _symbol_database 11 | # @@protoc_insertion_point(imports) 12 | 13 | _sym_db = _symbol_database.Default() 14 | 15 | 16 | 17 | 18 | DESCRIPTOR = _descriptor.FileDescriptor( 19 | name='object_detection/protos/square_box_coder.proto', 20 | package='object_detection.protos', 21 | syntax='proto2', 22 | serialized_options=None, 23 | serialized_pb=_b('\n.object_detection/protos/square_box_coder.proto\x12\x17object_detection.protos\"S\n\x0eSquareBoxCoder\x12\x13\n\x07y_scale\x18\x01 \x01(\x02:\x02\x31\x30\x12\x13\n\x07x_scale\x18\x02 \x01(\x02:\x02\x31\x30\x12\x17\n\x0clength_scale\x18\x03 \x01(\x02:\x01\x35') 24 | ) 25 | 26 | 27 | 28 | 29 | _SQUAREBOXCODER = _descriptor.Descriptor( 30 | name='SquareBoxCoder', 31 | full_name='object_detection.protos.SquareBoxCoder', 32 | filename=None, 33 | file=DESCRIPTOR, 34 | containing_type=None, 35 | fields=[ 36 | _descriptor.FieldDescriptor( 37 | name='y_scale', full_name='object_detection.protos.SquareBoxCoder.y_scale', index=0, 38 | number=1, type=2, cpp_type=6, label=1, 39 | has_default_value=True, default_value=float(10), 40 | message_type=None, enum_type=None, containing_type=None, 41 | is_extension=False, extension_scope=None, 42 | serialized_options=None, file=DESCRIPTOR), 43 | _descriptor.FieldDescriptor( 44 | name='x_scale', full_name='object_detection.protos.SquareBoxCoder.x_scale', index=1, 45 | number=2, type=2, cpp_type=6, label=1, 46 | has_default_value=True, default_value=float(10), 47 | message_type=None, enum_type=None, containing_type=None, 48 | is_extension=False, extension_scope=None, 49 | serialized_options=None, file=DESCRIPTOR), 50 | _descriptor.FieldDescriptor( 51 | name='length_scale', full_name='object_detection.protos.SquareBoxCoder.length_scale', index=2, 52 | number=3, type=2, cpp_type=6, label=1, 53 | has_default_value=True, default_value=float(5), 54 | message_type=None, enum_type=None, containing_type=None, 55 | is_extension=False, extension_scope=None, 56 | serialized_options=None, file=DESCRIPTOR), 57 | ], 58 | extensions=[ 59 | ], 60 | nested_types=[], 61 | enum_types=[ 62 | ], 63 | serialized_options=None, 64 | is_extendable=False, 65 | syntax='proto2', 66 | extension_ranges=[], 67 | oneofs=[ 68 | ], 69 | serialized_start=75, 70 | serialized_end=158, 71 | ) 72 | 73 | DESCRIPTOR.message_types_by_name['SquareBoxCoder'] = _SQUAREBOXCODER 74 | _sym_db.RegisterFileDescriptor(DESCRIPTOR) 75 | 76 | SquareBoxCoder = _reflection.GeneratedProtocolMessageType('SquareBoxCoder', (_message.Message,), { 77 | 'DESCRIPTOR' : _SQUAREBOXCODER, 78 | '__module__' : 'object_detection.protos.square_box_coder_pb2' 79 | # @@protoc_insertion_point(class_scope:object_detection.protos.SquareBoxCoder) 80 | }) 81 | _sym_db.RegisterMessage(SquareBoxCoder) 82 | 83 | 84 | # @@protoc_insertion_point(module_scope) 85 | -------------------------------------------------------------------------------- /object_detection/protos/ssd_anchor_generator.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | // Configuration proto for SSD anchor generator described in 6 | // https://arxiv.org/abs/1512.02325. See 7 | // anchor_generators/multiple_grid_anchor_generator.py for details. 8 | message SsdAnchorGenerator { 9 | // Number of grid layers to create anchors for. 10 | optional int32 num_layers = 1 [default = 6]; 11 | 12 | // Scale of anchors corresponding to finest resolution. 13 | optional float min_scale = 2 [default = 0.2]; 14 | 15 | // Scale of anchors corresponding to coarsest resolution 16 | optional float max_scale = 3 [default = 0.95]; 17 | 18 | // Can be used to override min_scale->max_scale, with an explicitly defined 19 | // set of scales. If empty, then min_scale->max_scale is used. 20 | repeated float scales = 12; 21 | 22 | // Aspect ratios for anchors at each grid point. 23 | repeated float aspect_ratios = 4; 24 | 25 | // When this aspect ratio is greater than 0, then an additional 26 | // anchor, with an interpolated scale is added with this aspect ratio. 27 | optional float interpolated_scale_aspect_ratio = 13 [default = 1.0]; 28 | 29 | // Whether to use the following aspect ratio and scale combination for the 30 | // layer with the finest resolution : (scale=0.1, aspect_ratio=1.0), 31 | // (scale=min_scale, aspect_ration=2.0), (scale=min_scale, aspect_ratio=0.5). 32 | optional bool reduce_boxes_in_lowest_layer = 5 [default = true]; 33 | 34 | // The base anchor size in height dimension. 35 | optional float base_anchor_height = 6 [default = 1.0]; 36 | 37 | // The base anchor size in width dimension. 38 | optional float base_anchor_width = 7 [default = 1.0]; 39 | 40 | // Anchor stride in height dimension in pixels for each layer. The length of 41 | // this field is expected to be equal to the value of num_layers. 42 | repeated int32 height_stride = 8; 43 | 44 | // Anchor stride in width dimension in pixels for each layer. The length of 45 | // this field is expected to be equal to the value of num_layers. 46 | repeated int32 width_stride = 9; 47 | 48 | // Anchor height offset in pixels for each layer. The length of this field is 49 | // expected to be equal to the value of num_layers. 50 | repeated int32 height_offset = 10; 51 | 52 | // Anchor width offset in pixels for each layer. The length of this field is 53 | // expected to be equal to the value of num_layers. 54 | repeated int32 width_offset = 11; 55 | } 56 | -------------------------------------------------------------------------------- /object_detection/protos/string_int_label_map.proto: -------------------------------------------------------------------------------- 1 | // Message to store the mapping from class label strings to class id. Datasets 2 | // use string labels to represent classes while the object detection framework 3 | // works with class ids. This message maps them so they can be converted back 4 | // and forth as needed. 5 | syntax = "proto2"; 6 | 7 | package object_detection.protos; 8 | 9 | message StringIntLabelMapItem { 10 | // String name. The most common practice is to set this to a MID or synsets 11 | // id. 12 | optional string name = 1; 13 | 14 | // Integer id that maps to the string name above. Label ids should start from 15 | // 1. 16 | optional int32 id = 2; 17 | 18 | // Human readable string label. 19 | optional string display_name = 3; 20 | }; 21 | 22 | message StringIntLabelMap { 23 | repeated StringIntLabelMapItem item = 1; 24 | }; 25 | -------------------------------------------------------------------------------- /object_detection/protos/target_assigner.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto2"; 2 | 3 | package object_detection.protos; 4 | 5 | import "object_detection/protos/box_coder.proto"; 6 | import "object_detection/protos/matcher.proto"; 7 | import "object_detection/protos/region_similarity_calculator.proto"; 8 | 9 | // Message to configure Target Assigner for object detectors. 10 | message TargetAssigner { 11 | optional Matcher matcher = 1; 12 | optional RegionSimilarityCalculator similarity_calculator = 2; 13 | optional BoxCoder box_coder = 3; 14 | } 15 | -------------------------------------------------------------------------------- /object_detection/samples/cloud/cloud.yml: -------------------------------------------------------------------------------- 1 | trainingInput: 2 | runtimeVersion: "1.12" 3 | scaleTier: CUSTOM 4 | masterType: standard_gpu 5 | workerCount: 5 6 | workerType: standard_gpu 7 | parameterServerCount: 3 8 | parameterServerType: standard 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /object_detection/test_data/pets_examples.record: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/test_data/pets_examples.record -------------------------------------------------------------------------------- /object_detection/test_images/image1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/test_images/image1.jpg -------------------------------------------------------------------------------- /object_detection/test_images/image2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/test_images/image2.jpg -------------------------------------------------------------------------------- /object_detection/test_images/image_info.txt: -------------------------------------------------------------------------------- 1 | 2 | Image provenance: 3 | image1.jpg: https://commons.wikimedia.org/wiki/File:Baegle_dwa.jpg 4 | image2.jpg: Michael Miley, 5 | https://www.flickr.com/photos/mike_miley/4678754542/in/photolist-88rQHL-88oBVp-88oC2B-88rS6J-88rSqm-88oBLv-88oBC4 6 | 7 | -------------------------------------------------------------------------------- /object_detection/tpu_exporters/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /object_detection/tpu_exporters/export_saved_model_tpu.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Python binary for exporting SavedModel, tailored for TPU inference.""" 16 | from __future__ import absolute_import 17 | from __future__ import division 18 | from __future__ import print_function 19 | 20 | import tensorflow as tf 21 | from object_detection.tpu_exporters import export_saved_model_tpu_lib 22 | 23 | flags = tf.app.flags 24 | FLAGS = flags.FLAGS 25 | 26 | flags.DEFINE_string('pipeline_config_file', None, 27 | 'A pipeline_pb2.TrainEvalPipelineConfig config file.') 28 | flags.DEFINE_string( 29 | 'ckpt_path', None, 'Path to trained checkpoint, typically of the form ' 30 | 'path/to/model.ckpt') 31 | flags.DEFINE_string('export_dir', None, 'Path to export SavedModel.') 32 | flags.DEFINE_string('input_placeholder_name', 'placeholder_tensor', 33 | 'Name of input placeholder in model\'s signature_def_map.') 34 | flags.DEFINE_string( 35 | 'input_type', 'tf_example', 'Type of input node. Can be ' 36 | 'one of [`image_tensor`, `encoded_image_string_tensor`, ' 37 | '`tf_example`]') 38 | flags.DEFINE_boolean('use_bfloat16', False, 'If true, use tf.bfloat16 on TPU.') 39 | 40 | 41 | def main(argv): 42 | if len(argv) > 1: 43 | raise tf.app.UsageError('Too many command-line arguments.') 44 | export_saved_model_tpu_lib.export(FLAGS.pipeline_config_file, FLAGS.ckpt_path, 45 | FLAGS.export_dir, 46 | FLAGS.input_placeholder_name, 47 | FLAGS.input_type, FLAGS.use_bfloat16) 48 | 49 | 50 | if __name__ == '__main__': 51 | tf.app.flags.mark_flag_as_required('pipeline_config_file') 52 | tf.app.flags.mark_flag_as_required('ckpt_path') 53 | tf.app.flags.mark_flag_as_required('export_dir') 54 | tf.app.run() 55 | -------------------------------------------------------------------------------- /object_detection/tpu_exporters/export_saved_model_tpu_lib_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Test for object detection's TPU exporter.""" 16 | 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | import os 22 | 23 | from absl.testing import parameterized 24 | import numpy as np 25 | import tensorflow as tf 26 | 27 | from object_detection.tpu_exporters import export_saved_model_tpu_lib 28 | 29 | flags = tf.app.flags 30 | FLAGS = flags.FLAGS 31 | 32 | 33 | def get_path(path_suffix): 34 | return os.path.join(tf.resource_loader.get_data_files_path(), 'testdata', 35 | path_suffix) 36 | 37 | 38 | class ExportSavedModelTPUTest(tf.test.TestCase, parameterized.TestCase): 39 | 40 | @parameterized.named_parameters( 41 | ('ssd', get_path('ssd/ssd_pipeline.config'), 'image_tensor', True, 20), 42 | ('faster_rcnn', 43 | get_path('faster_rcnn/faster_rcnn_resnet101_atrous_coco.config'), 44 | 'image_tensor', True, 20)) 45 | def testExportAndLoad(self, 46 | pipeline_config_file, 47 | input_type='image_tensor', 48 | use_bfloat16=False, 49 | repeat=1): 50 | 51 | input_placeholder_name = 'placeholder_tensor' 52 | export_dir = os.path.join(FLAGS.test_tmpdir, 'tpu_saved_model') 53 | if tf.gfile.Exists(export_dir): 54 | tf.gfile.DeleteRecursively(export_dir) 55 | ckpt_path = None 56 | export_saved_model_tpu_lib.export(pipeline_config_file, ckpt_path, 57 | export_dir, input_placeholder_name, 58 | input_type, use_bfloat16) 59 | 60 | inputs = np.random.rand(256, 256, 3) 61 | tensor_dict_out = export_saved_model_tpu_lib.run_inference_from_saved_model( 62 | inputs, export_dir, input_placeholder_name, repeat) 63 | for k, v in tensor_dict_out.items(): 64 | tf.logging.info('{}: {}'.format(k, v)) 65 | 66 | 67 | if __name__ == '__main__': 68 | tf.test.main() 69 | -------------------------------------------------------------------------------- /object_detection/tpu_exporters/testdata/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /object_detection/tpu_exporters/testdata/faster_rcnn/faster_rcnn_resnet101_atrous_coco.config: -------------------------------------------------------------------------------- 1 | # Faster R-CNN with Resnet-101 (v1), Atrous version 2 | # Trained on COCO, initialized from Imagenet classification checkpoint 3 | 4 | model { 5 | faster_rcnn { 6 | num_classes: 90 7 | image_resizer { 8 | keep_aspect_ratio_resizer { 9 | min_dimension: 600 10 | max_dimension: 1024 11 | } 12 | } 13 | feature_extractor { 14 | type: 'faster_rcnn_resnet101' 15 | first_stage_features_stride: 8 16 | } 17 | first_stage_anchor_generator { 18 | grid_anchor_generator { 19 | scales: [0.25, 0.5, 1.0, 2.0] 20 | aspect_ratios: [0.5, 1.0, 2.0] 21 | height_stride: 8 22 | width_stride: 8 23 | } 24 | } 25 | first_stage_atrous_rate: 2 26 | first_stage_box_predictor_conv_hyperparams { 27 | op: CONV 28 | regularizer { 29 | l2_regularizer { 30 | weight: 0.0 31 | } 32 | } 33 | initializer { 34 | truncated_normal_initializer { 35 | stddev: 0.01 36 | } 37 | } 38 | } 39 | first_stage_nms_score_threshold: 0.0 40 | first_stage_nms_iou_threshold: 0.7 41 | first_stage_max_proposals: 300 42 | first_stage_localization_loss_weight: 2.0 43 | first_stage_objectness_loss_weight: 1.0 44 | initial_crop_size: 14 45 | maxpool_kernel_size: 2 46 | maxpool_stride: 2 47 | second_stage_box_predictor { 48 | mask_rcnn_box_predictor { 49 | use_dropout: false 50 | dropout_keep_probability: 1.0 51 | fc_hyperparams { 52 | op: FC 53 | regularizer { 54 | l2_regularizer { 55 | weight: 0.0 56 | } 57 | } 58 | initializer { 59 | variance_scaling_initializer { 60 | factor: 1.0 61 | uniform: true 62 | mode: FAN_AVG 63 | } 64 | } 65 | } 66 | } 67 | } 68 | second_stage_post_processing { 69 | batch_non_max_suppression { 70 | score_threshold: 0.0 71 | iou_threshold: 0.6 72 | max_detections_per_class: 100 73 | max_total_detections: 300 74 | } 75 | score_converter: SOFTMAX 76 | } 77 | second_stage_localization_loss_weight: 2.0 78 | second_stage_classification_loss_weight: 1.0 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /object_detection/tpu_exporters/testdata/ssd/ssd_pipeline.config: -------------------------------------------------------------------------------- 1 | model { 2 | ssd { 3 | num_classes: 2 4 | image_resizer { 5 | fixed_shape_resizer { 6 | height: 1280 7 | width: 1280 8 | } 9 | } 10 | feature_extractor { 11 | type: "ssd_resnet50_v1_fpn" 12 | depth_multiplier: 1.0 13 | min_depth: 16 14 | conv_hyperparams { 15 | regularizer { 16 | l2_regularizer { 17 | weight: 0.000399999989895 18 | } 19 | } 20 | initializer { 21 | truncated_normal_initializer { 22 | mean: 0.0 23 | stddev: 0.0299999993294 24 | } 25 | } 26 | activation: RELU_6 27 | batch_norm { 28 | decay: 0.996999979019 29 | center: true 30 | scale: true 31 | epsilon: 0.0010000000475 32 | train: true 33 | } 34 | } 35 | override_base_feature_extractor_hyperparams: true 36 | fpn { 37 | min_level: 2 38 | } 39 | } 40 | box_coder { 41 | faster_rcnn_box_coder { 42 | y_scale: 10.0 43 | x_scale: 10.0 44 | height_scale: 5.0 45 | width_scale: 5.0 46 | } 47 | } 48 | matcher { 49 | argmax_matcher { 50 | matched_threshold: 0.5 51 | unmatched_threshold: 0.5 52 | ignore_thresholds: false 53 | negatives_lower_than_unmatched: true 54 | force_match_for_each_row: true 55 | use_matmul_gather: true 56 | } 57 | } 58 | similarity_calculator { 59 | iou_similarity { 60 | } 61 | } 62 | box_predictor { 63 | weight_shared_convolutional_box_predictor { 64 | conv_hyperparams { 65 | regularizer { 66 | l2_regularizer { 67 | weight: 0.000399999989895 68 | } 69 | } 70 | initializer { 71 | random_normal_initializer { 72 | mean: 0.0 73 | stddev: 0.00999999977648 74 | } 75 | } 76 | activation: RELU_6 77 | batch_norm { 78 | decay: 0.996999979019 79 | scale: true 80 | epsilon: 0.0010000000475 81 | } 82 | } 83 | depth: 256 84 | num_layers_before_predictor: 4 85 | kernel_size: 3 86 | class_prediction_bias_init: -4.59999990463 87 | } 88 | } 89 | anchor_generator { 90 | multiscale_anchor_generator { 91 | min_level: 2 92 | max_level: 7 93 | anchor_scale: 3.0 94 | aspect_ratios: 1.0 95 | aspect_ratios: 2.0 96 | aspect_ratios: 0.5 97 | scales_per_octave: 2 98 | } 99 | } 100 | post_processing { 101 | batch_non_max_suppression { 102 | score_threshold: 9.99999993923e-09 103 | iou_threshold: 0.600000023842 104 | max_detections_per_class: 300 105 | max_total_detections: 600 106 | use_static_shapes: true 107 | } 108 | score_converter: SIGMOID 109 | } 110 | normalize_loss_by_num_matches: true 111 | loss { 112 | localization_loss { 113 | weighted_smooth_l1 { 114 | } 115 | } 116 | classification_loss { 117 | weighted_sigmoid_focal { 118 | gamma: 2.0 119 | alpha: 0.25 120 | } 121 | } 122 | classification_weight: 1.0 123 | localization_weight: 1.0 124 | } 125 | encode_background_as_zeros: true 126 | normalize_loc_loss_by_codesize: true 127 | inplace_batchnorm_update: true 128 | freeze_batchnorm: false 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /object_detection/tpu_exporters/utils.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Utilities for TPU inference.""" 16 | from __future__ import absolute_import 17 | from __future__ import division 18 | from __future__ import print_function 19 | 20 | import tensorflow as tf 21 | 22 | 23 | def bfloat16_to_float32(tensor): 24 | """Converts a tensor to tf.float32 only if it is tf.bfloat16.""" 25 | if tensor.dtype == tf.bfloat16: 26 | return tf.cast(tensor, dtype=tf.float32) 27 | else: 28 | return tensor 29 | 30 | 31 | def bfloat16_to_float32_nested(bfloat16_tensor_dict): 32 | """Converts bfloat16 tensors in a nested structure to float32. 33 | 34 | Other tensors not of dtype bfloat16 will be left as is. 35 | 36 | Args: 37 | bfloat16_tensor_dict: A Python dict, values being Tensor or Python 38 | list/tuple of Tensor. 39 | 40 | Returns: 41 | A Python dict with the same structure as `bfloat16_tensor_dict`, 42 | with all bfloat16 tensors converted to float32. 43 | """ 44 | float32_tensor_dict = {} 45 | for k, v in bfloat16_tensor_dict.items(): 46 | if isinstance(v, tf.Tensor): 47 | float32_tensor_dict[k] = bfloat16_to_float32(v) 48 | elif isinstance(v, (list, tuple)): 49 | float32_tensor_dict[k] = [bfloat16_to_float32(t) for t in v] 50 | return float32_tensor_dict 51 | -------------------------------------------------------------------------------- /object_detection/tpu_exporters/utils_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Test for Utility functions.""" 16 | 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | import tensorflow as tf 22 | 23 | from object_detection.tpu_exporters import utils 24 | 25 | 26 | class UtilsTest(tf.test.TestCase): 27 | 28 | def testBfloat16ToFloat32(self): 29 | bfloat16_tensor = tf.random.uniform([2, 3], dtype=tf.bfloat16) 30 | float32_tensor = utils.bfloat16_to_float32(bfloat16_tensor) 31 | self.assertEqual(float32_tensor.dtype, tf.float32) 32 | 33 | def testOtherDtypesNotConverted(self): 34 | int32_tensor = tf.ones([2, 3], dtype=tf.int32) 35 | converted_tensor = utils.bfloat16_to_float32(int32_tensor) 36 | self.assertEqual(converted_tensor.dtype, tf.int32) 37 | 38 | def testBfloat16ToFloat32Nested(self): 39 | tensor_dict = { 40 | 'key1': tf.random.uniform([2, 3], dtype=tf.bfloat16), 41 | 'key2': [ 42 | tf.random.uniform([1, 2], dtype=tf.bfloat16) for _ in range(3) 43 | ], 44 | 'key3': tf.ones([2, 3], dtype=tf.int32), 45 | } 46 | tensor_dict = utils.bfloat16_to_float32_nested(tensor_dict) 47 | 48 | self.assertEqual(tensor_dict['key1'].dtype, tf.float32) 49 | for t in tensor_dict['key2']: 50 | self.assertEqual(t.dtype, tf.float32) 51 | self.assertEqual(tensor_dict['key3'].dtype, tf.int32) 52 | 53 | 54 | if __name__ == '__main__': 55 | tf.test.main() 56 | -------------------------------------------------------------------------------- /object_detection/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/utils/__init__.py -------------------------------------------------------------------------------- /object_detection/utils/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/utils/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /object_detection/utils/__pycache__/label_map_util.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/utils/__pycache__/label_map_util.cpython-37.pyc -------------------------------------------------------------------------------- /object_detection/utils/__pycache__/ops.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/utils/__pycache__/ops.cpython-37.pyc -------------------------------------------------------------------------------- /object_detection/utils/__pycache__/shape_utils.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/utils/__pycache__/shape_utils.cpython-37.pyc -------------------------------------------------------------------------------- /object_detection/utils/__pycache__/spatial_transform_ops.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/utils/__pycache__/spatial_transform_ops.cpython-37.pyc -------------------------------------------------------------------------------- /object_detection/utils/__pycache__/static_shape.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/utils/__pycache__/static_shape.cpython-37.pyc -------------------------------------------------------------------------------- /object_detection/utils/__pycache__/visualization_utils.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/object_detection/utils/__pycache__/visualization_utils.cpython-37.pyc -------------------------------------------------------------------------------- /object_detection/utils/category_util.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Functions for importing/exporting Object Detection categories.""" 17 | 18 | from __future__ import absolute_import 19 | from __future__ import division 20 | from __future__ import print_function 21 | 22 | import csv 23 | 24 | import tensorflow as tf 25 | 26 | 27 | def load_categories_from_csv_file(csv_path): 28 | """Loads categories from a csv file. 29 | 30 | The CSV file should have one comma delimited numeric category id and string 31 | category name pair per line. For example: 32 | 33 | 0,"cat" 34 | 1,"dog" 35 | 2,"bird" 36 | ... 37 | 38 | Args: 39 | csv_path: Path to the csv file to be parsed into categories. 40 | Returns: 41 | categories: A list of dictionaries representing all possible categories. 42 | The categories will contain an integer 'id' field and a string 43 | 'name' field. 44 | Raises: 45 | ValueError: If the csv file is incorrectly formatted. 46 | """ 47 | categories = [] 48 | 49 | with tf.gfile.Open(csv_path, 'r') as csvfile: 50 | reader = csv.reader(csvfile, delimiter=',', quotechar='"') 51 | for row in reader: 52 | if not row: 53 | continue 54 | 55 | if len(row) != 2: 56 | raise ValueError('Expected 2 fields per row in csv: %s' % ','.join(row)) 57 | 58 | category_id = int(row[0]) 59 | category_name = row[1] 60 | categories.append({'id': category_id, 'name': category_name}) 61 | 62 | return categories 63 | 64 | 65 | def save_categories_to_csv_file(categories, csv_path): 66 | """Saves categories to a csv file. 67 | 68 | Args: 69 | categories: A list of dictionaries representing categories to save to file. 70 | Each category must contain an 'id' and 'name' field. 71 | csv_path: Path to the csv file to be parsed into categories. 72 | """ 73 | categories.sort(key=lambda x: x['id']) 74 | with tf.gfile.Open(csv_path, 'w') as csvfile: 75 | writer = csv.writer(csvfile, delimiter=',', quotechar='"') 76 | for category in categories: 77 | writer.writerow([category['id'], category['name']]) 78 | -------------------------------------------------------------------------------- /object_detection/utils/category_util_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for object_detection.utils.category_util.""" 17 | 18 | from __future__ import absolute_import 19 | from __future__ import division 20 | from __future__ import print_function 21 | 22 | import os 23 | 24 | import tensorflow as tf 25 | 26 | from object_detection.utils import category_util 27 | 28 | 29 | class EvalUtilTest(tf.test.TestCase): 30 | 31 | def test_load_categories_from_csv_file(self): 32 | csv_data = """ 33 | 0,"cat" 34 | 1,"dog" 35 | 2,"bird" 36 | """.strip(' ') 37 | csv_path = os.path.join(self.get_temp_dir(), 'test.csv') 38 | with tf.gfile.Open(csv_path, 'wb') as f: 39 | f.write(csv_data) 40 | 41 | categories = category_util.load_categories_from_csv_file(csv_path) 42 | self.assertTrue({'id': 0, 'name': 'cat'} in categories) 43 | self.assertTrue({'id': 1, 'name': 'dog'} in categories) 44 | self.assertTrue({'id': 2, 'name': 'bird'} in categories) 45 | 46 | def test_save_categories_to_csv_file(self): 47 | categories = [ 48 | {'id': 0, 'name': 'cat'}, 49 | {'id': 1, 'name': 'dog'}, 50 | {'id': 2, 'name': 'bird'}, 51 | ] 52 | csv_path = os.path.join(self.get_temp_dir(), 'test.csv') 53 | category_util.save_categories_to_csv_file(categories, csv_path) 54 | saved_categories = category_util.load_categories_from_csv_file(csv_path) 55 | self.assertEqual(saved_categories, categories) 56 | 57 | 58 | if __name__ == '__main__': 59 | tf.test.main() 60 | -------------------------------------------------------------------------------- /object_detection/utils/context_manager.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Python context management helper.""" 16 | 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | 22 | class IdentityContextManager(object): 23 | """Returns an identity context manager that does nothing. 24 | 25 | This is helpful in setting up conditional `with` statement as below: 26 | 27 | with slim.arg_scope(x) if use_slim_scope else IdentityContextManager(): 28 | do_stuff() 29 | 30 | """ 31 | 32 | def __enter__(self): 33 | return None 34 | 35 | def __exit__(self, exec_type, exec_value, traceback): 36 | del exec_type 37 | del exec_value 38 | del traceback 39 | return False 40 | -------------------------------------------------------------------------------- /object_detection/utils/context_manager_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Tests for tensorflow_models.object_detection.utils.context_manager.""" 16 | 17 | from __future__ import absolute_import 18 | from __future__ import division 19 | from __future__ import print_function 20 | 21 | import tensorflow as tf 22 | from object_detection.utils import context_manager 23 | 24 | 25 | class ContextManagerTest(tf.test.TestCase): 26 | 27 | def test_identity_context_manager(self): 28 | with context_manager.IdentityContextManager() as identity_context: 29 | self.assertIsNone(identity_context) 30 | 31 | 32 | if __name__ == '__main__': 33 | tf.test.main() 34 | -------------------------------------------------------------------------------- /object_detection/utils/dataset_util.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Utility functions for creating TFRecord data sets.""" 17 | 18 | from __future__ import absolute_import 19 | from __future__ import division 20 | from __future__ import print_function 21 | 22 | import tensorflow as tf 23 | 24 | 25 | def int64_feature(value): 26 | return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) 27 | 28 | 29 | def int64_list_feature(value): 30 | return tf.train.Feature(int64_list=tf.train.Int64List(value=value)) 31 | 32 | 33 | def bytes_feature(value): 34 | return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) 35 | 36 | 37 | def bytes_list_feature(value): 38 | return tf.train.Feature(bytes_list=tf.train.BytesList(value=value)) 39 | 40 | 41 | def float_list_feature(value): 42 | return tf.train.Feature(float_list=tf.train.FloatList(value=value)) 43 | 44 | 45 | def read_examples_list(path): 46 | """Read list of training or validation examples. 47 | 48 | The file is assumed to contain a single example per line where the first 49 | token in the line is an identifier that allows us to find the image and 50 | annotation xml for that example. 51 | 52 | For example, the line: 53 | xyz 3 54 | would allow us to find files xyz.jpg and xyz.xml (the 3 would be ignored). 55 | 56 | Args: 57 | path: absolute path to examples list file. 58 | 59 | Returns: 60 | list of example identifiers (strings). 61 | """ 62 | with tf.gfile.GFile(path) as fid: 63 | lines = fid.readlines() 64 | return [line.strip().split(' ')[0] for line in lines] 65 | 66 | 67 | def recursive_parse_xml_to_dict(xml): 68 | """Recursively parses XML contents to python dict. 69 | 70 | We assume that `object` tags are the only ones that can appear 71 | multiple times at the same level of a tree. 72 | 73 | Args: 74 | xml: xml tree obtained by parsing XML file contents using lxml.etree 75 | 76 | Returns: 77 | Python dictionary holding XML contents. 78 | """ 79 | if not xml: 80 | return {xml.tag: xml.text} 81 | result = {} 82 | for child in xml: 83 | child_result = recursive_parse_xml_to_dict(child) 84 | if child.tag != 'object': 85 | result[child.tag] = child_result[child.tag] 86 | else: 87 | if child.tag not in result: 88 | result[child.tag] = [] 89 | result[child.tag].append(child_result[child.tag]) 90 | return {xml.tag: result} 91 | -------------------------------------------------------------------------------- /object_detection/utils/dataset_util_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for object_detection.utils.dataset_util.""" 17 | 18 | from __future__ import absolute_import 19 | from __future__ import division 20 | from __future__ import print_function 21 | 22 | import os 23 | import tensorflow as tf 24 | 25 | from object_detection.utils import dataset_util 26 | 27 | 28 | class DatasetUtilTest(tf.test.TestCase): 29 | 30 | def test_read_examples_list(self): 31 | example_list_data = """example1 1\nexample2 2""" 32 | example_list_path = os.path.join(self.get_temp_dir(), 'examples.txt') 33 | with tf.gfile.Open(example_list_path, 'wb') as f: 34 | f.write(example_list_data) 35 | 36 | examples = dataset_util.read_examples_list(example_list_path) 37 | self.assertListEqual(['example1', 'example2'], examples) 38 | 39 | 40 | if __name__ == '__main__': 41 | tf.test.main() 42 | -------------------------------------------------------------------------------- /object_detection/utils/json_utils.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Utilities for dealing with writing json strings. 16 | 17 | json_utils wraps json.dump and json.dumps so that they can be used to safely 18 | control the precision of floats when writing to json strings or files. 19 | """ 20 | import json 21 | from json import encoder 22 | 23 | 24 | def Dump(obj, fid, float_digits=-1, **params): 25 | """Wrapper of json.dump that allows specifying the float precision used. 26 | 27 | Args: 28 | obj: The object to dump. 29 | fid: The file id to write to. 30 | float_digits: The number of digits of precision when writing floats out. 31 | **params: Additional parameters to pass to json.dumps. 32 | """ 33 | original_encoder = encoder.FLOAT_REPR 34 | if float_digits >= 0: 35 | encoder.FLOAT_REPR = lambda o: format(o, '.%df' % float_digits) 36 | try: 37 | json.dump(obj, fid, **params) 38 | finally: 39 | encoder.FLOAT_REPR = original_encoder 40 | 41 | 42 | def Dumps(obj, float_digits=-1, **params): 43 | """Wrapper of json.dumps that allows specifying the float precision used. 44 | 45 | Args: 46 | obj: The object to dump. 47 | float_digits: The number of digits of precision when writing floats out. 48 | **params: Additional parameters to pass to json.dumps. 49 | 50 | Returns: 51 | output: JSON string representation of obj. 52 | """ 53 | original_encoder = encoder.FLOAT_REPR 54 | original_c_make_encoder = encoder.c_make_encoder 55 | if float_digits >= 0: 56 | encoder.FLOAT_REPR = lambda o: format(o, '.%df' % float_digits) 57 | encoder.c_make_encoder = None 58 | try: 59 | output = json.dumps(obj, **params) 60 | finally: 61 | encoder.FLOAT_REPR = original_encoder 62 | encoder.c_make_encoder = original_c_make_encoder 63 | 64 | return output 65 | 66 | 67 | def PrettyParams(**params): 68 | """Returns parameters for use with Dump and Dumps to output pretty json. 69 | 70 | Example usage: 71 | ```json_str = json_utils.Dumps(obj, **json_utils.PrettyParams())``` 72 | ```json_str = json_utils.Dumps( 73 | obj, **json_utils.PrettyParams(allow_nans=False))``` 74 | 75 | Args: 76 | **params: Additional params to pass to json.dump or json.dumps. 77 | 78 | Returns: 79 | params: Parameters that are compatible with json_utils.Dump and 80 | json_utils.Dumps. 81 | """ 82 | params['float_digits'] = 4 83 | params['sort_keys'] = True 84 | params['indent'] = 2 85 | params['separators'] = (',', ': ') 86 | return params 87 | 88 | -------------------------------------------------------------------------------- /object_detection/utils/model_util_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Test utility functions for manipulating Keras models.""" 17 | 18 | from __future__ import absolute_import 19 | from __future__ import division 20 | from __future__ import print_function 21 | 22 | import tensorflow as tf 23 | 24 | from object_detection.utils import model_util 25 | 26 | 27 | class ExtractSubmodelUtilTest(tf.test.TestCase): 28 | 29 | def test_simple_model(self): 30 | inputs = tf.keras.Input(shape=(256,)) # Returns a placeholder tensor 31 | 32 | # A layer instance is callable on a tensor, and returns a tensor. 33 | x = tf.keras.layers.Dense(128, activation='relu', name='a')(inputs) 34 | x = tf.keras.layers.Dense(64, activation='relu', name='b')(x) 35 | x = tf.keras.layers.Dense(32, activation='relu', name='c')(x) 36 | x = tf.keras.layers.Dense(16, activation='relu', name='d')(x) 37 | x = tf.keras.layers.Dense(8, activation='relu', name='e')(x) 38 | predictions = tf.keras.layers.Dense(10, activation='softmax')(x) 39 | 40 | model = tf.keras.Model(inputs=inputs, outputs=predictions) 41 | 42 | new_in = model.get_layer( 43 | name='b').input 44 | new_out = model.get_layer( 45 | name='d').output 46 | 47 | new_model = model_util.extract_submodel( 48 | model=model, 49 | inputs=new_in, 50 | outputs=new_out) 51 | 52 | batch_size = 3 53 | ones = tf.ones((batch_size, 128)) 54 | final_out = new_model(ones) 55 | self.assertAllEqual(final_out.shape, (batch_size, 16)) 56 | 57 | if __name__ == '__main__': 58 | tf.test.main() 59 | -------------------------------------------------------------------------------- /object_detection/utils/np_box_mask_list.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Numpy BoxMaskList classes and functions.""" 17 | 18 | from __future__ import absolute_import 19 | from __future__ import division 20 | from __future__ import print_function 21 | 22 | import numpy as np 23 | from object_detection.utils import np_box_list 24 | 25 | 26 | class BoxMaskList(np_box_list.BoxList): 27 | """Convenience wrapper for BoxList with masks. 28 | 29 | BoxMaskList extends the np_box_list.BoxList to contain masks as well. 30 | In particular, its constructor receives both boxes and masks. Note that the 31 | masks correspond to the full image. 32 | """ 33 | 34 | def __init__(self, box_data, mask_data): 35 | """Constructs box collection. 36 | 37 | Args: 38 | box_data: a numpy array of shape [N, 4] representing box coordinates 39 | mask_data: a numpy array of shape [N, height, width] representing masks 40 | with values are in {0,1}. The masks correspond to the full 41 | image. The height and the width will be equal to image height and width. 42 | 43 | Raises: 44 | ValueError: if bbox data is not a numpy array 45 | ValueError: if invalid dimensions for bbox data 46 | ValueError: if mask data is not a numpy array 47 | ValueError: if invalid dimension for mask data 48 | """ 49 | super(BoxMaskList, self).__init__(box_data) 50 | if not isinstance(mask_data, np.ndarray): 51 | raise ValueError('Mask data must be a numpy array.') 52 | if len(mask_data.shape) != 3: 53 | raise ValueError('Invalid dimensions for mask data.') 54 | if mask_data.dtype != np.uint8: 55 | raise ValueError('Invalid data type for mask data: uint8 is required.') 56 | if mask_data.shape[0] != box_data.shape[0]: 57 | raise ValueError('There should be the same number of boxes and masks.') 58 | self.data['masks'] = mask_data 59 | 60 | def get_masks(self): 61 | """Convenience function for accessing masks. 62 | 63 | Returns: 64 | a numpy array of shape [N, height, width] representing masks 65 | """ 66 | return self.get_field('masks') 67 | -------------------------------------------------------------------------------- /object_detection/utils/np_box_ops_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for object_detection.np_box_ops.""" 17 | 18 | from __future__ import absolute_import 19 | from __future__ import division 20 | from __future__ import print_function 21 | 22 | import numpy as np 23 | import tensorflow as tf 24 | 25 | from object_detection.utils import np_box_ops 26 | 27 | 28 | class BoxOpsTests(tf.test.TestCase): 29 | 30 | def setUp(self): 31 | boxes1 = np.array([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]], 32 | dtype=float) 33 | boxes2 = np.array([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], 34 | [0.0, 0.0, 20.0, 20.0]], 35 | dtype=float) 36 | self.boxes1 = boxes1 37 | self.boxes2 = boxes2 38 | 39 | def testArea(self): 40 | areas = np_box_ops.area(self.boxes1) 41 | expected_areas = np.array([6.0, 5.0], dtype=float) 42 | self.assertAllClose(expected_areas, areas) 43 | 44 | def testIntersection(self): 45 | intersection = np_box_ops.intersection(self.boxes1, self.boxes2) 46 | expected_intersection = np.array([[2.0, 0.0, 6.0], [1.0, 0.0, 5.0]], 47 | dtype=float) 48 | self.assertAllClose(intersection, expected_intersection) 49 | 50 | def testIOU(self): 51 | iou = np_box_ops.iou(self.boxes1, self.boxes2) 52 | expected_iou = np.array([[2.0 / 16.0, 0.0, 6.0 / 400.0], 53 | [1.0 / 16.0, 0.0, 5.0 / 400.0]], 54 | dtype=float) 55 | self.assertAllClose(iou, expected_iou) 56 | 57 | def testIOA(self): 58 | boxes1 = np.array([[0.25, 0.25, 0.75, 0.75], 59 | [0.0, 0.0, 0.5, 0.75]], 60 | dtype=np.float32) 61 | boxes2 = np.array([[0.5, 0.25, 1.0, 1.0], 62 | [0.0, 0.0, 1.0, 1.0]], 63 | dtype=np.float32) 64 | ioa21 = np_box_ops.ioa(boxes2, boxes1) 65 | expected_ioa21 = np.array([[0.5, 0.0], 66 | [1.0, 1.0]], 67 | dtype=np.float32) 68 | self.assertAllClose(ioa21, expected_ioa21) 69 | 70 | 71 | if __name__ == '__main__': 72 | tf.test.main() 73 | -------------------------------------------------------------------------------- /object_detection/utils/patch_ops.py: -------------------------------------------------------------------------------- 1 | # Copyright 2019 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Operations for image patches.""" 17 | 18 | from __future__ import absolute_import 19 | from __future__ import division 20 | from __future__ import print_function 21 | 22 | import tensorflow as tf 23 | 24 | 25 | def get_patch_mask(y, x, patch_size, image_shape): 26 | """Creates a 2D mask array for a square patch of a given size and location. 27 | 28 | The mask is created with its center at the y and x coordinates, which must be 29 | within the image. While the mask center must be within the image, the mask 30 | itself can be partially outside of it. If patch_size is an even number, then 31 | the mask is created with lower-valued coordinates first (top and left). 32 | 33 | Args: 34 | y: An integer or scalar int32 tensor. The vertical coordinate of the 35 | patch mask center. Must be within the range [0, image_height). 36 | x: An integer or scalar int32 tensor. The horizontal coordinate of the 37 | patch mask center. Must be within the range [0, image_width). 38 | patch_size: An integer or scalar int32 tensor. The square size of the 39 | patch mask. Must be at least 1. 40 | image_shape: A list or 1D int32 tensor representing the shape of the image 41 | to which the mask will correspond, with the first two values being image 42 | height and width. For example, [image_height, image_width] or 43 | [image_height, image_width, image_channels]. 44 | 45 | Returns: 46 | Boolean mask tensor of shape [image_height, image_width] with True values 47 | for the patch. 48 | 49 | Raises: 50 | tf.errors.InvalidArgumentError: if x is not in the range [0, image_width), y 51 | is not in the range [0, image_height), or patch_size is not at least 1. 52 | """ 53 | image_hw = image_shape[:2] 54 | mask_center_yx = tf.stack([y, x]) 55 | with tf.control_dependencies([ 56 | tf.debugging.assert_greater_equal( 57 | patch_size, 1, 58 | message='Patch size must be >= 1'), 59 | tf.debugging.assert_greater_equal( 60 | mask_center_yx, 0, 61 | message='Patch center (y, x) must be >= (0, 0)'), 62 | tf.debugging.assert_less( 63 | mask_center_yx, image_hw, 64 | message='Patch center (y, x) must be < image (h, w)') 65 | ]): 66 | mask_center_yx = tf.identity(mask_center_yx) 67 | 68 | half_patch_size = tf.cast(patch_size, dtype=tf.float32) / 2 69 | start_yx = mask_center_yx - tf.cast(tf.floor(half_patch_size), dtype=tf.int32) 70 | end_yx = mask_center_yx + tf.cast(tf.ceil(half_patch_size), dtype=tf.int32) 71 | 72 | start_yx = tf.maximum(start_yx, 0) 73 | end_yx = tf.minimum(end_yx, image_hw) 74 | 75 | start_y = start_yx[0] 76 | start_x = start_yx[1] 77 | end_y = end_yx[0] 78 | end_x = end_yx[1] 79 | 80 | lower_pad = image_hw[0] - end_y 81 | upper_pad = start_y 82 | left_pad = start_x 83 | right_pad = image_hw[1] - end_x 84 | mask = tf.ones([end_y - start_y, end_x - start_x], dtype=tf.bool) 85 | return tf.pad(mask, [[upper_pad, lower_pad], [left_pad, right_pad]]) 86 | -------------------------------------------------------------------------------- /object_detection/utils/static_shape.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Helper functions to access TensorShape values. 17 | 18 | The rank 4 tensor_shape must be of the form [batch_size, height, width, depth]. 19 | """ 20 | 21 | from __future__ import absolute_import 22 | from __future__ import division 23 | from __future__ import print_function 24 | 25 | 26 | def get_dim_as_int(dim): 27 | """Utility to get v1 or v2 TensorShape dim as an int. 28 | 29 | Args: 30 | dim: The TensorShape dimension to get as an int 31 | 32 | Returns: 33 | None or an int. 34 | """ 35 | try: 36 | return dim.value 37 | except AttributeError: 38 | return dim 39 | 40 | 41 | def get_batch_size(tensor_shape): 42 | """Returns batch size from the tensor shape. 43 | 44 | Args: 45 | tensor_shape: A rank 4 TensorShape. 46 | 47 | Returns: 48 | An integer representing the batch size of the tensor. 49 | """ 50 | tensor_shape.assert_has_rank(rank=4) 51 | return get_dim_as_int(tensor_shape[0]) 52 | 53 | 54 | def get_height(tensor_shape): 55 | """Returns height from the tensor shape. 56 | 57 | Args: 58 | tensor_shape: A rank 4 TensorShape. 59 | 60 | Returns: 61 | An integer representing the height of the tensor. 62 | """ 63 | tensor_shape.assert_has_rank(rank=4) 64 | return get_dim_as_int(tensor_shape[1]) 65 | 66 | 67 | def get_width(tensor_shape): 68 | """Returns width from the tensor shape. 69 | 70 | Args: 71 | tensor_shape: A rank 4 TensorShape. 72 | 73 | Returns: 74 | An integer representing the width of the tensor. 75 | """ 76 | tensor_shape.assert_has_rank(rank=4) 77 | return get_dim_as_int(tensor_shape[2]) 78 | 79 | 80 | def get_depth(tensor_shape): 81 | """Returns depth from the tensor shape. 82 | 83 | Args: 84 | tensor_shape: A rank 4 TensorShape. 85 | 86 | Returns: 87 | An integer representing the depth of the tensor. 88 | """ 89 | tensor_shape.assert_has_rank(rank=4) 90 | return get_dim_as_int(tensor_shape[3]) 91 | -------------------------------------------------------------------------------- /object_detection/utils/static_shape_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for object_detection.utils.static_shape.""" 17 | 18 | from __future__ import absolute_import 19 | from __future__ import division 20 | from __future__ import print_function 21 | 22 | import tensorflow as tf 23 | 24 | from object_detection.utils import static_shape 25 | 26 | 27 | class StaticShapeTest(tf.test.TestCase): 28 | 29 | def test_return_correct_batchSize(self): 30 | tensor_shape = tf.TensorShape(dims=[32, 299, 384, 3]) 31 | self.assertEqual(32, static_shape.get_batch_size(tensor_shape)) 32 | 33 | def test_return_correct_height(self): 34 | tensor_shape = tf.TensorShape(dims=[32, 299, 384, 3]) 35 | self.assertEqual(299, static_shape.get_height(tensor_shape)) 36 | 37 | def test_return_correct_width(self): 38 | tensor_shape = tf.TensorShape(dims=[32, 299, 384, 3]) 39 | self.assertEqual(384, static_shape.get_width(tensor_shape)) 40 | 41 | def test_return_correct_depth(self): 42 | tensor_shape = tf.TensorShape(dims=[32, 299, 384, 3]) 43 | self.assertEqual(3, static_shape.get_depth(tensor_shape)) 44 | 45 | def test_die_on_tensor_shape_with_rank_three(self): 46 | tensor_shape = tf.TensorShape(dims=[32, 299, 384]) 47 | with self.assertRaises(ValueError): 48 | static_shape.get_batch_size(tensor_shape) 49 | static_shape.get_height(tensor_shape) 50 | static_shape.get_width(tensor_shape) 51 | static_shape.get_depth(tensor_shape) 52 | 53 | if __name__ == '__main__': 54 | tf.test.main() 55 | -------------------------------------------------------------------------------- /object_detection/utils/test_utils_test.py: -------------------------------------------------------------------------------- 1 | # Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | 16 | """Tests for object_detection.utils.test_utils.""" 17 | 18 | from __future__ import absolute_import 19 | from __future__ import division 20 | from __future__ import print_function 21 | 22 | import numpy as np 23 | import tensorflow as tf 24 | 25 | from object_detection.utils import test_utils 26 | 27 | 28 | class TestUtilsTest(tf.test.TestCase): 29 | 30 | def test_diagonal_gradient_image(self): 31 | """Tests if a good pyramid image is created.""" 32 | pyramid_image = test_utils.create_diagonal_gradient_image(3, 4, 2) 33 | 34 | # Test which is easy to understand. 35 | expected_first_channel = np.array([[3, 2, 1, 0], 36 | [4, 3, 2, 1], 37 | [5, 4, 3, 2]], dtype=np.float32) 38 | self.assertAllEqual(np.squeeze(pyramid_image[:, :, 0]), 39 | expected_first_channel) 40 | 41 | # Actual test. 42 | expected_image = np.array([[[3, 30], 43 | [2, 20], 44 | [1, 10], 45 | [0, 0]], 46 | [[4, 40], 47 | [3, 30], 48 | [2, 20], 49 | [1, 10]], 50 | [[5, 50], 51 | [4, 40], 52 | [3, 30], 53 | [2, 20]]], dtype=np.float32) 54 | 55 | self.assertAllEqual(pyramid_image, expected_image) 56 | 57 | def test_random_boxes(self): 58 | """Tests if valid random boxes are created.""" 59 | num_boxes = 1000 60 | max_height = 3 61 | max_width = 5 62 | boxes = test_utils.create_random_boxes(num_boxes, 63 | max_height, 64 | max_width) 65 | 66 | true_column = np.ones(shape=(num_boxes)) == 1 67 | self.assertAllEqual(boxes[:, 0] < boxes[:, 2], true_column) 68 | self.assertAllEqual(boxes[:, 1] < boxes[:, 3], true_column) 69 | 70 | self.assertTrue(boxes[:, 0].min() >= 0) 71 | self.assertTrue(boxes[:, 1].min() >= 0) 72 | self.assertTrue(boxes[:, 2].max() <= max_height) 73 | self.assertTrue(boxes[:, 3].max() <= max_width) 74 | 75 | def test_first_rows_close_as_set(self): 76 | a = [1, 2, 3, 0, 0] 77 | b = [3, 2, 1, 0, 0] 78 | k = 3 79 | self.assertTrue(test_utils.first_rows_close_as_set(a, b, k)) 80 | 81 | a = [[1, 2], [1, 4], [0, 0]] 82 | b = [[1, 4 + 1e-9], [1, 2], [0, 0]] 83 | k = 2 84 | self.assertTrue(test_utils.first_rows_close_as_set(a, b, k)) 85 | 86 | a = [[1, 2], [1, 4], [0, 0]] 87 | b = [[1, 4 + 1e-9], [2, 2], [0, 0]] 88 | k = 2 89 | self.assertFalse(test_utils.first_rows_close_as_set(a, b, k)) 90 | 91 | 92 | if __name__ == '__main__': 93 | tf.test.main() 94 | -------------------------------------------------------------------------------- /output/img.xml: -------------------------------------------------------------------------------- 1 | 2 | images 3 | 11.jpg 4 | E:\dataset\resolute\images\11.jpg 5 | 6 | Unknown 7 | 8 | 9 | 471 10 | 511 11 | 3 12 | 13 | 0 14 | 15 | cat 16 | Unspecified 17 | 0 18 | 0 19 | 20 | 100 21 | 100 22 | 200 23 | 200 24 | 25 | 26 | dog 27 | Unspecified 28 | 0 29 | 0 30 | 31 | 100 32 | 100 33 | 200 34 | 200 35 | 36 | 37 | person 38 | Unspecified 39 | 0 40 | 0 41 | 42 | 100 43 | 100 44 | 200 45 | 200 46 | 47 | 48 | cat 49 | Unspecified 50 | 0 51 | 0 52 | 53 | 100 54 | 100 55 | 200 56 | 200 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /readme_images/1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/readme_images/1.PNG -------------------------------------------------------------------------------- /readme_images/2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/readme_images/2.PNG -------------------------------------------------------------------------------- /readme_images/3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/readme_images/3.PNG -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import sys 2 | from PyQt5 import QtCore, QtGui, uic 3 | 4 | qtCreatorFile = "main.ui" 5 | 6 | Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile) 7 | 8 | class myownGUI(QtGui.QMainWindow, Ui_MainWindow): 9 | def __init__(self): 10 | QtGui.QMainWindow.__init__(self) 11 | Ui_MainWindow.__init__(self) 12 | self.setupUi(self) 13 | 14 | 15 | #button 16 | self.Do_button.clicked.connect(self.action) 17 | 18 | #slider 19 | self.SLIDER.valueChanged[int].connect(self.SLIDER_update) 20 | 21 | #"global" variable init. by callback 22 | self.SLIDER_update() 23 | 24 | 25 | #The button callback 26 | def action(self): 27 | print ("DOING ACTION!") 28 | print (self.Slider) 29 | #trying to display the image in the Image_label 30 | image = QtGui.QImage(QtGui.QImageReader(":/images/test.png").read()) 31 | self.Image_label.setPixmap(QtGui.QPixmap(image)) 32 | #self.Image_label.show() #unuseful command? 33 | 34 | 35 | #Slider update callback 36 | def SLIDER_update(self): 37 | self.Slider= self.SLIDER.value() 38 | if (self.Slider % 2 == 0): #even 39 | self.Slider = self.Slider + 1 40 | self.Slider_label.setText(str(self.Slider)) 41 | 42 | if __name__ == "__main__": 43 | app = QtGui.QApplication(sys.argv) 44 | window = myownGUI() 45 | window.show() 46 | sys.exit(app.exec_()) -------------------------------------------------------------------------------- /test_images/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/test_images/1.jpg -------------------------------------------------------------------------------- /test_images/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/test_images/10.jpg -------------------------------------------------------------------------------- /test_images/11.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/test_images/11.jpg -------------------------------------------------------------------------------- /test_images/12.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/test_images/12.jpg -------------------------------------------------------------------------------- /test_images/13.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/test_images/13.jpg -------------------------------------------------------------------------------- /test_images/14.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/test_images/14.jpg -------------------------------------------------------------------------------- /test_images/16.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/test_images/16.jpg -------------------------------------------------------------------------------- /test_images/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/test_images/2.jpg -------------------------------------------------------------------------------- /test_images/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/test_images/5.jpg -------------------------------------------------------------------------------- /test_images/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/test_images/6.jpg -------------------------------------------------------------------------------- /test_images/7.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/test_images/7.jpg -------------------------------------------------------------------------------- /test_images/8.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bislara/Object-detection-GUI/ce28b7e45e39b5526b90adef5de91a08f12bd682/test_images/8.jpeg -------------------------------------------------------------------------------- /train.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import os 3 | import six.moves.urllib as urllib 4 | import sys 5 | import tarfile 6 | import tensorflow as tf 7 | import zipfile 8 | import pathlib 9 | 10 | from collections import defaultdict 11 | from io import StringIO 12 | from matplotlib import pyplot as plt 13 | from PIL import Image 14 | from IPython.display import display 15 | 16 | from object_detection import visualization_utils as vis_util 17 | 18 | 19 | 20 | PATH_TO_LABELS = 'data/label_map.pbtxt' 21 | # category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True) 22 | 23 | 24 | def load_model(model_name): 25 | # base_url = 'models/' 26 | model_file = 'models/'+model_name + '/' 27 | # model_dir = tf.keras.utils.get_file(fname=model_name,untar=True) 28 | 29 | model_dir = model_file + "saved_model" 30 | 31 | model = tf.saved_model.load(str(model_dir)) 32 | model = model.signatures['serving_default'] 33 | 34 | return model 35 | 36 | 37 | def run_inference_for_single_image(model, image): 38 | image = np.asarray(image) 39 | # The input needs to be a tensor, convert it using `tf.convert_to_tensor`. 40 | input_tensor = tf.convert_to_tensor(image) 41 | # The model expects a batch of images, so add an axis with `tf.newaxis`. 42 | input_tensor = input_tensor[tf.newaxis,...] 43 | 44 | # Run inference 45 | output_dict = model(input_tensor) 46 | 47 | # All outputs are batches tensors. 48 | # Convert to numpy arrays, and take index [0] to remove the batch dimension. 49 | # We're only interested in the first num_detections. 50 | num_detections = int(output_dict.pop('num_detections')) 51 | output_dict = {key:value[0, :num_detections].numpy() 52 | for key,value in output_dict.items()} 53 | output_dict['num_detections'] = num_detections 54 | 55 | # detection_classes should be ints. 56 | output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64) 57 | 58 | # Handle models with masks: 59 | if 'detection_masks' in output_dict: 60 | # Reframe the the bbox mask to the image size. 61 | detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks( 62 | output_dict['detection_masks'], output_dict['detection_boxes'], 63 | image.shape[0], image.shape[1]) 64 | detection_masks_reframed = tf.cast(detection_masks_reframed > 0.5, 65 | tf.uint8) 66 | output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy() 67 | 68 | return output_dict 69 | 70 | 71 | def show_inference(model, image_path): 72 | # the array based representation of the image will be used later in order to prepare the 73 | # result image with boxes and labels on it. 74 | image_np = np.array(Image.open(image_path)) 75 | # Actual detection. 76 | output_dict = run_inference_for_single_image(model, image_np) 77 | print(output_dict) 78 | class_list = output_dict["detection_classes"] 79 | print(class_list[0]) 80 | print(class_list[1]) 81 | # Visualization of the results of a detection. 82 | # vis_util.visualize_boxes_and_labels_on_image_array( 83 | # image_np, 84 | # output_dict['detection_boxes'], 85 | # output_dict['detection_classes'], 86 | # output_dict['detection_scores'], 87 | # category_index, 88 | # instance_masks=output_dict.get('detection_masks_reframed', None), 89 | # use_normalized_coordinates=True, 90 | # line_thickness=8) 91 | 92 | # display(Image.fromarray(image_np)) 93 | 94 | model_name = 'ssd_mobilenet_v1_coco_2018_01_28' 95 | detection_model = load_model(model_name) 96 | print(detection_model.inputs) 97 | 98 | image_path = "images/1.jpg" 99 | show_inference(detection_model, image_path) 100 | 101 | --------------------------------------------------------------------------------